Injecting text content copied from page

Asked

Viewed 114 times

4

I would 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 link from the website. You can see this happening on this website.

1 answer

3


To do this you need to add an Event Handler to the event copy.

Inside that Handler you have to fetch the already selected text 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: http://jsfiddle.net/wq6egLL9/

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

  • 1

    Our Perfect thank you so much

  • There is a great news portal here in Brazil that does this, replacing the copied text with a notice + link to the post. It’s extremely annoying, especially when you just want to copy a sentence.

  • Sergio, how do I run this example in Firefox? It worked on Chrome and IE here.

  • This is very good to protect content

Browser other questions tagged

You are not signed in. Login or sign up in order to post.