Skip to content

Commit 6f87603

Browse files
fixed orphan content script error #77
1 parent 4e3339a commit 6f87603

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/background/background.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ import browser from "webextension-polyfill";
1919

2020
browser.runtime.onInstalled.addListener(async ({ reason }) => {
2121
// should this be async?
22-
if (reason === "install") {
22+
if (reason === "install" || reason === "update") {
2323
// opening the welcome page first buys the extension time to inject into the avalible pages
2424
// uninstall survey
25-
browser.runtime.setUninstallURL("https://forms.gle/Eqi9Hgs86hSVrvT57");
26-
const isMissingCommands = await checkCommands();
27-
const welcomeUrl = new URL("https://tabbutler.netlify.app/welcome");
28-
if (isMissingCommands) {
29-
// if there are missing/unbound commands, set a query param to show on the welcome page
30-
welcomeUrl.searchParams.set("missing_commands", "true");
31-
await browser.tabs.create({ url: welcomeUrl.toString() }); // not really nessecary to await
25+
if (reason === "install") {
26+
browser.runtime.setUninstallURL("https://forms.gle/Eqi9Hgs86hSVrvT57");
27+
const isMissingCommands = await checkCommands();
28+
const welcomeUrl = new URL("https://tabbutler.netlify.app/welcome");
29+
if (isMissingCommands) {
30+
// if there are missing/unbound commands, set a query param to show on the welcome page
31+
welcomeUrl.searchParams.set("missing_commands", "true");
32+
await browser.tabs.create({ url: welcomeUrl.toString() }); // not really nessecary to await
33+
}
3234
}
3335
await injectExtension(); // not nessecary to await
3436
}

src/content/content.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ if (existingTabButlerModalRoot) {
1515
existingTabButlerModalRoot.remove();
1616
}
1717

18+
const shutDownEvent = "tab-butler-shutdown";
19+
// send a shutdown message to any other content script in this tab, disabling them permernently
20+
document.dispatchEvent(new CustomEvent(shutDownEvent));
21+
// add a listener for the same shutdown event
22+
document.addEventListener(shutDownEvent, shutdownScript);
23+
24+
1825
// by only creating and appending the element dynamically when it is requested, it should save on memory and reduce some parts of the code
1926
// like the visibility toggeling and the style tag removal in the root
2027
// this should also help in sites where the dom might change from time to time, invalidating
@@ -46,6 +53,7 @@ const messageListener = (messagePayload: MessagePlayload) => {
4653
browser.runtime.onMessage.addListener(messageListener);
4754

4855
function mountSearchComponent(message: Message) {
56+
console.log("mounting...");
4957
// create a new modal root on mount and append as the last child of the body
5058
tabButlerModalRoot = document.createElement("tab-butler-modal");
5159
// needs to be open so that the click event can bubble up
@@ -101,6 +109,14 @@ const unmountOnClick = (event: MouseEvent) => {
101109
}
102110
};
103111

112+
function shutdownScript() {
113+
console.log("shutting down")
114+
unmountSearchComponent();
115+
// remove the listener to so it can't be triggered again
116+
// this prevents any old content scripts in the tab from being triggered in the tab
117+
browser.runtime.onMessage.removeListener(messageListener);
118+
}
119+
104120
window.addEventListener("beforeunload", () => {
105121
if (isOpen) {
106122
unmountSearchComponent();

0 commit comments

Comments
 (0)