-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathoffscreen.js
42 lines (36 loc) · 1006 Bytes
/
offscreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import {createPortProxy} from '@/js/port';
import {ownRoot} from '@/js/urls';
import {getWindowClients} from './util';
const FILENAME = 'offscreen.html';
const DOC_URL = ownRoot + FILENAME;
/** @type {OffscreenAPI | CommandsAPI} */
const offscreen = createPortProxy(() => (
creating ??= create().finally(done)
), {
lock: '/' + FILENAME,
});
export default offscreen;
let creating;
async function findOffscreenClient() {
for (const c of await getWindowClients())
if (c.url === DOC_URL)
return c;
}
async function create() {
__.DEBUGTRACE('getDoc creating...');
try {
await chrome.offscreen.createDocument({
url: DOC_URL,
reasons: ['BLOBS', 'DOM_PARSER', 'MATCH_MEDIA', 'WORKERS'],
justification: 'ManifestV3 requirement',
});
} catch (err) {
if (!err.message.startsWith('Only a single offscreen')) throw err;
}
__.DEBUGLOG('getDoc created');
return findOffscreenClient();
}
function done() {
creating = null;
__.DEBUGLOG('getDoc done');
}