-1

While working on something non-development related, I noticed something interested while on answers.com. I copied and pasted some text from an answer, and noticed that along with my answer I got some text that I didn't copy. After looking into it some more, it seems that if you copy 8 or more words from anywhere on the site, it adds some text when you paste it. For instance, if I go to a page on the site and copy 8 words or more, and then paste that somewhere, you will get something like:

Insanity is doing the same thing over and over again but expecting different results.

Read more: http://wiki.answers.com/Q/Who_first_said_the_definition_of_insanity_is_to_do_the_same_thing_over_and_over_and_expect_different_results#ixzz1xqDl0yUW

My questions is, does anyone know how that is done?! I've looked into the code a bit, but haven't found anything giving me any clues :) Thanks!

P.S. Yes, that page was the one I was viewing when I noticed this :)

ansarob
  • 825
  • 2
  • 13
  • 32

1 Answers1

1
function addText() {
    var body_element = document.body;
    var selection = window.getSelection();
    var extraText = "<br /><br />some extra info here";
    var copytext = selection + extraText;
    var newdiv = document.createElement('div');
    newdiv.setAttribute('style','position:absolute;left:-99999px;')
    newdiv.innerHTML = copytext;
    body_element.appendChild(newdiv);
    selection.selectAllChildren(newdiv);
    window.setTimeout(function() {
        body_element.removeChild(newdiv);
    },0);
}
document.oncopy = addText;

Credits: How to add extra info to copied web text

Community
  • 1
  • 1
Sepehr
  • 2,051
  • 19
  • 29