Sign in to follow this  
Rob Loach

[web] Copy To Clipboard (in FireFox)

Recommended Posts

Rob Loach    1504
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

Share this post


Link to post
Share on other sites
evolutional    1393
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?

Share this post


Link to post
Share on other sites
Rob Loach    1504
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.

Share this post


Link to post
Share on other sites
Sander    1332
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!

Share this post


Link to post
Share on other sites
markr    1692
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

Share this post


Link to post
Share on other sites
evolutional    1393
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.

Share this post


Link to post
Share on other sites
Rob Loach    1504
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.

Share this post


Link to post
Share on other sites
Rob Loach    1504
I have settled with displaying it in a prompt box so that it works in every browser:

EXAMPLE

<a href="javascript:void(prompt('The following is the IP of USERNAME:', 'xxx.xxx.xxx.xxx'))" title="User IP: xxx.xxx.xxx.xxx">IP</a>

Share this post


Link to post
Share on other sites
paulw    122
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Are you serious ?
Would you like to see parts of you private life or business being sent on an unknown web server while surfing ?
Sharing your clipboard with the Internet IS a security issue... Especially when you are not aware of it.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this