简体   繁体   English

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

[英]Copy to clipboard with Javascript in Firefox

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.要知道这在 IE 中很容易,而在 Chrome 和 Opera 中是不可能的,除非使用 Flash。 Because of different reasons I am unable to use the flash solution!由于不同的原因我无法使用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_Clipboardhttps://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.进行了一些测试但没有取得巨大成功,并认为网络上没有指南可以解释如何以通用方式完成。 Eg a simple guide about how to enable javascript access to the clipboard.例如,关于如何启用对剪贴板的 javascript 访问的简单指南。 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.喜欢这样做并在此处发布,但首先需要一个可行的解决方案。

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

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?那么,有人有可行的解决方案吗?

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 错误中制造一些噪音会帮助我们获得(安全)解决方案。

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");
}

You can use firefox navigator object您可以使用 Firefox navigator器对象

navigator.clipboard.writeText("text you want to copy").then(() => {
    // on success
}, (e) => {
    // on error
});
document.execCommand("copy");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM