在Firefox中使用JavaScript复制到剪贴板 [英] Copy to clipboard with Javascript in Firefox

查看:151
本文介绍了在Firefox中使用JavaScript复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



如果它在过去工作,但现在netscape.security.PrivilegeManager.enablePrivilege被保护,直到我(自17版以来)。



看起来好像依然可以根据这篇文章:

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

p>

相信仍然有必要在user.js文件中启用这样的可能性

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

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



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

  document.execCommand(copy,false,null)



  var gClipboardHelper = Components.classes [@ mozilla.org/widget/clipboardhelper; 。1\" ]的getService(Components.interfaces.nsIClipboardHelper); 
gClipboardHelper.copyString(请把我放在剪贴板上);

两者都会在第一次尝试时产生错误。



下面的解决方案需要用户按CTRL + C,我需要一个解决方案,文本应该按下一个按钮(很多在一个单一的页面)复制。

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



我以前的解决方案是这样的: / p>

  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); //无返回值
返回0;



$ b code
$ b

元件.class在未经授权的代码中是未定义的(不是附加的等),所以我不相信任何解决方法,这将工作更多。一种方法是制作一个在特权代码区执行的插件,并将复制的文本发送给该插件,以便将其复制到操作系统剪贴板(新的可能项目)。



这个只留下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);



$ b $ p
$ b $ p


$ b $

解决方案

通过创建一个暴露剪贴板对象的Firefox插件来解决:
<$>

p $ p> if(window.ffclipboard){
window.ffclipboard.setText(clipboard text);
}


I really need a way to copy some text to the OS clipboard in 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!

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).

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

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_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.

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).

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).

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

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?

解决方案

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

Example:

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

这篇关于在Firefox中使用JavaScript复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