admin管理员组文章数量:1321441
since I have looked around and could not find any nice solution for copying text on firefox or chrome to clipboard. However, I have tried some codes provide by firefox in its developer site, but still not work and there was one errror with permission denied. Here is the code I tried the last minute.
var copytext = "Text to copy";
var str = Components.classes["@mozilla/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
str.data = copytext;
Does anyone have a good solution to deal with this? I would appreciate for your sharing. Thanks.
since I have looked around and could not find any nice solution for copying text on firefox or chrome to clipboard. However, I have tried some codes provide by firefox in its developer site, but still not work and there was one errror with permission denied. Here is the code I tried the last minute.
var copytext = "Text to copy";
var str = Components.classes["@mozilla/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
str.data = copytext;
Does anyone have a good solution to deal with this? I would appreciate for your sharing. Thanks.
Share Improve this question edited Dec 9, 2010 at 11:08 Mark Mayo 12.6k12 gold badges56 silver badges88 bronze badges asked Dec 3, 2010 at 9:52 SinalSinal 1,1655 gold badges17 silver badges37 bronze badges 1- 1 Maybe this feature (setting the clipboard to an arbitrary JavaScript string) has been disabled in these browsers for security reasons. – pts Commented Dec 3, 2010 at 9:57
2 Answers
Reset to default 4I take it it's for more than just you viewing?
If not, you can adjust the setting in about:config in your Firefox browser. Look for 'signed' in the filter, and set the single result to DISABLED.
However, if you want code for the whole thing, it's tricker as Firefox is quite well secured against that. One tricky way is to use a Flash object to pass the string to, and then use Flash to copy to the clipboard :)
I am found next solution:
On key down handler create "pre" tag. Set content to copy to this tag. Make Selection on this tag and return true in handler. This call standard handler of chrome and copied selected text.
And if u need u may be set timeout for function for restoring previous selection. My implementantions 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. :(');
};
}
usage:
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;
On paste its create textarea and work same.
Sorry for bad English - not my native language.
本文标签: javascriptCopy to clipboard on firefox and google chromeStack Overflow
版权声明:本文标题:javascript - Copy to clipboard on firefox and google chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742101176a2420807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论