Join the AMA (Ask Me Anything) with the Firefox leadership team to celebrate Firefox 20th anniversary and discuss Firefox’s future on Mozilla Connect. Mark your calendar on Thursday, November 14, 18:00 - 20:00 UTC!

Този сайт ще има ограничена функционалност, докато се извършва тече неговата поддръжка. Ако дадена статия не може реши проблема ви и искате да зададете въпрос, нашата общност е готова да ви помогне на @firefox в Twitter и /r/firefox в Reddit.

Търсене в помощните статии

Избягвайте измамите при поддръжката. Никога няма да ви помолим да се обадите или изпратите SMS на телефонен номер или да споделите лична информация. Моля, докладвайте подозрителна активност на "Докладване за злоупотреба".

Научете повече

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