chrome-extensions-samples/functional-samples/cookbook.offscreen-clipboar...
Oliver Dunk d2296fac85
Update dependencies (#1096)
Updates a nunber of dependencies in a single PR. These were
suggested by dependabot but updating them individually would
mean the reposistory would be in a broken state until everything
had merged.

I'm not personally a huge fan of lowercasing doctype, but
Prettier has taken an opinionated stance here and there is not a
way to disable it:

https://github.com/prettier/prettier/issues/15096
2024-02-20 12:53:23 +01:00
..
README.md Update description fields for all samples (#994) 2023-08-17 18:19:42 +01:00
background.js Update dependencies (#1096) 2024-02-20 12:53:23 +01:00
manifest.json Update description fields for all samples (#994) 2023-08-17 18:19:42 +01:00
offscreen.html Update dependencies (#1096) 2024-02-20 12:53:23 +01:00
offscreen.js Add offscreen dom parsing sample (#835) 2023-03-03 14:06:43 +01:00

README.md

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

Context

As of January 2023, the web platform has two 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.