Skip to content

Commit

Permalink
Fix extension restarts in Firefox (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcustyphoon authored Jul 18, 2024
1 parent 526466b commit 9383a00
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/content_scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@

const { nonce } = [...document.scripts].find(script => script.getAttributeNames().includes('nonce'));
const script = document.createElement('script');
script.type = 'module';
script.nonce = nonce;
script.src = browser.runtime.getURL('/main_world/index.js');
document.documentElement.append(script);
Expand Down
60 changes: 32 additions & 28 deletions src/main_world/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
const moduleCache = {};
'use strict';

document.documentElement.addEventListener('xkitinjectionrequest', async event => {
const { detail, target } = event;
const { id, path, args } = JSON.parse(detail);
{
const moduleCache = {};

try {
moduleCache[path] ??= await import(path);
const func = moduleCache[path].default;
document.documentElement.addEventListener('xkitinjectionrequest', async event => {
const { detail, target } = event;
const { id, path, args } = JSON.parse(detail);

if (target.isConnected === false) return;
try {
moduleCache[path] ??= await import(path);
const func = moduleCache[path].default;

const result = await func.apply(target, args);
target.dispatchEvent(
new CustomEvent('xkitinjectionresponse', { detail: JSON.stringify({ id, result }) })
);
} catch (exception) {
target.dispatchEvent(
new CustomEvent('xkitinjectionresponse', {
detail: JSON.stringify({
id,
exception: {
message: exception.message,
name: exception.name,
stack: exception.stack,
...exception
}
if (target.isConnected === false) return;

const result = await func.apply(target, args);
target.dispatchEvent(
new CustomEvent('xkitinjectionresponse', { detail: JSON.stringify({ id, result }) })
);
} catch (exception) {
target.dispatchEvent(
new CustomEvent('xkitinjectionresponse', {
detail: JSON.stringify({
id,
exception: {
message: exception.message,
name: exception.name,
stack: exception.stack,
...exception
}
})
})
})
);
}
});
);
}
});

document.documentElement.dispatchEvent(new CustomEvent('xkitinjectionready'));
document.documentElement.dispatchEvent(new CustomEvent('xkitinjectionready'));
}

0 comments on commit 9383a00

Please sign in to comment.