harriyott.com

Wednesday, January 12, 2005

JavaScript clipboard control

I'm really not sure I like what I've just found. I noticed in the Visual Studio help that there is a "copy code" link, which eventually calls:

window.clipboardData.setData("Text", "some text for the clipboard");

That's fine from a clearly labelled link, but it also works from a <script> block, and in the IE address bar. I'm almost tempted to add it to this page, so you can try it, but I wouldn't want to replace whatever's in your clipboard without warning. There's plenty of people that would though.

Guess what the getData function does? It grabs what is already in the clipboard. Again, this can be run in a script block, which will happen when the page loads. This can then easily be posted to a server. I wonder how many times that has happened to me. You can see it in action by copying some text, and clicking the link. I won't post it anywhere.

Show clipboard contents

Scary huh?

[Update: more details on my later post]

15 Comments:

Anonymous Anonymous said...

works only in ie. firefox doesn't seem to do anything ;)

January 20, 2005 11:49 AM  
Blogger Simon said...

True. About the only time I'm happy that my code doesn't work cross-browser!

January 20, 2005 12:22 PM  
Anonymous Anonymous said...

Another "New Feature" from the Big M.... (Microsoft)
I tried it on the Fox, and There is no security BUG like that !!!
It's working only for IE, and it's bad !!! it's really really bad !!! as users using Copy&Paste all day long, and you can get a lot of Secret information like that !!!!

Password,
Credit Card Numbers,
Emails,
and more !!!!!!

Be carefull using Microsoft's Browser (IE) !!!!!
Gal.

January 20, 2005 1:37 PM  
Anonymous Anonymous said...

In IE:

Tools -> Internet Options, Security.

In the appropriate Custom Level section(s):
Scripting, Allow paste operations via script option
Set this to "Disable" or "Prompt".

January 20, 2005 6:09 PM  
Anonymous Anonymous said...

Before people get too rabid in their IE is evil rants, this is *disabled* by default for the internet zone in XP SP2... It is only enabled for the local-intranet and trusted zones.

January 20, 2005 9:47 PM  
Blogger Simon said...

That's interesting. I'll check my IE settings. You're right, it's not good to jump to conclusions.

January 20, 2005 10:46 PM  
Anonymous Anonymous said...

I have XP SP 2 loaded on a couple of my machines. Your link shows the clipboard contents when using both.

January 20, 2005 11:23 PM  
Blogger Simon said...

The plot thickens. Mine says "prompt" on the settings at home (where I am now), and does ask me before showing the contents. At work (where I discovered it), the setting must be "enable". Actually, it might not be, because I found it in Visual Studio help, which would run with local or intranet permissions. I'll check tomorrow.

January 20, 2005 11:32 PM  
Blogger umesh said...

tested with many browsers only works on IE and other similar IE Basted browsers like Maxthon / MyIE2 Doesnt work with firfox,netscape,etc..

January 31, 2005 6:45 AM  
Blogger Simon said...

Thanks. Someone else told me that it was introduced in IE5.

January 31, 2005 8:56 PM  
Anonymous lognar said...

thank you

you have helped me with this code

March 19, 2006 1:55 PM  
Anonymous Anonymous said...

I don't like how people are going to try and use this as something against IE. Firefox has one bug I'll never get over... a major memory leak. Everytime I use it (on my clean, brand new Dell laptop) I notice that every tab I open takes up 10 MBs of processing memory! I'm not the only one to complain of this either. I would say that javascript cutting and pasting is the least of my worries compared to 10 MBs of ram being eaten up.

July 05, 2007 10:13 PM  
Anonymous Anonymous said...

This is not really a huge flaw in the browser, copy and paste is possible across browsers and even between browsers using javascript and in fact Microsoft are currently looking at using it in live clipboard I personally think this is a very exciting prospect for where web 2.0 is going

December 11, 2007 5:30 PM  
Anonymous Rodrigo Rocco Rubim said...

Besides being astonished by the fact it works (i tried "javascript clipboard" in google and felt here), your code helped me a lot! thanks man!!!
I was trying to figure out a way of seeing source code of dynamically created pages. Here is the final solution (do this in your site-bar, obviously in IE, as told above):

javascript:window.clipboardData.setData("Text", document.getElementsByTagName('html')[0].innerHTML);void(0);

and...voilà! The source code dynamically created is there

Best regards from Rio de Janeiro, Brazil

January 11, 2008 4:48 AM  
Anonymous Anonymous said...

Another wonderful solution from Microsoft. Gotta love the rich guys.

April 30, 2008 4:52 PM  

Post a Comment

Links to this post:

Create a Link

<< Home