How to Copy to Clipboard in Firefox and other browsers other than IE?

Status
Not open for further replies.

debiprasad_sahoo

Web Junky 2.0
Dear friends, I need to copy some contents to clipboard on the click event of a button. For IE the js code is as follow
Code:
window.clipboardData.setData('Text', 'the text you need to copy');

For firefox there is some complex code, which can copy to clipboard, but it is not working correctly. It shows this error
uncaught exception: A script from "*loveorkut.com/" was denied UniversalXPConnect Privilages.
The js code is as follow
Code:
        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 = "The Text you want to copy";
       
        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);
However this code works in local, it asks for a confirmation only to give access to the clipboard. I need this to be worked in Internet also. Any other good code will be better. Please help.

If you have any questions or ideas, then please discuss here.
 

QwertyManiac

Commander in Chief
Mozilla doesn't allow copying to clipboard unless the script is signed (Digital signatures, from Verizon, etc.). And even if its signed it asks for user permission before allowing the script to do so.

But there is one hack, it uses a flash object to perform the same task and doesn't require permissions, etc. You may use it from here: *www.jeffothy.com/weblog/clipboard-copy/

(P.s. There is a setting in About:Config which allows script to copy to clipboard, but it makes no sense asking a user to change all those defaults, etc.)

Or go the Mozilla's ugly and long way: *developer.mozilla.org/en/docs/Using_the_Clipboard
 

Lucky_star

Still Shining!
There is this flash component which allows copying to clipboard from any browsers. Its not designed by me. But I had used it once and its working in almost all browsers I have tested. I am not getting the exact source of this file. But its released under the GPL, so freely distributable.

Download this file: *rapidshare.com/files/81866988/_clipboard.swf.html
Keep it in the same folder.

Here is the code:


Code:
<script type="text/javascript">
function copy(text2copy) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
    }
}

</script>
Now pass any parameters to the function copy(parameters) and it will be copied to the clipboard instantly.

Like this:
Code:
<input type="button" onClick="copy('ThinkDigit Forum')">
This will copy Thinkdigit Forum to the Clipboard.
Tell me if you have any problems.
 
Status
Not open for further replies.
Top Bottom