问题:
What is the best way to copy text to the clipboard? 将文本复制到剪贴板的最佳方法是什么? (multi-browser) (多浏览器)
I have tried: 我试过了:
function copyToClipboard(text) {
if (window.clipboardData) { // Internet Explorer
window.clipboardData.setData("Text", text);
} else {
unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
clipboardHelper.copyString(text);
}
}
but in Internet Explorer it gives a syntax error. 但是在Internet Explorer中,它会给出语法错误。 In Firefox, it says unsafeWindow is not defined
. 在Firefox中,它说unsafeWindow is not defined
。
A nice trick without flash: How does Trello access the user's clipboard? 一个没有闪光灯的好技巧: Trello如何访问用户的剪贴板?
解决方案:
参考一: https://stackoom.com/question/1g72/如何使用JavaScript复制到剪贴板参考二: https://oldbug.net/q/1g72/How-do-I-copy-to-the-clipboard-in-JavaScript
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/3210488