diff --git a/chrome-extension/background.js b/chrome-extension/background.js index 3410066..dbf2907 100644 --- a/chrome-extension/background.js +++ b/chrome-extension/background.js @@ -40,10 +40,12 @@ chrome.alarms.onAlarm.addListener((alarm) => { // 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: config.checkIntervalMinutes + periodInMinutes: interval }); - console.log(`Alarm set for every ${config.checkIntervalMinutes} minute(s)`); + console.log(`Alarm set for every ${interval} minute(s) (${interval * 60} seconds)`); } // Load config from storage @@ -87,6 +89,15 @@ async function runStockCheck() { for (const url of config.urls) { try { const products = await fetchAndParseProducts(url); + + // Alert if 0 products found - likely bot protection + if (products.length === 0) { + console.warn("WARNING: 0 products found - possible bot protection!"); + await sendWarningNotification("Bot Protection Detected", + "Found 0 products - you may need to manually verify you're human on PokemonCenter.com"); + continue; + } + const { newProducts, restockedProducts } = processProducts(products); // Send notifications @@ -147,7 +158,7 @@ async function fetchAndParseProducts(url) { // Give extra time for dynamic content to load console.log("Waiting for dynamic content..."); - await new Promise(r => setTimeout(r, 5000)); + await new Promise(r => setTimeout(r, 10000)); // 10 seconds for slow loads // Send message to content script to extract products let products = []; @@ -225,6 +236,39 @@ function processProducts(products) { return { newProducts, restockedProducts }; } +// Send warning notification (for bot protection, errors, etc.) +async function sendWarningNotification(title, message) { + if (!config.discordWebhook) { + console.log("No Discord webhook configured"); + return; + } + + const embed = { + title: `⚠️ ${title}`, + description: message, + color: 0xFF6600, // Orange for warnings + footer: { text: "Pokemon Stock Monitor (Chrome Extension)" }, + timestamp: new Date().toISOString() + }; + + try { + const response = await fetch(config.discordWebhook, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + content: "@everyone", + embeds: [embed] + }) + }); + + if (response.ok) { + console.log(`Warning notification sent: ${title}`); + } + } catch (error) { + console.error("Error sending warning notification:", error); + } +} + // Send Discord notification async function sendDiscordNotification(product, alertType) { if (!config.discordWebhook) {