81 lines
2.5 KiB
JavaScript
81 lines
2.5 KiB
JavaScript
// popup.js — instellingen opslaan en verbinding testen
|
|
|
|
const sabHost = document.getElementById("sabHost");
|
|
const sabApiKey = document.getElementById("sabApiKey");
|
|
const sabCategory = document.getElementById("sabCategory");
|
|
const saveBtn = document.getElementById("saveBtn");
|
|
const testBtn = document.getElementById("testBtn");
|
|
const statusEl = document.getElementById("status");
|
|
const statusDot = document.getElementById("statusDot");
|
|
const footerText = document.getElementById("footerText");
|
|
|
|
// Laad opgeslagen instellingen
|
|
chrome.storage.sync.get(["sabHost", "sabApiKey", "sabCategory"], (config) => {
|
|
if (config.sabHost) sabHost.value = config.sabHost;
|
|
if (config.sabApiKey) sabApiKey.value = config.sabApiKey;
|
|
if (config.sabCategory) sabCategory.value = config.sabCategory;
|
|
|
|
updateFooter(!!config.sabHost && !!config.sabApiKey);
|
|
});
|
|
|
|
function updateFooter(configured) {
|
|
if (configured) {
|
|
statusDot.classList.add("active");
|
|
footerText.textContent = "Geconfigureerd — klaar om NZB-links te onderscheppen";
|
|
} else {
|
|
statusDot.classList.remove("active");
|
|
footerText.textContent = "Nog niet geconfigureerd";
|
|
}
|
|
}
|
|
|
|
function showStatus(message, isError = false) {
|
|
statusEl.textContent = message;
|
|
statusEl.className = isError ? "error" : "success";
|
|
}
|
|
|
|
saveBtn.addEventListener("click", () => {
|
|
const host = sabHost.value.trim();
|
|
const key = sabApiKey.value.trim();
|
|
const cat = sabCategory.value.trim();
|
|
|
|
if (!host || !key) {
|
|
showStatus("⚠️ Vul de host én API-sleutel in.", true);
|
|
return;
|
|
}
|
|
|
|
chrome.storage.sync.set({ sabHost: host, sabApiKey: key, sabCategory: cat }, () => {
|
|
showStatus("✅ Instellingen opgeslagen!");
|
|
updateFooter(true);
|
|
});
|
|
});
|
|
|
|
testBtn.addEventListener("click", async () => {
|
|
const host = sabHost.value.trim();
|
|
const key = sabApiKey.value.trim();
|
|
|
|
if (!host || !key) {
|
|
showStatus("⚠️ Vul eerst de host en API-sleutel in.", true);
|
|
return;
|
|
}
|
|
|
|
testBtn.textContent = "⏳ Bezig...";
|
|
testBtn.disabled = true;
|
|
|
|
try {
|
|
const cleanHost = host.replace(/\/$/, "");
|
|
const url = `${cleanHost}/api?output=json&apikey=${key}&mode=version`;
|
|
const response = await fetch(url);
|
|
const data = await response.json();
|
|
|
|
if (data.version) {
|
|
showStatus(`✅ Verbinding OK — SABnzbd versie ${data.version}`);
|
|
} else {
|
|
showStatus("❌ Reactie ontvangen maar geen versie. Controleer je API-sleutel.", true);
|
|
}
|
|
} catch (err) {
|
|
showStatus(`❌ Verbinding mislukt: ${err.message}`, true);
|
|
}
|
|
|
|
testBtn.textContent = "🔌 Test verbinding";
|
|
testBtn.disabled = false;
|
|
});
|