Add per site delays
This commit is contained in:
@ -30,6 +30,18 @@ function isBlacklisted(url, blacklist) {
|
|||||||
return blacklist.some((p) => matchPattern(url, p));
|
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();
|
let activeTabs = new Map();
|
||||||
|
|
||||||
// Load settings
|
// Load settings
|
||||||
@ -59,7 +71,7 @@ function updateIcon(state) {
|
|||||||
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
|
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
|
||||||
if (changeInfo.status !== "complete" || !tab.url) return;
|
if (changeInfo.status !== "complete" || !tab.url) return;
|
||||||
const settings = await getSettings();
|
const settings = await getSettings();
|
||||||
const { delay, blacklist, paused } = settings;
|
const { delay, blacklist, paused } = await getSettings();
|
||||||
|
|
||||||
if (paused || isBlacklisted(tab.url, blacklist)) return;
|
if (paused || isBlacklisted(tab.url, blacklist)) return;
|
||||||
if (tab.url.startsWith("moz-extension://")) 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);
|
const url = encodeURIComponent(tab.url);
|
||||||
updateIcon("wait");
|
updateIcon("wait");
|
||||||
|
|
||||||
|
if (activeTabs.has(tabId)) clearTimeout(activeTabs.get(tabId));
|
||||||
|
const siteDelay = getDelayForUrl(url, await getSettings());
|
||||||
|
|
||||||
if (activeTabs.has(tabId)) clearTimeout(activeTabs.get(tabId));
|
if (activeTabs.has(tabId)) clearTimeout(activeTabs.get(tabId));
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
fetch(`${SCROBBLE_ENDPOINT}${url}`).then(() => updateIcon("scrobbled"));
|
scrobbleStart(url, scrobbleBaseUrl);
|
||||||
}, delay * 1000);
|
updateIcon(tabId, "scrobbled");
|
||||||
|
setTimeout(() => updateIcon(tabId, "wait"), 3000);
|
||||||
|
}, siteDelay * 1000);
|
||||||
|
|
||||||
activeTabs.set(tabId, timeout);
|
activeTabs.set(tabId, timeout);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -8,6 +8,9 @@
|
|||||||
<h2>Life Scrobbler Settings</h2>
|
<h2>Life Scrobbler Settings</h2>
|
||||||
<label>Delay (seconds): <input id="delay" type="number" min="1" /></label>
|
<label>Delay (seconds): <input id="delay" type="number" min="1" /></label>
|
||||||
<br /><br />
|
<br /><br />
|
||||||
|
<label>Custom per-site delays (domain = seconds):</label><br />
|
||||||
|
<textarea id="siteDelays" rows="4" cols="40"></textarea>
|
||||||
|
<br /><br />
|
||||||
<label>Blacklist (one per line):</label><br />
|
<label>Blacklist (one per line):</label><br />
|
||||||
<textarea id="blacklist" rows="6" cols="40"></textarea>
|
<textarea id="blacklist" rows="6" cols="40"></textarea>
|
||||||
<br /><br />
|
<br /><br />
|
||||||
|
|||||||
11
options.js
11
options.js
@ -3,8 +3,17 @@ document.getElementById("save").addEventListener("click", save);
|
|||||||
|
|
||||||
const DEFAULT_SETTINGS = {
|
const DEFAULT_SETTINGS = {
|
||||||
delay: 7,
|
delay: 7,
|
||||||
blacklist: ["*.unbl.ink", "moz-extension://", "*.google.com", "gmail.com", "*.chatgpt.com", "*.ebay.com", "*.amazon.com"],
|
blacklist: [
|
||||||
|
"*.unbl.ink",
|
||||||
|
"moz-extension://",
|
||||||
|
"*.google.com",
|
||||||
|
"gmail.com",
|
||||||
|
"*.chatgpt.com",
|
||||||
|
"*.ebay.com",
|
||||||
|
"*.amazon.com",
|
||||||
|
],
|
||||||
paused: false,
|
paused: false,
|
||||||
|
siteDelays: { "readscomisconline.ru": 1 },
|
||||||
};
|
};
|
||||||
|
|
||||||
function restore() {
|
function restore() {
|
||||||
|
|||||||
Reference in New Issue
Block a user