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:
Answer area listenercopy
And block the default behavior of the event.
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.copy
Event, and a prompt is displayed. if the length of the copied text is smaller than 42, no copyright information is added.
copy
For details about the event, refer:
Https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/oncopy
Http://codebits.glennjones.net/editing/setclipboarddata.htm
No.