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 .
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 .
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);
In some browsers clipboardData
is a property of event
, in IE it is a property of window
, hence the window.clipboardData ? window : event