sw.mjs raw
1 let _offscreenInflight = null;
2
3 function ensureOffscreen() {
4 if (_offscreenInflight) return _offscreenInflight;
5 _offscreenInflight = chrome.offscreen.getContexts({ reasons: ['WORKERS'] })
6 .then(ctxs => {
7 if (ctxs.length === 0) {
8 return chrome.offscreen.createDocument({
9 url: chrome.runtime.getURL('offscreen.html'),
10 reasons: ['WORKERS'],
11 justification: 'Hosts persistent signer wasm Worker'
12 });
13 }
14 })
15 .finally(() => { _offscreenInflight = null; });
16 return _offscreenInflight;
17 }
18
19 chrome.runtime.onInstalled.addListener(() => ensureOffscreen());
20 chrome.runtime.onStartup.addListener(() => ensureOffscreen());
21
22 chrome.runtime.onMessage.addListener((msg, _sender, respond) => {
23 if (msg.target) return false;
24 ensureOffscreen()
25 .then(() => chrome.runtime.sendMessage({ target: 'offscreen', ...msg }))
26 .then(r => respond(r))
27 .catch(e => respond({ error: e.message }));
28 return true;
29 });
30