Site Manager
window.onload = () => {
// Hent gemt API-nøgle og keyword og indsæt i felter
document.getElementById("apiKey").value = localStorage.getItem("datafeedr_api_key") || "";
document.getElementById("defaultKeyword").value = localStorage.getItem("default_keyword") || "";
const select = document.getElementById("siteSelect");
const websiteSelect = document.getElementById("websiteSelect");
sites.forEach(site => {
const option1 = document.createElement("option");
option1.value = site;
option1.textContent = site;
select.appendChild(option1);
const option2 = document.createElement("option");
option2.value = site;
option2.textContent = site;
websiteSelect.appendChild(option2);
});
renderSiteList();
};
// Hent dynamisk liste fra localStorage, ellers brug fallback
let sites = JSON.parse(localStorage.getItem("site_list")) || [];
// Ryd dropdowns først
select.innerHTML = "";
websiteSelect.innerHTML = "";
// Tilføj sites til begge dropdowns
sites.forEach(site => {
const option1 = document.createElement("option");
option1.value = site;
option1.textContent = site;
select.appendChild(option1);
const option2 = document.createElement("option");
option2.value = site;
option2.textContent = site;
websiteSelect.appendChild(option2);
});
// Vis gemte bokse
renderSiteList();
};
function saveSiteData() {
const site = document.getElementById("siteSelect").value;
const merchant = document.getElementById("merchantInput").value;
const network = document.getElementById("networkInput").value;
localStorage.setItem(`site_${site}_merchant`, merchant);
localStorage.setItem(`site_${site}_network`, network);
document.getElementById("merchantInput").value = "";
document.getElementById("networkInput").value = "";
renderSiteList();
}
function saveAPISettings() {
const key = document.getElementById("apiKey").value;
const keyword = document.getElementById("defaultKeyword").value;
localStorage.setItem("datafeedr_api_key", key);
localStorage.setItem("default_keyword", keyword);
alert("API-data gemt!");
}
function renderSiteList() {
const container = document.getElementById("siteList");
container.innerHTML = "";
sites.forEach(site => {
const merchant = localStorage.getItem(`site_${site}_merchant`) || "";
const network = localStorage.getItem(`site_${site}_network`) || "";
if (!merchant && !network) return;
let merchantCount = 0;
try {
const matches = merchant.match(/s:\d+:"\d+"/g);
merchantCount = matches ? matches.length : 0;
} catch {
merchantCount = 0;
}
const networkCount = network.split(",").filter(x => x.trim() !== "").length;
const box = document.createElement("div");
box.className = "site-box";
box.style = "background:#fff; border-radius:8px; padding:15px; margin:10px 0; box-shadow:0 1px 3px rgba(0,0,0,0.1); position:relative;";
box.innerHTML = `
${site}
Active
Merchants: ${merchantCount} configured
Networks: ${networkCount} configured
`;
container.appendChild(box);
});
}
function toggleBox(btn) {
const box = btn.closest(".site-box").querySelector(".site-box-body");
box.style.display = box.style.display === "none" || box.style.display === "" ? "block" : "none";
}
function deleteSite(site) {
localStorage.removeItem(`site_${site}_merchant`);
localStorage.removeItem(`site_${site}_network`);
renderSiteList();
}
function exportCSV() {
const rows = document.querySelectorAll("#productList tr");
let csvContent = "Produktnavn,Forhandler,Bedst i Test,Bedst til Prisen,Bedste Køb\n";
rows.forEach(row => {
const checked = row.querySelector(".product-check")?.checked;
if (!checked) return;
const name = row.querySelector("td:nth-child(2) div").innerText.split('\n')[0];
const merchant = row.querySelector("td:nth-child(5)").innerText.split('\n')[0];
const awards = row.querySelectorAll(".award");
const bestInTest = awards[0].classList.contains("active") ? "âś…" : "";
const bestValue = awards[1].classList.contains("active") ? "âś…" : "";
const bestBuy = awards[2].classList.contains("active") ? "âś…" : "";
csvContent += `${name},${merchant},${bestInTest},${bestValue},${bestBuy}\n`;
});
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("download", "vinder_produkter.csv");
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function addNewSite() {
const newSiteName = document.getElementById("newSiteName").value.trim();
if (!newSiteName) {
alert("Skriv et navn pĂĄ det nye site.");
return;
}
if (sites.includes(newSiteName)) {
alert("Dette site findes allerede.");
return;
}
sites.push(newSiteName);
localStorage.setItem("sites", JSON.stringify(sites));
document.getElementById("newSiteName").value = "";
const siteSelect = document.getElementById("siteSelect");
const websiteSelect = document.getElementById("websiteSelect");
const opt1 = document.createElement("option");
opt1.value = newSiteName;
opt1.textContent = newSiteName;
siteSelect.appendChild(opt1);
const opt2 = document.createElement("option");
opt2.value = newSiteName;
opt2.textContent = newSiteName;
websiteSelect.appendChild(opt2);
renderSiteList();
}