Files
chrome-extensions-samples/functional-samples/cookbook.offscreen-clipboard-write
Sebastian Benz 1229875d30 Add offscreen dom parsing sample (#835)
* Add offscreen dom parsing sample

* Don't use offscreen.hasDocument

Use clients.matchAll instead. Also fixes a bug in the clipboard example
causing the document not to be closed if the passed argument is not a
string.

* Fix readme and remove accidentally committed popup files.
2023-03-03 14:06:43 +01:00
..
2023-02-22 13:25:39 +01:00
2023-02-22 13:25:39 +01:00
2023-02-22 13:25:39 +01:00

This recipe shows how to write a string to the system clipboard using the Offscreen API.

Context

As of January 2023, the web platform has to ways to interact with the clipboard: document.execCommand() and navigator.clipboard (see MDN's docs). Unfortunately, neither of these APIs are exposed to JavaScript workers. This means that in order for an extension to read from or write values to the system clipboard, the extension must do so in a web page. Enter the Offscreen API. This API was introduced to give extension developers an unobtrusive way to use DOM APIs in the background.

In the future, the Chrome team is planning to add clipboard support directly to extension service workers. As such, this recipe is written to make it as easy as possible to replace addToClipboard()'s offscreen document-based implementation with one that directly uses the appropriate clipboard API.

Running this extension

  1. Clone this repository.
  2. Load this directory in Chrome as an unpacked extension.
  3. Open the Extension menu and click the extension named "Offscreen API - Clipboard".

You will now have "Hello, World!" on your system clipboard.