Copy to clipboard with Javascript in Firefox

I really need a way to copy some text to the OS clipboard in Firefox.

Be aware that this is easy in IE and not possible in Chrome and Opera unless flash is used. Due to various reasons, I can not use the flash solution!

If it had worked in the past, but now netscape.security.PrivilegeManager.enablePrivilege is protected, as far as I can understand (since version 17).

It seems that everything is still possible according to this article:

https://developer.mozilla.org/en-US/docs/Using_the_Clipboard

Believe me, you still need to enable the feature in a user.js file like this

user_pref("capability.policy.policynames", "allowclipboard"); user_pref("capability.policy.allowclipboard.sites", "http://"); user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess"); 

But how do I do this? They did some tests without much success and thought that there was no guide on the Internet explaining how this should be done in a general way. For instance. A simple guide on how to include javascript access on the clipboard. Hopefully this is also a guide that a beginner can use. How to do this and publish it here, but first you need a working solution.

According to the network, there are 2 solutions for copying to the clipboard;

 document.execCommand("copy", false, null) 

and

 var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper); gClipboardHelper.copyString("Put me on the clipboard, please."); 

Both generate a crash on my first attempt.

The following solution requires the user to press CTRL + C, and I need a solution in which the text should be copied based on the click of a button (many on the same page).

stack overflow

My old solution was this:

 var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); if(clip) { var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); if(trans) { var str = new Object(); var len = new Object(); var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); if(str) { var clipid=Components.interfaces.nsIClipboard; if(clipid) { str.data = cliptext; trans.addDataFlavor('text/unicode'); trans.setTransferData("text/unicode", str, cliptext.length*2); clip.setData(trans, null, clipid.kGlobalClipboard); // No return value return 0; } } } } 

The components of .classes are undefined in unprivileged code (not adding, etc.), so I do not believe that any solution will work more with this. One option is to create an add-in that will run in the privileged area of ​​the code and send the text that must be copied to this add-on so that it processes the copy to the OS clipboard (a good new possible project).

This leaves document.execCommand ("copy", false, null) in the field as a standalone solution.

I tried this code and it does not copy anything to the OS clipboard - but it does not generate any btw errors.

 var pre = document.getElementById('pcryptcopytext'); if(!pre) { pre = document.createElement("pre"); pre.setAttribute('id', 'pcryptcopytext'); pre.setAttribute('style', 'opacity: 0; position: absolute; top: -10000px; right: 0;'); document.body.appendChild(pre); } pre.innerHTML = cliptext; pre.contentEditable = true; //pre.unselectable = "off"; //pre.focus(); if (document.createRange) { var rng = document.createRange(); rng.selectNodeContents(pre); document.execCommand("copy", false, null); document.body.removeChild(pre); } 

So does anyone have a working solution?

+6
source share
2 answers

Solved by creating a Firefox add-in that provides a clipboard object: https://github.com/myplaceonline/myplaceonline_ffclipboard

Example:

 if (window.ffclipboard) { window.ffclipboard.setText("clipboard text"); } 
+3
source

It seems like this is no longer supported and there is no replacement :(

https://support.mozilla.org/en-US/questions/977068#answer-500083

Maybe some noise in the Firefox error will help us get a (safe) solution.

+4
source

All Articles