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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21696052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:29:35  来源:igfitidea点击:

Copy to clipboard with Javascript in Firefox

javascriptfirefoxclipboard

提问by Beast

I really need a way to copy some text to the OS clipboard in Firefox.

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

Know it is easy in IE and not possible in Chrome and Opera unless flash is used. Because of different reasons I am unable to use the flash solution!

知道这在 IE 中很容易,在 Chrome 和 Opera 中不可能,除非使用 Flash。由于不同的原因我无法使用闪存解决方案!

Had it working in the past but now the netscape.security.PrivilegeManager.enablePrivilege is protected as far as I can figure out (since ver. 17).

它在过去工作,但现在 netscape.security.PrivilegeManager.enablePrivilege 受到保护,据我所知(自 17 版起)。

It looks as if it is still possible according to this article:

根据这篇文章,看起来似乎仍然是可能的:

https://developer.mozilla.org/en-US/docs/Using_the_Clipboard

https://developer.mozilla.org/en-US/docs/Using_the_Clipboard

Believe it is still necessary to enable the possibility in the user.js file like this

相信还是有必要像这样在user.js文件中开启这种可能性

user_pref("capability.policy.policynames", "allowclipboard");
user_pref("capability.policy.allowclipboard.sites", "http://");
user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess"); 

But how shall I do it? Have made some test without great success and think there is no guide on the web that explain how it shall be done in a generic way. E.g. a simple guide about how to enable javascript access to the clipboard. Hopefully also a guide that can be used by the novice user. Like to do it and post it here but need a working solution first.

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

According to the web there are 2 solutions for copy to clipboard;

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

document.execCommand("copy", false, null) 

and

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

Both generate a failure with my first try.

我的第一次尝试都失败了。

The solution below need the user to press CTRL+C and I need a solution where the text shall copy based on the press of a button (many on a single page).

下面的解决方案需要用户按 CTRL+C,而我需要一个解决方案,其中文本应根据按钮(许多在单个页面上)的按下进行复制。

https://stackoverflow.com/questions/4344325/copy-to-clipboard-on-firefox-and-google-chrome/11346026#11346026

https://stackoverflow.com/questions/4344325/copy-to-clipboard-on-firefox-and-google-chrome/11346026#11346026

My old solution was like this:

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

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 is undefined in unprivileged code (not add-on etc) so I do not believe any solution with this will work any more. One option is to make an add-on that will execute in privileged code area and send the text that shall be copied to this add-on for it to handle the copy to the OS clipboard (nice new possible project).

Components.classes 在非特权代码(不是附加组件等)中是未定义的,所以我认为任何解决方案都不会再起作用。一种选择是制作一个将在特权代码区域中执行的附加组件,并发送应复制到此附加组件的文本,以便它处理复制到操作系统剪贴板(不错的新项目)。

This only leave document.execCommand("copy", false, null) in the field as a stand alone solution.

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

Tried this code and it does not copy anything to the OS clipboard - but do not generate any errors 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);
}

So, anybody got a working solution?

那么,有人有一个可行的解决方案吗?

采纳答案by Kevin

Solved by creating a Firefox Add-on that exposes the clipboard object: https://github.com/myplaceonline/myplaceonline_ffclipboard

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

Example:

例子:

if (window.ffclipboard) {
  window.ffclipboard.setText("clipboard text");
}

回答by nh2

Looks like this is not supported any more, and there is no replacement :(

看起来这不再受支持,并且没有替代品:(

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

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

Maybe making some noise in a Firefox bug will help us get a (safe) solution.

也许在 Firefox 错误中制造一些噪音会帮助我们获得(安全的)解决方案。