Closed Bug 1151429 Opened 9 years ago Closed 9 years ago

Allow JS to copy text to the clipboard by triggering document.execCommand('copy') or document.execCommand('cut') in user-initiated thread

Categories

(Core :: DOM: Editor, defect)

x86_64
Linux
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 1012662

People

(Reporter: armenzg, Unassigned)

References

()

Details

(Whiteboard: [clipops] [parity:chrome] [parity:IE])

Currently in bugzilla, we have to add Flash to allow clicking on a button and putting the bug summary into the clipboard.

If we want to move the web forward (and bugzilla) we should fix this by providing a solution with JS.
Per spec, what you need is now possible (though in a slightly convoluted way) - this is how you do it:

document.addEventListener('copy', function(e){
  e.clipboardData.setData('text/plain', 'foo');
  e.preventDefault(); // default behaviour is to copy any selected text
}
element.onclick = function(){
  document.execCommand('copy'); // only works in click handler or other user-triggered thread
}

Clicking element should now place the string 'foo' on the clipboard. The underlying security logic depends on the "which JS threads are allowed to show popup windows?" rules - see http://dev.w3.org/2006/webapi/clipops/clipops.html#events-that-are-allowed-to-modify-the-clipboard .

However, this functionality isn't yet implemented in Firefox - I'm not sure if any browsers support it just yet. In Firefox it's particularly tricky I believe because of interaction with key/menu handling and selection code - if there is no selection, Firefox doesn't let you trigger copy actions at all I think, disabling both the menu entry and the shortcut key. I'd like to have support for the onbeforecopy - type events instead.

The spec is probably at a stage where some actual implementation effort and experience would be great (and feasible) after my edits in December.
Summary: Allow JS to copy text to the clipboard → Allow JS to copy text to the clipboard by triggering document.execCommand('copy') or document.execCommand('cut') in user-initiated thread
Whiteboard: [clipops]
Chrome has recently enabled this functionality. I haven’t yet figured out how to make it work on contents of form fields like <textarea>, but in Chrome 42 (currently beta), it is now possible to select text on a web page and copy it to the clipboard via document.execCommand('copy') from within a user-initiated event handler.

Source w/ demo: https://www.chromestatus.com/feature/5223997243392000

Also note that this functionality is available in Internet Explorer as well.
Whiteboard: [clipops] → [clipops] [parity:chrome] [parity:IE]
Hm.. I had apparently reported this as bug 1012662 already, sorry about that :-/
Status: NEW → RESOLVED
Closed: 9 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.