I write extensions for Google Chrome. I try to write / read the contents of the buffer. I do it like this:

chrome.tabs.executeScript( { code: "document.addEventListener('copy', function(e){ var data=e.clipboardData.getData('text'); alert(data);});" }) 

This method does not work. Perhaps there is a mistake somewhere? I also came across this API https://developer.chrome.com/apps/clipboard . Perhaps now buffer events can only be caught using it? The example from here https://www.w3.org/TR/clipboard-apis/#the-copy-action says that my method should work. What is the problem, I do not understand.

    1 answer 1

    In this version, chrome.tabs.executeScript executes the code synchronously. You hang up an event handler that may happen sometime in the future, so this option will not work.

    chrome.clipboard API is currently only available for the dev branch of the browser, and I would not recommend using it yet.

    Since the clipboard is available globally, its contents can be obtained from the background script using the snippet:

     function getClipboard() { var textEl = document.createElement('textarea'); document.body.appendChild(textEl); textEl.focus(); document.execCommand('paste'); var value = textEl.value; document.body.removeChild(textEl); return value; } 

    Don't forget to add clipboardRead to the permissions section in the manifest.json file.

    PS On the topic of working with the clipboard there is an example of the Clipboard History extension.