Closed Bug 1375794 Opened 7 years ago Closed 7 years ago

document.execCommand("Paste") does not work

Categories

(WebExtensions :: Frontend, defect)

54 Branch
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED DUPLICATE of bug 1340578

People

(Reporter: jzav, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0
Build ID: 20170608105825

Steps to reproduce:

Have FF54 and clipboardRead permission in manifest file but cannot make document.execCommand("Paste") work:

function paste() {
var pasteText = document.querySelector("#my_id");
pasteText.focus();
document.execCommand("Paste");
}

document.querySelector("#my_button_id").addEventListener("click", paste);


Actual results:

pasteText element is focused but nothing is pasted. Tried with input as well as textarea element. What can please be wrong?


Expected results:

clipboard content is pasted in pasteText element
Component: Untriaged → WebExtensions: Frontend
Product: Firefox → Toolkit
Just wanted to add that I tried to do this from inside my addon: http://tinyurl.com/at-your-command
I also struggled with this. What worked in my case was to (from a content script) create a temporary textarea element that I insert in the document, set the contentEditable attribute on this element to true, and perform the paste:

  var textarea = document.createElement("textarea");
  textarea.contentEditable = true;
  // insert textarea somewhere in your document
  textarea.focus();
  document.execCommand("paste");
  // retrieve the pasted text with textarea.textContent
  // remove textarea from the document

HTH
This is a known/documented issue.  Marking as duplicate of bug 1340578.
Status: UNCONFIRMED → RESOLVED
Closed: 7 years ago
Resolution: --- → DUPLICATE
(In reply to Nicolás González-Deleito from comment #2)
> I also struggled with this. What worked in my case was to (from a content
> script) create a temporary textarea element that I insert in the document,
> set the contentEditable attribute on this element to true, and perform the
> paste:
> 
>   var textarea = document.createElement("textarea");
>   textarea.contentEditable = true;
>   // insert textarea somewhere in your document
>   textarea.focus();
>   document.execCommand("paste");
>   // retrieve the pasted text with textarea.textContent
>   // remove textarea from the document
> 
> HTH

Thank you. This works.
Product: Toolkit → WebExtensions
You need to log in before you can comment on or make changes to this bug.