// Load stats from storage
function updateStats() {
    chrome.storage.local.get(['filteredCount'], (result) => {
        const count = result.filteredCount || 0;
        document.getElementById('blockCount').innerText = count;
    });
}

// Handle Toggles
const toggles = document.querySelectorAll('input[type="checkbox"]');
toggles.forEach((toggle, index) => {
    const key = `toggle_${index}`;
    // Load saved state
    chrome.storage.local.get([key], (res) => {
        toggle.checked = res[key] !== false; // Default to true
    });
    // Save on change
    toggle.addEventListener('change', () => {
        chrome.storage.local.set({ [key]: toggle.checked });
    });
});

// Open Options Page
document.querySelector('.settings-btn').addEventListener('click', () => {
    if (typeof chrome !== 'undefined' && chrome.runtime && chrome.runtime.openOptionsPage) {
        chrome.runtime.openOptionsPage();
    } else {
        window.open('options.html', '_blank');
    }
});

// Update stats periodically
updateStats();
setInterval(updateStats, 1000);
