Include copyrighted text when copying from site

2

Would the person copy a text from my site and, no matter where it sticks, display the text it copied and a copyright message?

I found a lot of content about blocking the copy of the content, this is not what I need, I do not have any code related to this, because I do not know how to do it or if you have how to do it.

    
asked by anonymous 03.04.2018 / 02:30

1 answer

5

Here is the JavaScript code that will add extra information to the copied web text. Basically is to create an invisible DOM element and fill it with the copied html code by adding the extra information.

Font

function addLink() {
    //Obtem o texto selecionado e acrescenta as informações extras
    var selection = window.getSelection(),
        pagelink = '<br /><br /> mensagem de direitos autorais: ' + document.location.href,
        copytext = selection + pagelink,
        newdiv = document.createElement('div');

    //esconder a div recém-criada
    newdiv.style.position = 'absolute';
    newdiv.style.left = '-99999px';

    //insere a div, adiciona as informações extras e defina a nova seleção
    document.body.appendChild(newdiv);
    newdiv.innerHTML = copytext;
    selection.selectAllChildren(newdiv);

    window.setTimeout(function () {
        document.body.removeChild(newdiv);
    }, 100);
}

document.addEventListener('copy', addLink);
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam hendrerit orci vel urna tincidunt, id aliquet leo dapibus. Nunc sodales arcu auctor, aliquam augue ut, blandit lorem. Ut eleifend dui in interdum fringilla. Etiam eleifend, sem et varius ornare, massa tellus tincidunt metus, ac ultricies ex diam ac arcu. Pellentesque at scelerisque ex. Quisque lobortis lectus sit amet porttitor dapibus. Nunc eget sagittis enim. Aenean mollis rutrum ante. Etiam lacinia aliquam pellentesque. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
<br>
<textarea placeholder="Cole aqui o texto copiado" rows="12" cols="52"></textarea>

To include the title of the page ( document.title ) in the extra information:

pagelink = '<br /><br /> &copy; '+document.title+'<br /> mensagem de direitos ..................

You can determine from which length of the selected text should display the extra information. Just add that line to your code

if (("" + selection).length < 30) return;

Complete code:

function addLink() {
    //Obtem o texto selecionado e acrescenta as informações extras
    var selection = window.getSelection();
    //se a seleção é curta não vamos incomodar nossos usuários
      if (("" + selection).length < 30) return;

     var  pagelink = '<br /><br /> mensagem de direitos autorais: ' + document.location.href + ' &copy; ' +document.domain,
        copytext = selection + pagelink,
        newdiv = document.createElement('div');

    //esconder a div recém-criada
    newdiv.style.position = 'absolute';
    newdiv.style.left = '-99999px';

    //insere a div, adiciona as informações extras e defina a nova seleção
    document.body.appendChild(newdiv);
    newdiv.innerHTML = copytext;
    selection.selectAllChildren(newdiv);

    window.setTimeout(function () {
        document.body.removeChild(newdiv);
    }, 100);
}

document.addEventListener('copy', addLink);
    
03.04.2018 / 03:02