![](/img/trans.png)
[英]Secret copy to clipboard JavaScript function in Chrome and Firefox?
[英]Copy to clipboard on firefox and google chrome
因為我環顧四周,找不到任何好的解決方案,可以將firefox或chrome上的文本復制到剪貼板。 但是,我已經嘗試了firefox在其開發者網站上提供的一些代碼,但仍然無法正常工作,並且有一個錯誤被拒絕。 這是我最后一分鍾嘗試的代碼。
var copytext = "Text to copy";
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
str.data = copytext;
有沒有人有一個很好的解決方案來處理這個問題? 我很感激你的分享。 謝謝。
我認為這不只是你的觀看?
如果沒有,您可以在Firefox瀏覽器中調整about:config中的設置。 在過濾器中查找“signed”,並將單個結果設置為DISABLED。
但是,如果你想要整個代碼的代碼,那就很好了,因為Firefox可以很好地保護它。 一個棘手的方法是使用Flash對象傳遞字符串,然后使用Flash復制到剪貼板:)
我找到了下一個解決方案:
在鍵向下處理程序上創建“pre”標記。 設置要復制到此標記的內容。 對此標記進行選擇並在處理程序中返回true。 此調用chrome的標准處理程序並復制所選文本。
如果你需要你可以設置超時功能恢復以前的選擇。 我對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. :(');
};
}
用法:
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;
在粘貼它創建textarea並且工作相同。
抱歉英語不好 - 不是我的母語。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.