- 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
57 lines
1.8 KiB
JavaScript
57 lines
1.8 KiB
JavaScript
// popup.js for Magnetflix toolbar popup
|
|
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";
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
const capturedPanel = document.getElementById("captured-state");
|
|
const manualPanel = document.getElementById("manual-state");
|
|
const magnetDisplay = document.getElementById("magnet-display");
|
|
const manualInput = document.getElementById("manual-magnet-input");
|
|
const btnContinueCaptured = document.getElementById("btn-continue-captured");
|
|
const btnContinueManual = document.getElementById("btn-continue-manual");
|
|
const btnLoadDemo = document.getElementById("btn-load-demo");
|
|
|
|
let currentCapturedMagnet = "";
|
|
|
|
// Check if a magnet was captured recently (within last 24h)
|
|
browserAPI.storage.local.get(["lastMagnet", "lastCapturedAt"], (result) => {
|
|
if (result && result.lastMagnet) {
|
|
currentCapturedMagnet = result.lastMagnet;
|
|
magnetDisplay.textContent = currentCapturedMagnet;
|
|
capturedPanel.classList.remove("hidden");
|
|
}
|
|
});
|
|
|
|
function openApp(magnetUrl) {
|
|
browserAPI.runtime.sendMessage(
|
|
{
|
|
action: "open_app",
|
|
magnet: magnetUrl
|
|
},
|
|
() => {
|
|
window.close();
|
|
}
|
|
);
|
|
}
|
|
|
|
btnContinueCaptured.addEventListener("click", () => {
|
|
if (currentCapturedMagnet) {
|
|
openApp(currentCapturedMagnet);
|
|
}
|
|
});
|
|
|
|
btnContinueManual.addEventListener("click", () => {
|
|
const val = manualInput.value.trim();
|
|
if (val) {
|
|
openApp(val);
|
|
} else {
|
|
manualInput.focus();
|
|
}
|
|
});
|
|
|
|
btnLoadDemo.addEventListener("click", () => {
|
|
manualInput.value = SAMPLE_MAGNET;
|
|
});
|
|
});
|