- Capture magnet links from data-magnet attributes and stop the page's own handlers from also firing on torrent and magnet links - Treat .torrent URLs with fragments as torrent links - Show tracker status, in-flight block counts, unchoked peer counts, and per-peer state in the torrent list - Bump the extension to 0.1.2 and rebuild the .xpi - Run the AppImage router with --debug
84 lines
2.5 KiB
JavaScript
84 lines
2.5 KiB
JavaScript
"use strict";
|
|
|
|
(function () {
|
|
function showToast(text, isError) {
|
|
let el = document.getElementById("moon-i2p-torrent-toast");
|
|
if (!el) {
|
|
el = document.createElement("div");
|
|
el.id = "moon-i2p-torrent-toast";
|
|
el.style.cssText = [
|
|
"position:fixed",
|
|
"right:18px",
|
|
"bottom:18px",
|
|
"z-index:2147483647",
|
|
"padding:10px 14px",
|
|
"border-radius:10px",
|
|
"font-family:system-ui,-apple-system,sans-serif",
|
|
"font-size:13px",
|
|
"color:#f8fafc",
|
|
"box-shadow:0 8px 24px rgba(0,0,0,0.35)",
|
|
"border:1px solid rgba(255,255,255,0.16)",
|
|
"max-width:360px",
|
|
].join(";");
|
|
document.documentElement.appendChild(el);
|
|
}
|
|
el.style.background = isError ? "#991b1b" : "#0f172a";
|
|
el.textContent = text;
|
|
clearTimeout(el._hideTimer);
|
|
el._hideTimer = setTimeout(() => {
|
|
if (el.parentNode) el.parentNode.removeChild(el);
|
|
}, 4500);
|
|
}
|
|
|
|
document.addEventListener(
|
|
"click",
|
|
async (ev) => {
|
|
if (ev.button !== 0 || ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey) {
|
|
return;
|
|
}
|
|
const anchor =
|
|
ev.target && ev.target.closest
|
|
? ev.target.closest("a[href], a[data-magnet]")
|
|
: null;
|
|
if (!anchor) return;
|
|
const dataMagnet = anchor.getAttribute("data-magnet") || "";
|
|
const href = anchor.getAttribute("href") || "";
|
|
let absUrl = "";
|
|
if (href) {
|
|
try {
|
|
absUrl = new URL(href, window.location.href).href;
|
|
} catch (_e) {
|
|
absUrl = "";
|
|
}
|
|
}
|
|
const magnetUri = dataMagnet.toLowerCase().startsWith("magnet:")
|
|
? dataMagnet
|
|
: absUrl.toLowerCase().startsWith("magnet:")
|
|
? absUrl
|
|
: "";
|
|
const isMagnet = Boolean(magnetUri);
|
|
const isTorrent = !isMagnet && /\.torrent([?#]|$)/i.test(absUrl);
|
|
if (!isMagnet && !isTorrent) return;
|
|
|
|
ev.preventDefault();
|
|
ev.stopPropagation();
|
|
ev.stopImmediatePropagation();
|
|
try {
|
|
const res = await browser.runtime.sendMessage(
|
|
isMagnet
|
|
? { type: "addMagnet", uri: magnetUri }
|
|
: { type: "addTorrentUrl", url: absUrl }
|
|
);
|
|
if (res && res.ok) {
|
|
showToast(`Added to moon-i2p (${(res.info_hash || "").slice(0, 12)}...)`, false);
|
|
} else {
|
|
showToast(`moon-i2p error: ${(res && res.error) || "router offline?"}`, true);
|
|
}
|
|
} catch (err) {
|
|
showToast(`moon-i2p error: ${err}`, true);
|
|
}
|
|
},
|
|
true
|
|
);
|
|
})();
|