views:

401

answers:

2

In JavaScript, how can you select text on a website, copy it (by Control+C, Command+C, or Edit Copy) and have JavaScript append a line or two to the clipboard so when the user pastes, the content they copied is shown as well as the extra line?

Also, would this be possible to do only within certain <div>s of the site? If so, how?

+1  A: 

You can use a combination of execCommand("Copy") and execCommand("Paste") to accomplish what you want.

This should help you out:

http://www.geekpedia.com/tutorial126_Clipboard-cut-copy-and-paste-with-JavaScript.html

James Skidmore
The problem is, how can you detect when the user copies something? Then I could figure out how to do it from that page you sent me.
jQuery has some excellent built-in copy/paste detection: http://www.devcurry.com/2009/07/detect-copy-paste-and-cut-operations-on.html
James Skidmore
A: 

I came across this on the faqs.org site [1] and was curious too. They use some javascript from tynt.com. I also found an ask-metafilter answer [2] pointing to a different javascript. They should be good starting points. I haven't worked it out yet myself but I expect you could attach event listeners to just the div in question.

  1. http://www.faqs.org/faqs/tv/sat-night-live/deep-thoughts/
  2. http://ask.metafilter.com/133804/How-does-HuffPo-append-URLs-to-copied-text
jla