Allow check intervals as low as 10 seconds
- Switch from Chrome alarms (30s min) to setTimeout loops - Config now uses checkIntervalSeconds instead of minutes - Default: 15 seconds, minimum: 10 seconds - Prevents overlapping checks with isChecking flag - Updated popup UI to show seconds Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const DEFAULT_CONFIG = {
|
const DEFAULT_CONFIG = {
|
||||||
discordWebhook: "",
|
discordWebhook: "",
|
||||||
checkIntervalMinutes: 1,
|
checkIntervalSeconds: 15, // Now in SECONDS, not minutes!
|
||||||
enabled: true,
|
enabled: true,
|
||||||
urls: [
|
urls: [
|
||||||
"https://www.pokemoncenter.com/category/tcg-cards?sort=relevance"
|
"https://www.pokemoncenter.com/category/tcg-cards?sort=relevance"
|
||||||
@@ -16,37 +16,62 @@ const DEFAULT_CONFIG = {
|
|||||||
let knownProducts = {};
|
let knownProducts = {};
|
||||||
let config = DEFAULT_CONFIG;
|
let config = DEFAULT_CONFIG;
|
||||||
let persistentTabId = null; // Keep tab open for faster refreshes
|
let persistentTabId = null; // Keep tab open for faster refreshes
|
||||||
|
let checkLoopRunning = false; // Prevent multiple loops
|
||||||
|
let isChecking = false; // Prevent overlapping checks
|
||||||
|
|
||||||
// Initialize
|
// Initialize
|
||||||
chrome.runtime.onInstalled.addListener(() => {
|
chrome.runtime.onInstalled.addListener(() => {
|
||||||
console.log("Pokemon Stock Monitor installed");
|
console.log("Pokemon Stock Monitor installed");
|
||||||
loadConfig();
|
loadConfig().then(() => {
|
||||||
loadProducts();
|
loadProducts();
|
||||||
setupAlarm();
|
startCheckLoop();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Load on startup
|
// Load on startup
|
||||||
chrome.runtime.onStartup.addListener(() => {
|
chrome.runtime.onStartup.addListener(() => {
|
||||||
loadConfig();
|
loadConfig().then(() => {
|
||||||
loadProducts();
|
loadProducts();
|
||||||
setupAlarm();
|
startCheckLoop();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle alarm
|
// Start the check loop (uses setTimeout to bypass Chrome's 30-sec alarm minimum)
|
||||||
chrome.alarms.onAlarm.addListener((alarm) => {
|
function startCheckLoop() {
|
||||||
if (alarm.name === "stockCheck") {
|
if (checkLoopRunning) {
|
||||||
runStockCheck();
|
console.log("Check loop already running");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
checkLoopRunning = true;
|
||||||
|
console.log(`Starting check loop: every ${config.checkIntervalSeconds} seconds`);
|
||||||
|
scheduleNextCheck();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// Setup periodic alarm
|
function scheduleNextCheck() {
|
||||||
function setupAlarm() {
|
if (!config.enabled || !checkLoopRunning) {
|
||||||
// Chrome minimum is 0.5 minutes (30 seconds) for packed extensions
|
checkLoopRunning = false;
|
||||||
const interval = Math.max(0.5, config.checkIntervalMinutes);
|
console.log("Check loop stopped");
|
||||||
chrome.alarms.create("stockCheck", {
|
return;
|
||||||
periodInMinutes: interval
|
}
|
||||||
});
|
|
||||||
console.log(`Alarm set for every ${interval} minute(s) (${interval * 60} seconds)`);
|
const intervalMs = Math.max(10, config.checkIntervalSeconds) * 1000; // Min 10 seconds
|
||||||
|
|
||||||
|
setTimeout(async () => {
|
||||||
|
if (!isChecking && config.enabled) {
|
||||||
|
isChecking = true;
|
||||||
|
try {
|
||||||
|
await runStockCheck();
|
||||||
|
} finally {
|
||||||
|
isChecking = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scheduleNextCheck();
|
||||||
|
}, intervalMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopCheckLoop() {
|
||||||
|
checkLoopRunning = false;
|
||||||
|
console.log("Check loop stopped");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load config from storage
|
// Load config from storage
|
||||||
@@ -61,7 +86,11 @@ async function loadConfig() {
|
|||||||
// Save config to storage
|
// Save config to storage
|
||||||
async function saveConfig() {
|
async function saveConfig() {
|
||||||
await chrome.storage.local.set({ config });
|
await chrome.storage.local.set({ config });
|
||||||
setupAlarm(); // Reset alarm with new interval
|
// Restart loop with new interval
|
||||||
|
stopCheckLoop();
|
||||||
|
if (config.enabled) {
|
||||||
|
startCheckLoop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load known products from storage
|
// Load known products from storage
|
||||||
@@ -381,9 +410,11 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
loadConfig().then(() => {
|
loadConfig().then(() => {
|
||||||
loadProducts().then(() => {
|
loadProducts().then(() => {
|
||||||
if (config.enabled && config.discordWebhook) {
|
if (config.enabled) {
|
||||||
|
console.log(`Check interval: ${config.checkIntervalSeconds} seconds`);
|
||||||
runStockCheck();
|
runStockCheck();
|
||||||
|
startCheckLoop();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}, 5000);
|
}, 3000);
|
||||||
|
|||||||
@@ -214,8 +214,9 @@
|
|||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div>
|
<div>
|
||||||
<label>Check Interval (min)</label>
|
<label>Check Interval (seconds)</label>
|
||||||
<input type="number" id="interval" min="1" max="60" value="1">
|
<input type="number" id="interval" min="10" max="300" value="15">
|
||||||
|
<small>Min 10 sec. Recommended: 15-30 sec</small>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||||||
document.getElementById("webhook").value = config.discordWebhook || "";
|
document.getElementById("webhook").value = config.discordWebhook || "";
|
||||||
document.getElementById("urls").value = (config.urls || []).join("\n");
|
document.getElementById("urls").value = (config.urls || []).join("\n");
|
||||||
document.getElementById("keywords").value = (config.keywords || []).join(", ");
|
document.getElementById("keywords").value = (config.keywords || []).join(", ");
|
||||||
document.getElementById("interval").value = config.checkIntervalMinutes || 1;
|
document.getElementById("interval").value = config.checkIntervalSeconds || 15;
|
||||||
document.getElementById("enabled").checked = config.enabled !== false;
|
document.getElementById("enabled").checked = config.enabled !== false;
|
||||||
document.getElementById("notifyNew").checked = config.notifyNewProducts !== false;
|
document.getElementById("notifyNew").checked = config.notifyNewProducts !== false;
|
||||||
document.getElementById("notifyRestock").checked = config.notifyRestocks !== false;
|
document.getElementById("notifyRestock").checked = config.notifyRestocks !== false;
|
||||||
@@ -27,7 +27,7 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||||||
discordWebhook: document.getElementById("webhook").value.trim(),
|
discordWebhook: document.getElementById("webhook").value.trim(),
|
||||||
urls: urlsText.split("\n").map(u => u.trim()).filter(u => u),
|
urls: urlsText.split("\n").map(u => u.trim()).filter(u => u),
|
||||||
keywords: keywordsText.split(",").map(k => k.trim()).filter(k => k),
|
keywords: keywordsText.split(",").map(k => k.trim()).filter(k => k),
|
||||||
checkIntervalMinutes: parseInt(document.getElementById("interval").value) || 1,
|
checkIntervalSeconds: parseInt(document.getElementById("interval").value) || 15,
|
||||||
enabled: document.getElementById("enabled").checked,
|
enabled: document.getElementById("enabled").checked,
|
||||||
notifyNewProducts: document.getElementById("notifyNew").checked,
|
notifyNewProducts: document.getElementById("notifyNew").checked,
|
||||||
notifyRestocks: document.getElementById("notifyRestock").checked
|
notifyRestocks: document.getElementById("notifyRestock").checked
|
||||||
|
|||||||
Reference in New Issue
Block a user