From c409840e9f57e122e4c0cab9c265204590ba1f01 Mon Sep 17 00:00:00 2001 From: nick-delirium Date: Fri, 23 May 2025 12:07:56 +0200 Subject: [PATCH] spot: extra permission, unmount listeners on invalidation --- spot/entrypoints/content/index.tsx | 15 ++++++++++++--- spot/wxt.config.ts | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/spot/entrypoints/content/index.tsx b/spot/entrypoints/content/index.tsx index 846a085e2..6957d4798 100644 --- a/spot/entrypoints/content/index.tsx +++ b/spot/entrypoints/content/index.tsx @@ -55,6 +55,9 @@ export default defineContentScript({ }, }); ctx.onInvalidated(() => { + window.removeEventListener("message", msgListener); + unmountNotifications(); + injected = false; ui.remove(); }); let micResponse: boolean | null = null; @@ -237,7 +240,7 @@ export default defineContentScript({ } }; - window.addEventListener("message", (event) => { + function msgListener(event: MessageEvent) { if (event.data.type === "orspot:ping") { window.postMessage({ type: "orspot:pong" }, "*"); } @@ -267,7 +270,8 @@ export default defineContentScript({ event: event.data.event, }); } - }); + } + window.addEventListener("message", msgListener); let injected = false; function injectScript() { @@ -309,14 +313,19 @@ export default defineContentScript({ ui.remove(); } + let scriptEl: HTMLScriptElement | null = null; function mountNotifications() { - const scriptEl = document.createElement("script"); + scriptEl = document.createElement("script"); scriptEl.src = browser.runtime.getURL("/notifications.js"); document.head.appendChild(scriptEl); } function unmountNotifications() { window.postMessage({ type: "ornotif:stop" }); + if (scriptEl) { + document.head.removeChild(scriptEl); + scriptEl = null; + } } mountNotifications(); diff --git a/spot/wxt.config.ts b/spot/wxt.config.ts index 3095bdfb4..a9305c3c6 100644 --- a/spot/wxt.config.ts +++ b/spot/wxt.config.ts @@ -29,6 +29,7 @@ export default defineConfig({ "webNavigation", "webRequest", "debugger", + "scripting", ], }, webExt: {