...

View Full Version : Copy to Clipboard



rnd me
10-10-2008, 04:39 PM
Here is a simple way to place small bits of text upon the user's clipboard.

Normally restricted by browser security, the clipboard is freely open to flash.
this function uses a simple flash app to dump to the clipboard.
It avoids any kind of user warning or confirmation regarding the clipboard that characterize JavaScript-only solutions.


Thus, it requires flash to be installed.


function copy(str) {
var D=document;
if(!copy.div) { copy.div = D.createElement('div'); D.body.appendChild(copy.div); }

flashVar = "Q1dTB3kAAAB4nKtgYI1nYOBfwMDAw8jgzPT//3975lAGBoYOdQYWhu\
SczIKk/MSiFIac1Lz0kgyG4MriktRchuLUEme41DQmBg4GGRDJ6Cc0l4l BAibCzsDO\
CDSJgwksyRwkzuAA5AIAd7oY/w==";

copy.div.innerHTML = '<embed src="'+flashVar+'" FlashVars="clipboard='+encodeURIComponent(str) +
'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
}


Example:

copy("Hello World!");
-Run above then press <CRTL>+<V> in a text area.
-It should say Hello World!

It is limited in the amount of data it can copy, about 2-4kb.
It also cannot read the clipboard at all, so don't bother to try.

Despite it's shortcomings, i still find it very useful, and hope you might as well.



Comments and questions welcome.

liorean
10-22-2008, 04:29 AM
With Flash 10, which was just released, Adobe straightened the security and among other things now requires user interaction to access local file system or clipboard.

rnd me
10-22-2008, 07:14 AM
those bums!

thanks for the heads up.

are you saying the code will not work, or is it just an extra click now?

liorean
10-22-2008, 09:11 AM
Well, read about it here: http://www.adobe.com/devnet/flashplayer/articles/fplayer10_security_changes_02.html#head31

Also interesting: http://www.adobe.com/devnet/flashplayer/articles/fplayer10_security_changes_03.html#head61

rnd me
10-22-2008, 06:14 PM
DO NOT USE

consider this script now dead.

thank you adobe.

if i can find access to copy of flash 10, i will rewrite it into a pop-up version that works.

I will re-post here if that ever happens.

pkiula
10-22-2008, 08:53 PM
Stupid Adobe!

Pls update this code if you can and do let us know. Thanks thanks thanks!

rnd me
10-22-2008, 10:15 PM
well, there are two main obsticles to me updating the code:
1. I don't own or have access to flash 10.
2. I don't program in flash.

i found the auto-copy flash file online. i simply wrote the compact and effective wrapper function that uses the flash.

since there seems to be some who are bummed about this script breaking, i offer some other, lesser, clipboard functions below.

none of them are cross-browser like the flash, and imho, not as good.
they are vendor specific, so third-party browsers are excluded.

still, they might be better than nothing. They would be fine for building your own application to run on your browser, and perhaps your friend's browser as well, if you help them set it up.

I would NOT use the below code on a site aimed at the general public.




