- Add pure-MoonBit bittorrent package: bencode codec, metainfo/magnet parsing with path traversal checks, peer wire protocol (BEP 10/9), and I2P Kademlia DHT with KRPC and 54-byte compact nodes - Add SHA-1 primitive in crypto for torrent info hashes - Add router torrent API and engine, enabled with --torrent-api (default 127.0.0.1:4445) and --download-dir - Add Firefox WebExtension torrent manager with native messaging host and packed .xpi - Add scripts to build a standalone Firefox + router AppImage and install the native host - Add just targets (proxy, appimage, extension), README docs, and ignore .cache/ and dist/
72 lines
2.2 KiB
JavaScript
72 lines
2.2 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]") : null;
|
|
if (!anchor) return;
|
|
const href = anchor.getAttribute("href") || "";
|
|
let absUrl = "";
|
|
try {
|
|
absUrl = new URL(href, window.location.href).href;
|
|
} catch (_e) {
|
|
return;
|
|
}
|
|
const isMagnet = absUrl.toLowerCase().startsWith("magnet:");
|
|
const isTorrent = /\.torrent(\?.*)?$/i.test(absUrl);
|
|
if (!isMagnet && !isTorrent) return;
|
|
|
|
ev.preventDefault();
|
|
ev.stopPropagation();
|
|
try {
|
|
const res = await browser.runtime.sendMessage(
|
|
isMagnet
|
|
? { type: "addMagnet", uri: absUrl }
|
|
: { 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
|
|
);
|
|
})();
|