diff --git a/background.js b/background.js index 40ae118..5987118 100644 --- a/background.js +++ b/background.js @@ -30,6 +30,18 @@ function isBlacklisted(url, blacklist) { return blacklist.some((p) => matchPattern(url, p)); } +function getDelayForUrl(url, settings) { + try { + const u = new URL(url); + for (const [domain, customDelay] of Object.entries(settings.siteDelays)) { + if (u.hostname.includes(domain)) { + return customDelay; + } + } + } catch (err) {} + return settings.delay; +} + let activeTabs = new Map(); // Load settings @@ -59,7 +71,7 @@ function updateIcon(state) { browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { if (changeInfo.status !== "complete" || !tab.url) return; const settings = await getSettings(); - const { delay, blacklist, paused } = settings; + const { delay, blacklist, paused } = await getSettings(); if (paused || isBlacklisted(tab.url, blacklist)) return; if (tab.url.startsWith("moz-extension://")) return; @@ -67,10 +79,15 @@ browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { const url = encodeURIComponent(tab.url); updateIcon("wait"); + if (activeTabs.has(tabId)) clearTimeout(activeTabs.get(tabId)); + const siteDelay = getDelayForUrl(url, await getSettings()); + if (activeTabs.has(tabId)) clearTimeout(activeTabs.get(tabId)); const timeout = setTimeout(() => { - fetch(`${SCROBBLE_ENDPOINT}${url}`).then(() => updateIcon("scrobbled")); - }, delay * 1000); + scrobbleStart(url, scrobbleBaseUrl); + updateIcon(tabId, "scrobbled"); + setTimeout(() => updateIcon(tabId, "wait"), 3000); + }, siteDelay * 1000); activeTabs.set(tabId, timeout); }); diff --git a/options.html b/options.html index af3d204..779f6f9 100644 --- a/options.html +++ b/options.html @@ -8,6 +8,9 @@