1

Subject:

I'm in the process of creating a chrome extension and, for certain features, I would like to copy some text to the clipboard and automatically paste it into whatever element has the focus for the user.

Getting my text into the clipboard is no problem. I can simply create a textarea in my background page, set its value accordingly and then select it's contents. Then, I can use document.execCommand("copy");


Problem:

The problem comes when I try to use document.execCommand('paste') in my content script. It works fine on simple text areas (like the one I'm typing in now). However, on many sites, it tends not to work. This typically happens when the editable element is inside an Iframe, or is actually a custom <div> rather than a vanilla <textarea>/<input>

Even though my trivial attempt fails to work in these cases, the built-in paste option that is provided by Google, works every time without fail.

Is it possible for a chrome extension to mimic this functionality in a customized context menu option? If so, how can this functionality be achieved?


Additional Info:

This operation is invoked when a context menu option is clicked. Said context menu option is only visible when the element currently in focus is categorized as editable by the chrome.contextMenus API


Similar Questions:

None of these provided me with a satisfactory answer

Community
  • 1
  • 1
Luke
  • 5,567
  • 4
  • 37
  • 66
  • Not sure why you would use copy-paste - it's more of a user mechanism than a coding one. The more common solution is to read the value you need into a variable, message it to the content scripts, which then inject it into the input field. – Steve Campbell Feb 12 '15 at 05:24

0 Answers0