Copy to clipboard on Firefox and Google Chrome

since I looked around and did not find anything good for copying text in firefox or chrome to the clipboard. However, I tried some codes provided by firefox on my developer's site, but it still doesn’t work, and there was one fix with permission allowed. Here is the code I tried at the last minute.

var copytext = "Text to copy"; var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); str.data = copytext; 

Does anyone have a good solution to solve this problem? I would appreciate your exchange. Thanks.

+4
source share
2 answers

I found the following solution:

In the manual manipulator create a tag "pre". Set the content to copy to this tag. Make a selection in this tag and return true in the handler. This standard standard chrome handler and copied selected text.

And if you need u, a timeout can be set for the function to restore the previous selection. My implementations on Mootools:

  function EnybyClipboard() { this.saveSelection = false; this.callback = false; this.pastedText = false; this.restoreSelection = function () { if (this.saveSelection) { window.getSelection().removeAllRanges(); for (var i = 0; i < this.saveSelection.length; i++) { window.getSelection().addRange(this.saveSelection[i]); } this.saveSelection = false; } }; this.copyText = function (text) { var div = $('special_copy'); if (!div) { div = new Element('pre', {'id' : 'special_copy', 'style': 'opacity: 0;position: absolute;top: -10000px;right: 0;'}); div.injectInside(document.body); } div.set('text', text); if (document.createRange) { var rng = document.createRange(); rng.selectNodeContents(div); this.saveSelection = []; var selection = window.getSelection(); for (var i = 0; i < selection.rangeCount; i++) { this.saveSelection[i] = selection.getRangeAt(i); } window.getSelection().removeAllRanges(); window.getSelection().addRange(rng); setTimeout(this.restoreSelection.bind(this), 100); } else return alert('Copy not work. :('); }; this.getPastedText = function () { if (!this.pastedText) alert('Nothing to paste. :('); return this.pastedText; }; this.pasteText = function (callback) { var div = $('special_paste'); if (!div) { div = new Element('textarea', {'id' : 'special_paste', 'style': 'opacity: 0;position: absolute;top: -10000px;right: 0;'}); div.injectInside(document.body); div.addEvent('keyup', function() { if (this.callback) { this.pastedText = $('special_paste').get('value'); this.callback.call(this.pastedText); this.callback = false; this.pastedText = false; setTimeout(this.restoreSelection.bind(this), 100); } }.bind(this)); } div.set('value', ''); if (document.createRange) { var rng = document.createRange(); rng.selectNodeContents(div); this.saveSelection = []; var selection = window.getSelection(); for (var i = 0; i < selection.rangeCount; i++) { this.saveSelection[i] = selection.getRangeAt(i); } window.getSelection().removeAllRanges(); window.getSelection().addRange(rng); div.focus(); this.callback = callback; } else return alert('Fail to paste. :('); }; } 

using:

 enyby_clip = new EnybyClipboard(); //init enyby_clip.copyText('some_text'); // place this in CTRL+C handler and return true; enyby_clip.pasteText(function callback(pasted_text) { alert(pasted_text); }); // place this in CTRL+V handler and return true; 

Paste it to create a text box and work the same way.

Sorry for the poor English - not my native language.

+1
source

I perceive it not only for viewing?

If not, you can configure the parameter approximately: config in your Firefox browser. Look at the “signed” in the filter and set the only result to the “DISABLED” state.

However, if you need code for all this, it is misleading, since Firefox is reasonably well protected from this. One tricky way is to use the Flash object to transfer the string, and then use Flash to copy to the clipboard :)

+4
source

All Articles