From 0a53bbd942229c31da5cf4e5ad16a42e63e83e7b Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Fri, 25 Sep 2026 16:51:40 -0500 Subject: [PATCH] fix(extension): resolve browserAPI from global lexical scope instead of window - In Firefox WebExtension content scripts, window is the webpage DOM window and does not expose extension APIs for security - Access global browser/chrome identifiers directly via typeof checks - Wrap background message dispatch in try-catch to prevent uncaught exceptions from blocking in-page modal display --- app.js | 2 +- background.js | 2 +- content.js | 20 +++++++++++++------- popup.js | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app.js b/app.js index 8712c35..fce7c3f 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,5 @@ // app.js - Logic for Magnetflix Page -const browserAPI = window.browser || window.chrome; +const browserAPI = typeof browser !== "undefined" ? browser : (typeof chrome !== "undefined" ? chrome : null); // State let selectedMovie = null; diff --git a/background.js b/background.js index e55dde8..3cf1410 100644 --- a/background.js +++ b/background.js @@ -1,5 +1,5 @@ // Background script for Magnetflix extension -const browserAPI = window.browser || window.chrome; +const browserAPI = typeof browser !== "undefined" ? browser : (typeof chrome !== "undefined" ? chrome : null); // Listen for messages from content scripts and popups browserAPI.runtime.onMessage.addListener((request, sender, sendResponse) => { diff --git a/content.js b/content.js index 7dc1c07..bec50dc 100644 --- a/content.js +++ b/content.js @@ -1,6 +1,6 @@ // Content script for Magnetflix: intercepts magnet links and displays popup modal (function () { - const browserAPI = window.browser || window.chrome; + const browserAPI = typeof browser !== "undefined" ? browser : (typeof chrome !== "undefined" ? chrome : null); let activeModalHost = null; // Helper to extract display name from magnet link @@ -330,14 +330,20 @@ event.preventDefault(); event.stopPropagation(); - // Inform background script of captured magnet - browserAPI.runtime.sendMessage({ - action: "magnet_captured", - magnet: href - }); - // Show popup modal on the page showMagnetModal(href); + + // Inform background script of captured magnet + if (browserAPI && browserAPI.runtime && browserAPI.runtime.sendMessage) { + try { + browserAPI.runtime.sendMessage({ + action: "magnet_captured", + magnet: href + }); + } catch (err) { + console.warn("Magnetflix: Could not notify background script:", err); + } + } } } }, diff --git a/popup.js b/popup.js index 6dcfae8..fad3202 100644 --- a/popup.js +++ b/popup.js @@ -1,5 +1,5 @@ // popup.js for Magnetflix toolbar popup -const browserAPI = window.browser || window.chrome; +const browserAPI = typeof browser !== "undefined" ? browser : (typeof chrome !== "undefined" ? chrome : null); const SAMPLE_MAGNET = "magnet:?xt=urn:btih:1234567890abcdef1234567890abcdef12345678&dn=Perfect.Blue.1997.1080p.BluRay.x264";