在Firefox中使用Javascript复制到剪贴板
我真的需要一种在Firefox中将一些文本复制到操作系统剪贴板的方法 知道这在IE中很容易,在Chrome和Opera中不可能,除非使用flash。由于不同的原因,我无法使用闪存解决方案 在过去它可以工作,但现在netscape.security.privilegmanager.enablePrivilege就我所知得到了保护(从第17版开始) 根据这篇文章,似乎仍然有可能: 相信仍然有必要在user.js文件中启用这种可能性,如下所示在Firefox中使用Javascript复制到剪贴板,javascript,firefox,clipboard,Javascript,Firefox,Clipboard,我真的需要一种在Firefox中将一些文本复制到操作系统剪贴板的方法 知道这在IE中很容易,在Chrome和Opera中不可能,除非使用flash。由于不同的原因,我无法使用闪存解决方案 在过去它可以工作,但现在netscape.security.privilegmanager.enablePrivilege就我所知得到了保护(从第17版开始) 根据这篇文章,似乎仍然有可能: 相信仍然有必要在user.js文件中启用这种可能性,如下所示 user_pref("capability.policy
user_pref("capability.policy.policynames", "allowclipboard");
user_pref("capability.policy.allowclipboard.sites", "http://");
user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess");
但是我该怎么做呢?我做了一些测试,但没有取得很大成功,并且认为网络上没有任何指南可以解释如何以通用方式进行测试。例如,关于如何启用对剪贴板的javascript访问的简单指南。希望这也是一个新手用户可以使用的指南。喜欢这样做,并张贴在这里,但需要一个工作的解决方案第一
根据web,有两种拷贝到剪贴板的解决方案
document.execCommand("copy", false, null)
及
两者都会在我第一次尝试时产生失败
下面的解决方案需要用户按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);
}
那么,有人找到了有效的解决方案吗?看起来这不再受支持,也没有替代方案:(
也许在Firefox bug中制造一些噪音会帮助我们获得一个(安全的)解决方案。通过创建一个暴露剪贴板对象的Firefox加载项来解决: 例如:
if (window.ffclipboard) {
window.ffclipboard.setText("clipboard text");
}
你只需要给Firefox你的数据,告诉它你想用它做什么
navigator.clipboard.writeText(“要复制的文本”)。然后(()=>{
//论成功
},(e)=>{
//论错误
});
文件。执行命令(“副本”);
非常感谢您的链接。我花了一些时间尝试过时的解决方案,直到找到您的答案。可以从Mozilla.org转到此处将该插件安装到Firefox中:它不需要重新启动。基于Flash的解决方案(如zeroclipboard)需要用户单击隐藏的Flash影片,因此如果您有复杂的UI需求,实现起来很困难。使用这个解决方案,当用户单击任何东西时,您只需调用两行代码。
if (window.ffclipboard) {
window.ffclipboard.setText("clipboard text");
}