From 56587c8c517a80fb11dda45f14e34725977a5e14 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sat, 30 Aug 2025 13:25:20 +0000 Subject: [PATCH 1/4] refactor: add once option to the listeners --- src/ext/content-scripts/entry-userscripts.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ext/content-scripts/entry-userscripts.js b/src/ext/content-scripts/entry-userscripts.js index 5721dd90..e160d7e3 100644 --- a/src/ext/content-scripts/entry-userscripts.js +++ b/src/ext/content-scripts/entry-userscripts.js @@ -25,19 +25,23 @@ function triageJS(userscript) { if (document.readyState !== "loading") { injectJS(userscript); } else { - document.addEventListener("DOMContentLoaded", () => { - injectJS(userscript); - }); + document.addEventListener( + "DOMContentLoaded", + () => injectJS(userscript), + { once: true }, + ); } } else if (runAt === "document-idle") { if (document.readyState === "complete") { injectJS(userscript); } else { - document.addEventListener("readystatechange", () => { + const handle = () => { if (document.readyState === "complete") { injectJS(userscript); + document.removeEventListener("readystatechange", handle); } - }); + }; + document.addEventListener("readystatechange", handle); } } } @@ -233,6 +237,10 @@ async function injection() { } function listeners() { + /** listen for CSP violations */ + document.addEventListener("securitypolicyviolation", cspFallback, { + once: true, + }); // listens for messages from background, popup, etc... browser.runtime.onMessage.addListener((request) => { const name = request.name; @@ -255,8 +263,6 @@ function listeners() { console.error(`Couldn't find ${filename} code!`); } }); - // listen for CSP violations - document.addEventListener("securitypolicyviolation", cspFallback); } async function initialize() { From 7e9301b00048b0eeac6266a727e07e7f4266bedc Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sat, 30 Aug 2025 13:33:59 +0000 Subject: [PATCH 2/4] fix: dynamically remove listeners when tab unload `browser.runtime.onMessage` listeners seems to prevent content scripts recycle from causing memory leaks --- .../content-scripts/entry-script-market.js | 23 +++++++++++++++---- src/ext/content-scripts/entry-userscripts.js | 23 +++++++++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/ext/content-scripts/entry-script-market.js b/src/ext/content-scripts/entry-script-market.js index 67022e47..45ba17d6 100644 --- a/src/ext/content-scripts/entry-script-market.js +++ b/src/ext/content-scripts/entry-script-market.js @@ -9,10 +9,10 @@ async function injection() { ); } for (const link of links) { + if (link["href"]) url = link["href"]; link.addEventListener( "click", (e) => { - url = link["href"]; e.stopImmediatePropagation(); e.preventDefault(); browser.runtime.sendMessage({ name: "WEB_USERJS_POPUP" }); @@ -23,14 +23,27 @@ async function injection() { } async function listeners() { - browser.runtime.onMessage.addListener(async (message) => { + /** + * handle messages from background, popup, etc... + * @type {import("webextension-polyfill").Runtime.OnMessageListener} + */ + const handleMessage = async (message) => { if (import.meta.env.MODE === "development") { console.debug(message, url); } if (message === "TAB_CLICK_USERJS") { - const response = url; - url = undefined; // respond only once of click event - return response; + return url; + } + }; + /** Dynamically remove listeners to avoid memory leaks */ + if (document.visibilityState === "visible") { + browser.runtime.onMessage.addListener(handleMessage); + } + document.addEventListener("visibilitychange", () => { + if (document.hidden) { + browser.runtime.onMessage.removeListener(handleMessage); + } else { + browser.runtime.onMessage.addListener(handleMessage); } }); } diff --git a/src/ext/content-scripts/entry-userscripts.js b/src/ext/content-scripts/entry-userscripts.js index e160d7e3..362bfacb 100644 --- a/src/ext/content-scripts/entry-userscripts.js +++ b/src/ext/content-scripts/entry-userscripts.js @@ -241,17 +241,19 @@ function listeners() { document.addEventListener("securitypolicyviolation", cspFallback, { once: true, }); - // listens for messages from background, popup, etc... - browser.runtime.onMessage.addListener((request) => { - const name = request.name; + /** + * listens for messages from background, popup, etc... + * @type {import("webextension-polyfill").Runtime.OnMessageListener} + */ + const handleMessage = (message) => { + const name = message.name; if (name === "CONTEXT_RUN") { // from bg script when context-menu item is clicked // double check to ensure context-menu scripts only run in top windows if (window !== window.top) return; - // loop through context-menu scripts saved to data object and find match // if no match found, nothing will execute and error will log - const filename = request.menuItemId; + const filename = message.menuItemId; for (let i = 0; i < data.files.menu.length; i++) { const item = data.files.menu[i]; if (item.scriptObject.filename === filename) { @@ -262,6 +264,17 @@ function listeners() { } console.error(`Couldn't find ${filename} code!`); } + }; + /** Dynamically remove listeners to avoid memory leaks */ + if (document.visibilityState === "visible") { + browser.runtime.onMessage.addListener(handleMessage); + } + document.addEventListener("visibilitychange", () => { + if (document.hidden) { + browser.runtime.onMessage.removeListener(handleMessage); + } else { + browser.runtime.onMessage.addListener(handleMessage); + } }); } From f8683a962eb88e286aa28f50b6d0c1b87d55daf8 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sat, 30 Aug 2025 14:04:38 +0000 Subject: [PATCH 3/4] build(gem): update dependencies --- fastlane/Gemfile.lock | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/fastlane/Gemfile.lock b/fastlane/Gemfile.lock index a8ba4db2..8cbf6d55 100644 --- a/fastlane/Gemfile.lock +++ b/fastlane/Gemfile.lock @@ -10,25 +10,27 @@ GEM artifactory (3.0.17) atomos (0.1.3) aws-eventstream (1.4.0) - aws-partitions (1.1125.0) - aws-sdk-core (3.226.2) + aws-partitions (1.1153.0) + aws-sdk-core (3.232.0) aws-eventstream (~> 1, >= 1.3.0) aws-partitions (~> 1, >= 1.992.0) aws-sigv4 (~> 1.9) base64 + bigdecimal jmespath (~> 1, >= 1.6.1) logger - aws-sdk-kms (1.106.0) - aws-sdk-core (~> 3, >= 3.225.0) + aws-sdk-kms (1.112.0) + aws-sdk-core (~> 3, >= 3.231.0) aws-sigv4 (~> 1.5) - aws-sdk-s3 (1.192.0) - aws-sdk-core (~> 3, >= 3.225.0) + aws-sdk-s3 (1.198.0) + aws-sdk-core (~> 3, >= 3.231.0) aws-sdk-kms (~> 1) aws-sigv4 (~> 1.5) aws-sigv4 (1.12.1) aws-eventstream (~> 1, >= 1.0.2) babosa (1.0.4) base64 (0.3.0) + bigdecimal (3.2.2) claide (1.1.0) colored (1.2) colored2 (3.1.2) @@ -157,13 +159,13 @@ GEM httpclient (2.9.0) mutex_m jmespath (1.6.2) - json (2.12.2) + json (2.13.2) jwt (2.10.2) base64 logger (1.7.0) mini_magick (4.13.2) mini_mime (1.1.5) - multi_json (1.15.0) + multi_json (1.17.0) multipart-post (2.4.1) mutex_m (0.3.0) nanaimo (0.4.0) @@ -179,15 +181,15 @@ GEM trailblazer-option (>= 0.1.1, < 0.2.0) uber (< 0.2.0) retriable (3.1.2) - rexml (3.4.1) + rexml (3.4.2) rouge (3.28.0) ruby2_keywords (0.0.5) rubyzip (2.4.1) security (0.1.5) - signet (0.20.0) + signet (0.21.0) addressable (~> 2.8) faraday (>= 0.17.5, < 3.a) - jwt (>= 1.5, < 3.0) + jwt (>= 1.5, < 4.0) multi_json (~> 1.10) simctl (1.6.10) CFPropertyList From bcf0e9c02678ec74a1905e3c76cb051d4ae51b84 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sat, 30 Aug 2025 14:05:24 +0000 Subject: [PATCH 4/4] ci: update xcode version --- .github/workflows/deployment.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deployment.yaml b/.github/workflows/deployment.yaml index 86855b3f..e66a4544 100644 --- a/.github/workflows/deployment.yaml +++ b/.github/workflows/deployment.yaml @@ -38,7 +38,7 @@ jobs: - run: npm run build:${{ matrix.platform }}-safari-15 if: ${{ ! github.event.release.prerelease }} - name: Set xcode version - run: sudo xcode-select -s "/Applications/Xcode_26_beta.app" # https://github.com/actions/runner-images/blob/main/images/macos/macos-15-arm64-Readme.md#xcode + run: sudo xcode-select -s "/Applications/Xcode_26_beta_6.app" # https://github.com/actions/runner-images/blob/main/images/macos/macos-15-arm64-Readme.md#xcode - run: xcodebuild -downloadPlatform iOS # Temporary fix for Xcode_26_beta - name: Run fastlane id: fastlane