diff --git a/app/scripts/app-init.js b/app/scripts/app-init.js index 275595beafed..b106b462fcfb 100644 --- a/app/scripts/app-init.js +++ b/app/scripts/app-init.js @@ -194,18 +194,24 @@ registerInPageContentScript(); * folder for more details. */ async function createOffscreen() { - if (await chrome.offscreen.hasDocument()) { - return; + try { + await chrome.offscreen.createDocument({ + url: './offscreen.html', + reasons: ['IFRAME_SCRIPTING'], + justification: + 'Used for Hardware Wallet and Snaps scripts to communicate with the extension.', + }); + console.debug('Offscreen iframe loaded'); + } catch (error) { + if ( + !error?.message?.startsWith( + 'Only a single offscreen document may be created', + ) + ) { + throw error; + } + console.debug('Offscreen document already exists; skipping creation'); } - - await chrome.offscreen.createDocument({ - url: './offscreen.html', - reasons: ['IFRAME_SCRIPTING'], - justification: - 'Used for Hardware Wallet and Snaps scripts to communicate with the extension.', - }); - - console.debug('Offscreen iframe loaded'); } createOffscreen();