//text version for IE5-8 and firefox 2-3:
function CBtxt(f) {var bP = typeof f == "undefined";if (window.clipboardData) {if (!bP) {window.clipboardData.setData("Text", f);return;}return window.clipboardData.getData("Text");} else {FT = "text/unicode";CI = Components.interfaces;Cb = CI.nsIClipboard;C = Components;Cs = CI.nsISupportsString;Cm = "@mozilla.org";Cmw = Cm + "/widget/clipboard;1";try {netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");e = C.classes[Cmw].createInstance(Cb);var clip = C.classes[Cmw].getService(Cb);} catch (e2) {return;}try {b = C.classes[Cm + "/widget/transferable;1"].createInstance(CI.nsITransferable);} catch (e3) {return;}b.addDataFlavor(FT);if (typeof f == "undefined") {clip.getData(b, clip.kGlobalClipboard);var str = {};var strLength = {};b.getTransferData(FT, str, strLength);if (str) {str = str.value.QueryInterface(Cs);}if (str) {f = str.data.substring(0, strLength.value / 2);}return f;}o = C.classes[Cm + "/supports-string;1"].createInstance(Cs);o.data = f;b.setTransferData(FT, o, f.length * 2);try {t = CI.nsIClipboard;} catch (e4) {return;}e.setData(b, null, t.kGlobalClipboard);return;}return;}


//IE only text version:
function CB(){ //sets or read clipboard in IE accepts 1 arg to set, returns cb when empty
if (arguments.length) {window.clipboardData.setData('Text', arguments[0])}
else { return window.clipboardData.getData('Text')}
}//end CB


//version that supports HTML markup (DOM source) for firefox only:
function CB(f) {if (!f) {f = undefined;}var bP = typeof f == "undefined"; var FT = "text/unicode";var CI = Components.interfaces;var Cb = CI.nsIClipboard;var C = Components;var Cs = CI.nsISupportsString;var Cm = "@mozilla.org";var Cmw = Cm + "/widget/clipboard;1";try {netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");var e = C.classes[Cmw].createInstance(Cb);var clip = C.classes[Cmw].getService(Cb);} catch (e2) {return;}try {var b = C.classes[Cm + "/widget/transferable;1"].createInstance(CI.nsITransferable);} catch (e3) {return;}b.addDataFlavor("text/html");b.addDataFlavor(FT);if (!f) {clip.getData(b, clip.kGlobalClipboard);var str = {};var bestFlavor = {};var strLength = {};b.getAnyTransferData(bestFlavor, str, strLength);if (bestFlavor.value == "text/html" || bestFlavor.value == "text/unicode") {if (str) {str = str.value.QueryInterface(Cs);}if (str) {f = str.data.substring(0, strLength.value / 2);}}return f;}var o = C.classes[Cm + "/supports-string;1"].createInstance(Cs);o.data = f;b.setTransferData(FT, o, f.length * 2);try {var t = CI.nsIClipboard;} catch (e4) {return;}e.setData(b, null, t.kGlobalClipboard);return;}



like i said, don't use these on your blog and expect them to work.

unlike the flash version, IE and firefox will ask for confirmation before allowing clipboard access.

Firefox users:
you must make a modification to about:config to get this code to work:
change signed.applets.codebase_principal_support to true.

note that this opens up firefox to enhanced capabilities like local file reading, clipboard access and cross-domain ajax. It will always first prompt the user to ask if these capabilities are trusted for the site. you should always click "deny" if you don't 100&#37; trust the webpage asking.

if you don't modify about:config, the user will never be asked, and the script will silently die.

While i dont use the code in web pages, i find the clipboard the fastest and simplest way to "talk" to exes and other applications, so i find it useful in my own work.

hope someone else can use this code, and if i ever do update the flash version, i will be sure to post the results here.

jhuckaby
01-08-2009, 01:56 AM
Hey guys,

Here is a free, JavaScript / Flash library for offering copy-to-clipboard functionality in your web pages. It is fully compatible with Flash Player 10.

http://code.google.com/p/zeroclipboard/

Enjoy!

- Joe

rnd me
01-08-2009, 06:15 PM
Hey guys,

Here is a free, JavaScript / Flash library for offering copy-to-clipboard functionality in your web pages. It is fully compatible with Flash Player 10.

http://code.google.com/p/zeroclipboard/

Enjoy!

- Joe

thanks for the heads up!

it's too bad that the code is 30X bigger an must be integrated into the page instead of the standalone cut and paste code i had...

but hey; it's better than nothing, and it's nice to have the functionality back.

maybey i could squeeze down the code and use a modal dialog to restore the cut and pastability...

thanks again for the update!

Daniellez
10-14-2009, 04:28 PM
I found another copy to clipboard script that works with Flash 10:

http://www.revivedwire.com/misc/scripts/Copy_to_Clipboard_with_Flash_10.php

rnd me
10-16-2009, 12:21 AM
I found another copy to clipboard script that works with Flash 10:

http://www.revivedwire.com/misc/scripts/Copy_to_Clipboard_with_Flash_10.php

thanks for the link.
This one is much smaller, it could all be worked into about 10kb.

still, it demands a physical click on a visible flash movie, whereas the old one could fire from just JS in the background.

Perhaps if it were worked into a showModalDialog routine, it could be very useful. I'm thinking a SMD popup that is as close to window.confirm() as possible, where the "ok" button runs the copy flash, and "cancel" closes the dialog.

that way, it could be used without having to change any document html or css, virtually re-providing the same "script library function" appeal of the older version (plus an extra confirm click, which probably isn't a bad thing anyway).

iAnissa
10-17-2009, 07:08 PM
Hello I created a script shaped bot automatically filled form
as: http://allosky.com/index.php the destination a feedback form skyrock
contact me: so-m3-poupey@hotmail.fr

it career
10-18-2009, 01:37 PM
Hello I created a script shaped bot automatically filled form
as: http://allosky.com/index.php the destination a feedback form skyrock
contact me: removed
In which programming language? what does the script do, better remove your email address from the post before moderator catches it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum