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 = {
|
||||
discordWebhook: "",
|
||||
checkIntervalMinutes: 1,
|
||||
checkIntervalSeconds: 15, // Now in SECONDS, not minutes!
|
||||
enabled: true,
|
||||
urls: [
|
||||
"https://www.pokemoncenter.com/category/tcg-cards?sort=relevance"
|
||||
@@ -16,37 +16,62 @@ const DEFAULT_CONFIG = {
|
||||
let knownProducts = {};
|
||||
let config = DEFAULT_CONFIG;
|
||||
let persistentTabId = null; // Keep tab open for faster refreshes
|
||||
let checkLoopRunning = false; // Prevent multiple loops
|
||||
let isChecking = false; // Prevent overlapping checks
|
||||
|
||||
// Initialize
|
||||
chrome.runtime.onInstalled.addListener(() => {
|
||||
console.log("Pokemon Stock Monitor installed");
|
||||
loadConfig();
|
||||
loadProducts();
|
||||
setupAlarm();
|
||||
loadConfig().then(() => {
|
||||
loadProducts();
|
||||
startCheckLoop();
|
||||
});
|
||||
});
|
||||
|
||||
// Load on startup
|
||||
chrome.runtime.onStartup.addListener(() => {
|
||||
loadConfig();
|
||||
loadProducts();
|
||||
setupAlarm();
|
||||
});
|
||||
|
||||
// Handle alarm
|
||||
chrome.alarms.onAlarm.addListener((alarm) => {
|
||||
if (alarm.name === "stockCheck") {
|
||||
runStockCheck();
|
||||
}
|
||||
});
|
||||
|
||||
// Setup periodic alarm
|
||||
function setupAlarm() {
|
||||
// Chrome minimum is 0.5 minutes (30 seconds) for packed extensions
|
||||
const interval = Math.max(0.5, config.checkIntervalMinutes);
|
||||
chrome.alarms.create("stockCheck", {
|
||||
periodInMinutes: interval
|
||||
loadConfig().then(() => {
|
||||
loadProducts();
|
||||
startCheckLoop();
|
||||
});
|
||||
console.log(`Alarm set for every ${interval} minute(s) (${interval * 60} seconds)`);
|
||||
});
|
||||
|
||||
// Start the check loop (uses setTimeout to bypass Chrome's 30-sec alarm minimum)
|
||||
function startCheckLoop() {
|
||||
if (checkLoopRunning) {
|
||||
console.log("Check loop already running");
|
||||
return;
|
||||
}
|
||||
checkLoopRunning = true;
|
||||
console.log(`Starting check loop: every ${config.checkIntervalSeconds} seconds`);
|
||||
scheduleNextCheck();
|
||||
}
|
||||
|
||||
function scheduleNextCheck() {
|
||||
if (!config.enabled || !checkLoopRunning) {
|
||||
checkLoopRunning = false;
|
||||
console.log("Check loop stopped");
|
||||
return;
|
||||
}
|
||||
|
||||
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
|
||||
@@ -61,7 +86,11 @@ async function loadConfig() {
|
||||
// Save config to storage
|
||||
async function saveConfig() {
|
||||
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
|
||||
@@ -381,9 +410,11 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
setTimeout(() => {
|
||||
loadConfig().then(() => {
|
||||
loadProducts().then(() => {
|
||||
if (config.enabled && config.discordWebhook) {
|
||||
if (config.enabled) {
|
||||
console.log(`Check interval: ${config.checkIntervalSeconds} seconds`);
|
||||
runStockCheck();
|
||||
startCheckLoop();
|
||||
}
|
||||
});
|
||||
});
|
||||
}, 5000);
|
||||
}, 3000);
|
||||
|
||||
Reference in New Issue
Block a user