Skip to content

Commit a10f632

Browse files
Minimum viable fix for removing hasDocument() method (GoogleChrome#823)
1 parent e868874 commit a10f632

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

Diff for: cookbook/offscreen-clipboard-write/background.js

+6-17
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,13 @@ chrome.action.onClicked.addListener(async () => {
2626
// `document.execCommand()`. To work around this, we'll create an offscreen
2727
// document and pass it the data we want to write to the clipboard.
2828
async function addToClipboard(value) {
29-
// This pattern ensures that the offscreen document exists before we try to
30-
// send it a message. If we didn't await the `hasDocument()` and
31-
// `createDocument()` calls, the `sendMessage()` call could send a message
32-
// before the offscreen document can register it's `runtime.onMessage`
33-
// listener.
34-
if (await chrome.offscreen.hasDocument()) {
35-
console.debug('Offscreen doc already exists.');
36-
} else {
37-
console.debug('Creating a new offscreen document.');
38-
39-
await chrome.offscreen.createDocument({
40-
url: 'offscreen.html',
41-
reasons: [chrome.offscreen.Reason.CLIPBOARD],
42-
justification: 'Write text to the clipboard.',
43-
});
44-
}
29+
await chrome.offscreen.createDocument({
30+
url: 'offscreen.html',
31+
reasons: [chrome.offscreen.Reason.CLIPBOARD],
32+
justification: 'Write text to the clipboard.',
33+
});
4534

46-
// Now that are sure we have an offscreen document, we can safely dispatch the
35+
// Now that we have an offscreen document, we can dispatch the
4736
// message.
4837
chrome.runtime.sendMessage({
4938
type: 'copy-data-to-clipboard',

Diff for: cookbook/offscreen-clipboard-write/offscreen.js

+3
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,7 @@ async function handleClipboardWrite(data) {
6565
textEl.value = data;
6666
textEl.select();
6767
document.execCommand('copy');
68+
69+
//Job's done! Close the offscreen document.
70+
window.close();
6871
}

0 commit comments

Comments
 (0)