将文本复制到剪贴板的最佳方法是什么?(多浏览器)
我试过了:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
但在Internet Explorer中,它会出现语法错误.在Firefox中,它说unsafeWindow is not defined.
没有闪存的好技巧:Trello如何访问用户的剪贴板?
为什么navigator.clipboard总是undefined在以下代码段中?
var clipboard = navigator.clipboard;
if (clipboard == undefined) {
console.log('clipboard is undefined');
} else {
clipboard.writeText('stuff to write').then(function() {
console.log('Copied to clipboard successfully!');
}, function() {
console.error('Unable to write to clipboard. :-(');
});
}
Run Code Online (Sandbox Code Playgroud)
有关剪贴板API的更多信息,请访问此处.
Chrome版本:68.0.3440.106.
我确信这在某些方面有效,但不再是.这很令人困惑,因为这个表表明Clipboard API是在Chrome中实现的(已经有一段时间了),但是这个特定的API方法表表明API的所有方法都不受支持?