- NTCP2: queue outbound data frames and flush them without blocking (send_blocks_nb, send_i2np_nb, flush_nonblocking) - NTCP2: bound write_all with a deadline and set TCP keepalive and user timeout on peer sockets - Firefox extension 0.1.3: save settings as a patch, keep unsaved form fields from being overwritten on refresh, add timeouts to router API calls, and honor the interceptTorrents toggle in the content script
104 lines
3.1 KiB
JavaScript
104 lines
3.1 KiB
JavaScript
"use strict";
|
|
|
|
(function () {
|
|
let interceptTorrents = true;
|
|
if (browser.storage && browser.storage.local) {
|
|
browser.storage.local
|
|
.get({ interceptTorrents: true })
|
|
.then((res) => {
|
|
interceptTorrents = !!res.interceptTorrents;
|
|
})
|
|
.catch(() => {});
|
|
}
|
|
if (browser.storage && browser.storage.onChanged) {
|
|
browser.storage.onChanged.addListener((changes, area) => {
|
|
if (area === "local" && Object.prototype.hasOwnProperty.call(changes, "interceptTorrents")) {
|
|
interceptTorrents = !!changes.interceptTorrents.newValue;
|
|
}
|
|
});
|
|
}
|
|
|
|
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 (!interceptTorrents) {
|
|
return;
|
|
}
|
|
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
|
|
);
|
|
})();
|