

If you're in a real hurry, here is a gist. In particular, background.html, background.js, and contentscript.js. If you want to see the above technique in practice, take a look at the code.

I have an extension whose only purpose is to paste, and the architecture came largely from this post. The two scripts are able to communicate pretty easily with message passing. Any pasting or copying from the DOM of the page the user is viewing has to occur in your content script. This essentially means you need to do the copy/paste actions from the system clipboard in your event/background pages, which is what Alasdair has outlined above. Take a look at the explanation of the extension architecture for a good overview of all this. This 'feature' is not documented anywhere besides Chromium's source code, so you should not rely on it and properly declare the clipboardWrite permission. That's why an extension can copy data to the clipboard. Content scripts cannot interact with the system clipboard, but they can interact with the DOM of pages the user has loaded. c00000fd Historically, extensions could always put data on the clipboard regardless of whether they have the permission. They are not able to interact with the DOM of pages the user has loaded. For anyone in a similar position, here is an expansion on his answer.īackground/event pages are able to interact with the system clipboard, provided you've requested the appropriate permissions. Alasdair's answer is excellent and worked great for me, but as a newcomer to Chrome extensions it still took me a while to get it working. Note that this will only work within a Chrome extension/app. At least on my machine, Chrome doesn't even render it to screen before it's removed, even with very large chunks of text. Var clipboardContents = helperdiv.This is too long for a comment on Alasdair's excellent response, so I'm creating another answer. Due to the fast append-select-copy-remove sequence, it doesn't seem to be necessary to hide the textarea or give it any particular CSS/attributes. read the clipboard contents from the helperdiv Var helperdiv = bg.document.createElement("div") add a DIV, contentEditable=true, to accept the paste action Here is one example of how to read the clipboard text in the background page: bg = () // get the background pageīg.= "" // clear the background page To see an example of this all working, see my BBCodePaste extension: if you want to pass the clipboard data back to an in page script, you'll need to use the message-passing API.If you make this a textarea, you will get plain-text, if you make it a div with contentEditable=true, you will get Formatted HTML create an element in your background page to accept the clipboard paste action.create a background script, since only the background script can access the clipboard.request "clipboardRead" permission in your manifest.In order to read Clipboard text in a chrome extension, you have to:
