61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
// background.js — luistert naar berichten van content.js en stuurt NZB naar SABnzbd
|
|
|
|
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|
if (message.type === "SEND_NZB") {
|
|
handleNzbUrl(message.url).then(sendResponse);
|
|
return true; // async response
|
|
}
|
|
});
|
|
|
|
async function handleNzbUrl(nzbUrl) {
|
|
const config = await chrome.storage.sync.get(["sabHost", "sabApiKey", "sabCategory"]);
|
|
|
|
const { sabHost, sabApiKey, sabCategory } = config;
|
|
|
|
if (!sabHost || !sabApiKey) {
|
|
showNotification("❌ Configuratie ontbreekt", "Stel de SABnzbd host en API-sleutel in via de extensie-popup.");
|
|
return { success: false, error: "Geen configuratie" };
|
|
}
|
|
|
|
// Bouw de SABnzbd API URL
|
|
// SABnzbd ondersteunt 'addurl' om een externe NZB-URL toe te voegen
|
|
const host = sabHost.replace(/\/$/, ""); // verwijder trailing slash
|
|
const category = sabCategory || "Default";
|
|
|
|
const params = new URLSearchParams({
|
|
output: "json",
|
|
apikey: sabApiKey,
|
|
mode: "addurl",
|
|
name: nzbUrl,
|
|
cat: category
|
|
});
|
|
|
|
const apiUrl = `${host}/api?${params.toString()}`;
|
|
|
|
try {
|
|
const response = await fetch(apiUrl, { method: "GET" });
|
|
const data = await response.json();
|
|
|
|
if (data.status === true) {
|
|
showNotification("✅ NZB toegevoegd!", "De download is toegevoegd aan SABnzbd.");
|
|
return { success: true };
|
|
} else {
|
|
const errMsg = data.error || "Onbekende fout van SABnzbd";
|
|
showNotification("❌ Fout van SABnzbd", errMsg);
|
|
return { success: false, error: errMsg };
|
|
}
|
|
} catch (err) {
|
|
const msg = `Kan SABnzbd niet bereiken: ${err.message}`;
|
|
showNotification("❌ Verbindingsfout", msg);
|
|
return { success: false, error: msg };
|
|
}
|
|
}
|
|
|
|
function showNotification(title, message) {
|
|
chrome.notifications.create({
|
|
type: "basic",
|
|
iconUrl: "icons/icon48.png",
|
|
title,
|
|
message
|
|
});
|
|
}
|