Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] Copy To Clipboard (in FireFox)

Started by Rob Loach Nov 10, 2004 at 9:30 PM 10 replies 78.2k views
Original Post
Rob Loach
Rob Loach
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: User IP Anyone know how to make it FireFox-compatible? Example: User IP <-- Works in IE but not FireFox
Rob Loach [Website] [Projects] [
evolutional
evolutional
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?
Rob Loach
Rob Loach
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] [
Sander
Sander
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!
markr
markr
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
evolutional
evolutional
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.
Rob Loach
Rob Loach
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] [
paulw
paulw
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
Sander
Sander
Check the dates before posting please. This thread died seven and a half months ago.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.