Sunday, September 16, 2007

Set Arbitrary Text To The Clipboard Using Javascript

You want to set a value to the clipboard, using javascript in a browser window.

And it needs to work in Internet Explorer and in Mozilla/FireFox.

You'd think it would be simple. It is, after all, the 21st century.

Think again.

The nearest to simple is a script by Krikkit. The very very short of it is that after over three hours of wasted time, I have a much simplified version of the script. Kudos to Krikkit for getting the script to the point he did - without it, I would have gotten nowhere. But this version is definitely superior because a) the comments are in English not Dutch :o) and b) it is very roughly only half the lines ('coz I found an alternative and much simpler way to accomplish the same thing).

So here's the code. It works in Internet Explorer and Mozilla/FireFox. Sorry - no Opera and I don't know about Safari.

function CopyStringToClipboard(s)
{
if (window.clipboardData)
{
/* Internet Explorer does things this way */
window.clipboardData.setData("Text", s);
}
else if (window.netscape)
{

try {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
} catch (ex) {window.alert("Mozilla is not configured to allow this script to set the clipboard data.");return;}

var ClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
ClipboardHelper.copyString(s);

}
}


However, life ain't quite that simple. FireFox will refuse to allow clipboard access (even just the humble lowly setting of clipboard data) unless the user makes a config change. Argh! That's ok-ish for techies like me who are making an app for internal use (as is the case right now), but if you're doing it for wider distribution...

Anyhows, the folks at Febooti have done a wonderful job of explaining exactly which setting needs to be changed and how.

Credits :

Krikkit's version - comments are in Dutch and it's much more complicated than necessary, but bravo, it does the job, and that's the main thing that matters, so definitely kudos in that direction

The broken Dutch version but with the comments having been translated from Dutch into English - and that's where I got the English comments I've used in my version, except that it also wasted me a slab of time because for no apparent reason, this version added square brackets around some numbers in the javascript, which caused it to not work, and it took a while to figure out what on earth was happening... Nonetheless, kudos for speaking both Dutch and English, or at very least, for taking initiative to pump the comments through an automated translator. :o)

... and thanks to the Mozilla Developer Centre where I finally found a much simpler way to accomplish the same thing. (But I still credit the Dutch version for part of the solution 'coz without it, I would never have known about that obscure "UniversalXPConnect" setting.)

That's all folks! Tune in next week for another episode of "And they told us computers would save time...". :o)

No comments: