Skip to content

Commit

Permalink
sw: catch Duplicate script ID 'inpage' error
Browse files Browse the repository at this point in the history
- related to #15958
  • Loading branch information
digiwand committed Sep 24, 2022
1 parent b6330fa commit 26ba321
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions app/scripts/app-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,27 @@ chrome.runtime.onMessage.addListener(() => {
* MAIN world injection does not work properly via manifest
* https://bugs.chromium.org/p/chromium/issues/detail?id=634381
*/
chrome.scripting.registerContentScripts([
{
id: 'inpage',
matches: ['file://*/*', 'http://*/*', 'https://*/*'],
js: ['inpage.js'],
runAt: 'document_start',
world: 'MAIN',
},
]);
const registerInPageContentScript = async () => {
try {
await chrome.scripting.registerContentScripts([
{
id: 'inpage',
matches: ['file://*/*', 'http://*/*', 'https://*/*'],
js: ['inpage.js'],
runAt: 'document_start',
world: 'MAIN',
},
]);
} catch (err) {
/**
* An error occurs when app-init is reloaded. Attempts to avoid the duplicate script error:
* 1. registeringContentScripts inside runtime.onInstalled - This cased a race condition in
* which the provider might not be loaded in time.
* 2. await chrome.scripting.getRegisteredContentScripts() to check for an existing
* inpage script before registering - The provider is not loaded on time.
*/
console.warn(`Dropped attempt to register inpage content script. ${err}`);
}
};

registerInPageContentScript();

0 comments on commit 26ba321

Please sign in to comment.