Closed
Bug 1151429
Opened 10 years ago
Closed 10 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)
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.
Reporter | ||
Comment 1•10 years ago
|
||
Ehsan also pointed me to this thread:
https://groups.google.com/forum/#!searchin/mozilla.dev.webapi/execcommand$20copy$20paste/mozilla.dev.webapi/Bvb-S152azI/APi3o1XoSsgJ
Comment 2•10 years ago
|
||
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
Updated•10 years ago
|
Whiteboard: [clipops]
Comment 3•10 years ago
|
||
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.
Updated•10 years ago
|
Whiteboard: [clipops] → [clipops] [parity:chrome] [parity:IE]
Comment 4•10 years ago
|
||
Hm.. I had apparently reported this as bug 1012662 already, sorry about that :-/
Updated•10 years ago
|
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → DUPLICATE
You need to log in
before you can comment on or make changes to this bug.
Description
•