|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Once the message has been posted from the service worker, checks are made to |
| 16 | +// confirm the message type and target before proceeding. This is so that the |
| 17 | +// module can easily be adapted into existing workflows where secondary uses for |
| 18 | +// the document (or alternate offscreen documents) might be implemented. |
| 19 | + |
| 20 | +// Registering this listener when the script is first executed ensures that the |
| 21 | +// offscreen document will be able to receive messages when the promise returned |
| 22 | +// by `offscreen.createDocument()` resolves. |
| 23 | +chrome.runtime.onMessage.addListener(handleMessages); |
| 24 | + |
| 25 | +// This function performs basic filtering and error checking on messages before |
| 26 | +// dispatching the |
| 27 | +// message to a more specific message handler. |
| 28 | +async function handleMessages(message) { |
| 29 | + // Return early if this message isn't meant for the offscreen document. |
| 30 | + if (message.target !== 'offscreen-doc') { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Dispatch the message to an appropriate handler. |
| 35 | + switch (message.type) { |
| 36 | + case 'copy-data-to-clipboard': |
| 37 | + handleClipboardWrite(message.data); |
| 38 | + break; |
| 39 | + default: |
| 40 | + console.warn(`Unexpected message type received: '${message.type}'.`); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +// We use a <textarea> element for two main reasons: |
| 46 | +// 1. preserve the formatting of multiline text, |
| 47 | +// 2. select the node's content using this element's `.select()` method. |
| 48 | +let textEl = document.querySelector('#text'); |
| 49 | + |
| 50 | +// Use the offscreen document's `document` interface to write a new value to the |
| 51 | +// system clipboard. |
| 52 | +// |
| 53 | +// At the time this demo was created (Jan 2023) the `navigator.clipboard` API |
| 54 | +// requires that the window is focused, but offscreen documents cannot be |
| 55 | +// focused. As such, we have to fall back to `document.execCommand()`. |
| 56 | +async function handleClipboardWrite(data) { |
| 57 | + // Error if we received the wrong kind of data. |
| 58 | + if (typeof data !== 'string') { |
| 59 | + throw new TypeError(`Value provided must be a 'string', got '${typeof data}'.`); |
| 60 | + } |
| 61 | + |
| 62 | + // `document.execCommand('copy')` works against the user's selection in a web |
| 63 | + // page. As such, we must insert the string we want to copy to the web page |
| 64 | + // and to select that content in the page before calling `execCommand()`. |
| 65 | + textEl.value = data; |
| 66 | + textEl.select(); |
| 67 | + document.execCommand('copy'); |
| 68 | +} |
0 commit comments