搜尋 Mozilla 技術支援網站

防止技術支援詐騙。我們絕對不會要求您撥打電話或發送簡訊,或是提供個人資訊。請用「回報濫用」功能回報可疑的行為。

了解更多

I am in need for a js snippet for client side copy paste functionality in firefox without any flash plugins

more options

Right now I am using the below snippet for achieving copy paste functionality on the client site..

if (window.netscape) {

   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
   var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
   if (!clip) return;
   var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
   if (!trans) return;
   trans.addDataFlavor('text/unicode');
   var str = new Object();
   var len = new Object();
   var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
   var copytext = maintext;
   str.data = copytext;
   trans.setTransferData("text/unicode", str, copytext.length * 2);
   var clipid = Components.interfaces.nsIClipboard;
   if (!clip) return false;
   clip.setData(trans, null, clipid.kGlobalClipboard);

}

But recently came to notice that netscape.security.PrivilegeManager is no longer supported as a security concern. Is there any alternative approach to achieve this??

Thanks,

Right now I am using the below snippet for achieving copy paste functionality on the client site.. if (window.netscape) { netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); if (!clip) return; var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); if (!trans) return; trans.addDataFlavor('text/unicode'); var str = new Object(); var len = new Object(); var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); var copytext = maintext; str.data = copytext; trans.setTransferData("text/unicode", str, copytext.length * 2); var clipid = Components.interfaces.nsIClipboard; if (!clip) return false; clip.setData(trans, null, clipid.kGlobalClipboard); } But recently came to notice that netscape.security.PrivilegeManager is no longer supported as a security concern. Is there any alternative approach to achieve this?? Thanks,

所有回覆 (4)

more options

Hi

This sounds more like a web development query that an issue with the Firefox web browser.

I recommend you ask at Stack Overflow where one of the experts there should be able to help you.

more options

Hi ykonathala, what problem are you trying to solve?

Do you think users don't know how to use the Copy feature on their own and need you to do it for them?

What method are you using in Google Chrome? Perhaps Firefox has adopted a similar API by now.

more options

We have some fields on the multi-select box where I show the value with label and the value of that options will have a tokens and that should be copied to clip board, when I click on a copy button, other wise users cannot get that value associated to each option of that multi-select box. So to achieve this I am using the above pasted code in the onclick of the copy button , but it looks like firefox now removed this capability of copying to clipboard from the client site.

And here is the snippet that I used for chrome and its working great !!

else if (navigator.userAgent.toLowerCase().indexOf("chrome") != -1) {           
          var success   = true,
           range     = document.createRange(),
           selection;
           
var tmpElem = $('
');
           tmpElem.css({
             position: "absolute",
             left:     "-1000px",
             top:      "-1000px"
           });
          
           tmpElem.text(maintext);
           $("body").append(tmpElem);
           
           range.selectNodeContents(tmpElem.get(0));
           selection = window.getSelection ();
           selection.removeAllRanges ();
           selection.addRange (range);
           
           document.execCommand('copy');
          }

So with this I thought of looking if something new is available in the firefox to achieve this functionality.

Thanks,

more options

When you try the code you use for Chrome in Firefox, do you get an error on execCommand('copy')? According to the documentation, it should work if the document is in designMode: https://developer.mozilla.org/docs/Web/API/Document/execCommand