Javascript: how to add more text to text copied by ala nydailynews.com

In nydailynews.com, when copying and pasting any text from a site, a fragment of text is added.

More details: http://www.nydailynews.com/#ixzz0aN123abc

How do they achieve this?

I searched all external javascript files (jquery) and cannot find anything that matches. Is this something that can be done in simple CSS?

+3
source share
2 answers

EventBug Firefox, , copy. JS . , , .

+3

: http://bavotasan.com/2010/add-a-copyright-notice-to-copied-text/

<script type="text/javascript">
function addLink() {
var body_element = document.getElementsByTagName('body')[0];
var selection;
selection = window.getSelection();
var pagelink = "<br /><br /> Read more at: <a href='"+document.location.href+"'>"+document.location.href+"</a><br />Copyright &copy; c.bavota"; // change this if you want
var copytext = selection + pagelink;
var newdiv = document.createElement('div');
newdiv.style.position='absolute';
newdiv.style.left='-99999px';
body_element.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function() {
    body_element.removeChild(newdiv);
},0);
}
document.oncopy = addLink;
</script>
+2

Source: https://habr.com/ru/post/1726300/


All Articles