![](/img/trans.png)
[英]Copy to clipboard pure javascript function in mozzila firefox
[英]Copy to clipboard with Javascript in Firefox
我真的需要一種將一些文本復制到 Firefox 中的操作系統剪貼板的方法。
要知道這在 IE 中很容易,而在 Chrome 和 Opera 中是不可能的,除非使用 Flash。 由於不同的原因我無法使用flash解決方案!
如果它在過去工作,但現在 netscape.security.PrivilegeManager.enablePrivilege 受到保護,據我所知(從第 17 版開始)。
根據這篇文章似乎仍然可以:
https://developer.mozilla.org/en-US/docs/Using_the_Clipboard
相信還是有必要像這樣在user.js文件中啟用可能性
user_pref("capability.policy.policynames", "allowclipboard");
user_pref("capability.policy.allowclipboard.sites", "http://");
user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess");
但是我該怎么做呢? 進行了一些測試但沒有取得巨大成功,並認為網絡上沒有指南可以解釋如何以通用方式完成。 例如,關於如何啟用對剪貼板的 javascript 訪問的簡單指南。 希望也是一個可以被新手用戶使用的指南。 喜歡這樣做並在此處發布,但首先需要一個可行的解決方案。
根據網絡,有兩種復制到剪貼板的解決方案;
document.execCommand("copy", false, null)
和
var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
gClipboardHelper.copyString("Put me on the clipboard, please.");
我的第一次嘗試都失敗了。
下面的解決方案需要用戶按 CTRL+C,我需要一個解決方案,其中文本應基於按下按鈕(許多在單個頁面上)進行復制。
我的舊解決方案是這樣的:
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;
}
}
}
}
Components.classes 在非特權代碼(不是附加組件等)中未定義,因此我認為任何解決方案都不會再起作用。 一種選擇是制作一個將在特權代碼區域中執行的附加組件,並將應復制到此附加組件的文本發送到它以處理復制到操作系統剪貼板(不錯的新可能項目)。
這只會在字段中留下 document.execCommand("copy", false, null) 作為獨立的解決方案。
嘗試了此代碼,它不會將任何內容復制到操作系統剪貼板 - 但不會產生任何錯誤順便說一句。
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);
}
那么,有人有可行的解決方案嗎?
似乎不再支持此功能,並且沒有替代品:(
https://support.mozilla.org/en-US/questions/977068#answer-500083
也許在 Firefox 錯誤中制造一些噪音會幫助我們獲得(安全)解決方案。
通過創建一個暴露剪貼板對象的 Firefox 插件來解決: https ://github.com/myplaceonline/myplaceonline_ffclipboard
例子:
if (window.ffclipboard) {
window.ffclipboard.setText("clipboard text");
}
您可以使用 Firefox navigator
器對象
navigator.clipboard.writeText("text you want to copy").then(() => {
// on success
}, (e) => {
// on error
});
document.execCommand("copy");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.