Why do some copyright notices come with Zhihu text? how is this function implemented? -Php Tutorial

Source: Internet
Author: User
For example, the last thing I copied in 123456 is that the copyright belongs to the author. For commercial reprint, please contact the author for authorization. for non-commercial reprint, please indicate the source. Author: Steven Liuyi link: www. zhihu. comquestion20451870answer15168034 Source: zhihu 123456 I copied

123456

The last thing that appears is
Copyright belongs to the author.
For commercial reprint, please contact the author for authorization. for non-commercial reprint, please indicate the source.
Author: Steven Liuyi
Link: http://www.zhihu.com/question/20451870/answer/15168034
Source: Zhihu

123456

Reply content:

For example, I copied

123456

The last thing that appears is
Copyright belongs to the author.
For commercial reprint, please contact the author for authorization. for non-commercial reprint, please indicate the source.
Author: Steven Liuyi
Link: http://www.zhihu.com/question/20451870/answer/15168034
Source: Zhihu

123456

General Idea:

  1. Answer area listenercopyAnd block the default behavior of the event.

  2. Obtain the selected content (window.getSelection()Add the copyright information to the clipboard (clipboardData.setData()).

The following code implements a simple DEMO
    
      Test    

Test dataTest

Script function setClipboardText (event) {event. preventDefault (); var node = document. createElement ('P'); // you are not familiar with documentfragment and do not know how to obtain the content in it. you use a stupid node method. appendChild (window. getSelection (). getRangeAt (0 ). cloneContents (); var htmlData ='

Copyright belongs to the author.
'+' For commercial reprinting, please contact the author for authorization. for non-commercial reprinting, please indicate the source.
'+' Author: tiantian
Link: http://segmentfault.com/u/tiantian_ <br/> '+' Source: segmentfault

'+ Node. innerHTML +'

'; Var textData =' copyright belongs to the author. \ N' + 'for commercial reprinting, please contact the author for authorization. for non-commercial reprinting, please indicate the source. \ N' + 'author: tiantian \ n link: http://segmentfault.com/u/tiantian_\n' +' Source: segmentfault \ n \ n' + window. getSelection (). getRangeAt (0); if (event. clipboardData) {event. clipboardData. setData ("text/html", htmlData); event. clipboardData. setData ("text/plain", textData);} else if (window. clipboardData) {return window. clipboardData. setData ("text", textData) ;}}; var answer = document. getElementById ("answer"); answer. addEventListener ('copy', function (e) {setClipboardText (e) ;}); script

After reading the Zhihu code, it is roughly implemented in this way, but some other judgments are added. for example, if the answer is set to "cannot be copied", it will be blocked.copyEvent, and a prompt is displayed. if the length of the copied text is smaller than 42, no copyright information is added.

copyFor details about the event, refer:
Https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/oncopy
Http://codebits.glennjones.net/editing/setclipboarddata.htm

No.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.