From 0ab1db04b18055b75f5c5f6b8c233ebc3733c890 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Fri, 25 Sep 2026 16:45:06 -0500 Subject: [PATCH] feat(background): add background script for storage and proxying - Persist captured magnet links in extension local storage - Update browser action badge indicator upon capturing links - Manage opening the pairing app tab with URI parameters - Provide fallback fetch proxy for dispatching downloads to FastAPI --- background.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 background.js diff --git a/background.js b/background.js new file mode 100644 index 0000000..e55dde8 --- /dev/null +++ b/background.js @@ -0,0 +1,71 @@ +// Background script for Magnetflix extension +const browserAPI = window.browser || window.chrome; + +// Listen for messages from content scripts and popups +browserAPI.runtime.onMessage.addListener((request, sender, sendResponse) => { + if (request.action === "magnet_captured") { + // Store latest captured magnet link + browserAPI.storage.local.set( + { + lastMagnet: request.magnet, + lastCapturedAt: Date.now() + }, + () => { + if (browserAPI.browserAction && browserAPI.browserAction.setBadgeText) { + browserAPI.browserAction.setBadgeText({ text: "🧲" }); + browserAPI.browserAction.setBadgeBackgroundColor({ color: "#e50914" }); + } + } + ); + sendResponse({ status: "stored" }); + return true; + } + + if (request.action === "open_app") { + const magnetParam = request.magnet ? encodeURIComponent(request.magnet) : ""; + const targetUrl = browserAPI.runtime.getURL(`app.html${magnetParam ? `?magnet=${magnetParam}` : ""}`); + + // Clear badge when opening app + if (browserAPI.browserAction && browserAPI.browserAction.setBadgeText) { + browserAPI.browserAction.setBadgeText({ text: "" }); + } + + browserAPI.tabs.create({ url: targetUrl }); + sendResponse({ status: "opened" }); + return true; + } + + if (request.action === "send_download") { + // Background fetch in case of origin/CORS or persistent background tasks + fetch("http://localhost:8000/download", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + magnet: request.magnet, + imdbid: request.imdbid + }) + }) + .then(async (response) => { + const data = await response.json().catch(() => ({})); + if (!response.ok) { + sendResponse({ + success: false, + status: response.status, + error: data.detail || response.statusText || "Server error" + }); + } else { + sendResponse({ success: true, data }); + } + }) + .catch((error) => { + sendResponse({ + success: false, + error: error.message || "Failed to connect to FastAPI at http://localhost:8000" + }); + }); + + return true; // Keep message channel open for async response + } +});