Injecting content into text copied from the page

4

I'd like to know how to inject content into text copied from the page. When I copied a text from a website, it came along with an institutional text and the site link. You can see this happening in this site .

    
asked by anonymous 27.02.2015 / 22:04

1 answer

3

To do this you have to add an event handler to the event copy .

Within this handler you have to get the text already selected and then add the new part.

Example:

function addLink(e) {
    e.preventDefault();
    var copyright = ' - Ler mais aqui: www.google.com';
    var novoTexto = copytext = window.getSelection() + copyright;
    (window.clipboardData ? window : event).clipboardData.setData('Text', copytext);
}

window.addEventListener('copy', addLink);

jsFiddle: link

In some browsers clipboardData is a property of event , in IE it is a property of window , hence the window.clipboardData ? window : event

    
27.02.2015 / 23:06