在Firefox中使用Javascript复制到剪贴板

Bea*_*ast 8 javascript clipboard firefox

我真的需要一种方法将一些文本复制到Firefox中的操作系统剪贴板中.

知道它在IE中很容易,除非使用闪存,否则Chrome和Opera无法实现.由于不同的原因,我无法使用闪存解决方案!

如果它在过去工作但现在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"); 
Run Code Online (Sandbox Code Playgroud)

但是我该怎么做呢?已经做了一些测试而没有取得很大成功,并且认为网上没有指南可以解释如何以通用方式完成.例如,关于如何启用javascript访问剪贴板的简单指南.希望也是新手用户可以使用的指南.喜欢这样做并在此发布,但首先需要一个有效的解决方案.

根据网络,有2个复制到剪贴板的解决方案;

document.execCommand("copy", false, null) 
Run Code Online (Sandbox Code Playgroud)

var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
  gClipboardHelper.copyString("Put me on the clipboard, please.");
Run Code Online (Sandbox Code Playgroud)

我的第一次尝试都会导致失败.

下面的解决方案需要用户按CTRL + C,我需要一个解决方案,文本将根据按下按钮复制(单页上很多).

/sf/ask/304102781/#11346026

我的旧解决方案是这样的:

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;
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在非特权代码(不是附加组件等)中未定义Components.classes,所以我不相信任何解决方案都可以使用.一种选择是创建一个将在特权代码区域中执行的附加组件,并将要复制的文本发送到此附加组件,以便将副本处理到OS剪贴板(很可能是新的可能项目).

这只会将document.execCommand("copy",false,null)作为独立解决方案留在字段中.

尝试了这个代码并且它不会将任何内容复制到操作系统剪贴板 - 但不会产生任何错误btw.

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);
}
Run Code Online (Sandbox Code Playgroud)

那么,任何人都有一个有效的解决方案?

nh2*_*nh2 6

看起来不再支持此功能,并且没有替代品:(

https://support.mozilla.org/en-US/questions/977068#answer-500083

也许在Firefox中发出一些噪音可以帮助我们获得(安全)解决方案.


Kev*_*vin 4

通过创建一个公开剪贴板对象的 Firefox 插件来解决:https://github.com/myplaceonline/myplaceonline_ffclipboard

例子:

if (window.ffclipboard) {
  window.ffclipboard.setText("clipboard text");
}
Run Code Online (Sandbox Code Playgroud)