Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

javascript - Injecting text when content is copied from a web page

I ran into a a site that injects a "Read More: " line at the end of any text that you highlight and copy from it's pages.

I've never seen this before, and without severely digging into their code, I can't find the code that does this.

Anyone know how this is done?

Example: http://peaceful-parenting.suite101.com/article.cfm/how_to_keep_kids_from_climbing_on

Copy a paragraph of text, and paste it into another application, and there will be a "read more" line afterwards.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Suite101 is using a 3rd party service called Tynt.

Tynt's JavaScript snippet is located at http://tcr.tynt.com/javascripts/Tracer.js

I explored the source for you (with some help from the Online JavaScript beautifier).

So here's what's happening:

  • the code activates on events like mouseup, mousedown, and copy (in the startListeningForTraces function)
  • if the user selected a range of text, then it:
    • creates the HTML for an attribution link, plus optional CC license URL
    • appends this HTML to the selection, placing it inside a zero-size <div> (to keep it invisible on-screen)
    • reports what was copied back to Tynt's servers

Tynt's code does a tremendous amount of work to make this work seamlessly across browsers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...