Advertisement
rAthus

JavaScript : add text when copying content

Mar 21st, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function addtext()
  2. {
  3.     var text = "<br /><br />Read more at www.arkanite.com";
  4.     var body = document.getElementsByTagName('body')[0];
  5.     var selection = window.getSelection();
  6.     var div = document.createElement('div');
  7.     for (var i=0; i<selection.rangeCount;++i)
  8.         div.appendChild(selection.getRangeAt(i).cloneContents());
  9.     var copytext = div.innerHTML+text;
  10.     var newdiv = document.createElement('div');
  11.     newdiv.style.position='absolute';
  12.     newdiv.style.top='-9999px';
  13.     body.appendChild(newdiv);
  14.     newdiv.innerHTML = copytext;
  15.     selection.selectAllChildren(newdiv);
  16.     window.setTimeout(function()
  17.     {
  18.         body.removeChild(newdiv);
  19.     }, 0);
  20. }
  21. document.oncopy = addtext;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement