5

On this page almost anywhere on the page if you copy you'll get the string Read more at http:// added to the end of your copy. I was wondering how. After looking at the source (post-copypaste.js) and setting a breakpoint I didn't understand. That area seems to be firing when i select text.

I tried looking at the DOM (via view selected source in firefox) and I didn't see the text in the dom. So it must be a javascript trick. I can imagine catching a control C event (i dont know if that is what is happening) but i cant imagine how you can add or affect the text being compied in since it belongs to the dom. I don't see flickering or anything

How does that JS trick work or how do i debug it to figure it out?

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • 4
    On that page, it's done via http://i.po.st/static/script/post-copypaste.js (found it in Chrome Web Inspector, there's a listener for the `copy` event on `div.block-feed`) – robertklep Feb 23 '13 at 20:39
  • 3
    Seriously...why the downvote?? The OP has a genuine question and is asking for help. Decent research has been done on the question and the OPs intentions are clear. – Mat Richardson Feb 23 '13 at 20:40
  • I'm not getting the same behaviour in Chrome 24... – guypursey Feb 23 '13 at 20:51
  • 1
    That is indeed Javascript! You can find a script to use [here](http://stackoverflow.com/questions/2026335/how-to-add-extra-info-to-copied-web-text/#answer-4777746)! – Barnabas Kecskes Feb 23 '13 at 21:14
  • @guypursey: My chrome 25 does it. –  Feb 23 '13 at 21:22
  • @Barnabas: Not bad. So it created a dom and selects that instead. But the awkward thing is the selection on the regular window/dom doesn't seem to be affected. –  Feb 23 '13 at 21:24
  • @acidzombie24 I just updated Chrome. I get the same behaviour if I copy-and-paste something between one and two lines in length. But if I copy a single word or just one line I don't. No idea if it'll help in trying to work out how this is done, but thought it might be worth mentioning. – guypursey Feb 24 '13 at 12:53

1 Answers1

0

But the awkward thing is the selection on the regular window/dom doesn't seem to be affected.

It is, but just not visible. What usually happens is there is a container somewhere else on the page (not necessarily visible). The content you have selected is being pasted in there, then extended, then copied and deleted from the container. It all needs a fraction of a second and by the time you paste it in somewhere, your clipboard is already storing the extended content.

If you look closely on the page you have linked in as an example, there is an empty div tag in the body with a class of pw-root. <div class='pw-root'></div> When you copy the text, for a second (visible in Firebug for instance) it changes as explained above then gets emptied again.

Barnabas Kecskes
  • 1,861
  • 17
  • 24
  • Interesting. I still have no idea how the selection (on screen not hidden) is untouched when the focus/selection is on the offscreen/hidden div. Someone posted http://bavotasan.com/2010/add-a-copyright-notice-to-copied-text/ and in that example the text does get unselected on copy because of the technique. –  Feb 24 '13 at 17:32
  • How do you break in such a way you can see that happening? I remember once someone showing me a chrome trick but I have no idea how to get firebug to break at that point –  Feb 24 '13 at 17:33