[web] Copy To Clipboard (in FireFox)

Started by
10 comments, last by Sander 17 years, 11 months ago
In RWeb's comment system, I have it display a little admin menu for each comment. Inside it are little labels (x, m, ip, etc). When you hover your mouse over ip, it displays the user's IP address. I made it so that when you click it, it copies the IP to the clipboard. This works great in IE, but sadly doesn't work in FireFox. Anyone have an alternative? Here's an example of the code I'm currently using: <a href="#" title="User IP: xxx.xxx.xx.xx - Click to copy to clipboard" onclick="window.clipboardData.setData('Text', 'xxx.xxx.xx.xx'); return false;">User IP</a> Anyone know how to make it FireFox-compatible? Example: User IP <-- Works in IE but not FireFox
Rob Loach [Website] [Projects] [Contact]
Advertisement
Original Source Link
<script language="javascript" type="text/javascript"><!--function copy_clip(meintext){ if (window.clipboardData)    {      // the IE-manier   window.clipboardData.setData("Text", meintext);      // waarschijnlijk niet de beste manier om Moz/NS te detecteren;   // het is mij echter onbekend vanaf welke versie dit precies werkt:   }   else if (window.netscape)    {       // dit is belangrijk maar staat nergens duidelijk vermeld:   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');      // maak een interface naar het clipboard   var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);   if (!clip) return;      // maak een transferable   var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);   if (!trans) return;      // specificeer wat voor soort data we op willen halen; text in dit geval   trans.addDataFlavor('text/unicode');      // om de data uit de transferable te halen hebben we 2 nieuwe objecten nodig   om het in op te slaan   var str = new Object();   var len = new Object();      var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);      var copytext=meintext;      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);      }   alert("Following info was copied to your clipboard:\n\n" + meintext);   return false;}//--></script>      



I've not tested it - and the comments are in Dutch, but it should be fairly self-explanatory?
Hrmmm... Can't get it working, I'll just ponder some alternatives to copying it to the clipboard. Maybe even send a note to FireFox. We'll see.
Rob Loach [Website] [Projects] [Contact]
There's probabely a slight difference between the old netscape way and the new Mozilla way, although they should be similar. Anyway, I translated the piece of code to English. Maybe it'll help:

<script language="javascript" type="text/javascript"><!--function copy_clip(meintext){ if (window.clipboardData)    {      // the IE-way   window.clipboardData.setData("Text", meintext);      // Probabely not the best way to detect netscape/mozilla.   // I am unsure from what version this is supported   }   else if (window.netscape)    {       // This is importent but it's not noted anywhere   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');      // create interface to the clipboard   var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);   if (!clip) return;      // create a transferable   var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);   if (!trans) return;      // specify the data we wish to handle. Plaintext in this case.   trans.addDataFlavor('text/unicode');      // To get the data from the transferable we need two new objects   var str = new Object();   var len = new Object();      var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);      var copytext=meintext;      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);      }   alert("Following info was copied to your clipboard:\n\n" + meintext);   return false;}//--></script>


My guess is that the "else if (window.netscape)" is wrong. That's clearly the wrong way to scan for mozilla/firefox. Look up a better UA detection and then try the code again. The easiest and best way to scan for browsertype would be by using MS propietry code, e.g.
function copy_clip(text){  <!--[if IE]>    window.clipboardData.setData("Text", text);    return;  <![endif]-->    //do Mozilla or Cross-Browser clipboard copy here.}

It validates on the W3C and works 100%. Sure, it's MS code but hey, it works!

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

I'm not sure that browsers should generally allow scripts to access the clipboard.

I presume there *is* a way of doing it in Mozilla similar to the above that involves asking the user for extra privileges to do stuff that you're normally not allowed to.

Mark
I must admit, clipboard access scares me slightly. What's to stop a website from inserting the contents of your clipboard into a hidden form element? I know the likelihood of having something critical in there is low, but you never know.
Quote:Original post by Sander
window.clipboardData.setData("Text", text);

That's what I'm using right now. Works great in IE. Thanks for translating that code. I'll read through it and see if I could hack around the Mozilla code. I could also just have the IP shown up in a seperate page as well.
Rob Loach [Website] [Projects] [Contact]
I have settled with displaying it in a prompt box so that it works in every browser:

EXAMPLE

<a href="&#106avascript:void(prompt('The following is the IP of USERNAME:', 'xxx.xxx.xxx.xxx'))" title="User IP: xxx.xxx.xxx.xxx">IP</a>
Rob Loach [Website] [Projects] [Contact]
They won't allow you to copy stuff to and from the clipboard in the new versions of mozilla.
http://www.mozilla.org/projects/security/known-vulnerabilities.html
Quote:I must admit, clipboard access scares me slightly...
The clipboard was invented as a paradigm-independent way for applications to share metadata. Who cares if you can read/write from it? There would be no way of determining from a malicious application what the read data applies to anyway. There are plenty of other scenarios that offer the same interoperability.

Paul W.

Assisted Solutions
Data Research Group

This topic is closed to new replies.

Advertisement