Skip to content

Alternative triggers for closing the site #3

@seichris

Description

@seichris

Alternative triggers for closing the site

Right now we got a 5-second analysis timeout configured. Instead of hardcoding the timeout we could trigger closing the site through:

1. Network Activity Monitoring

Stop when network activity dies down:

// Track recent network activity
let lastNetworkActivity = Date.now();
let activityCheckTimer = null;

const checkNetworkActivity = () => {
    const timeSinceLastActivity = Date.now() - lastNetworkActivity;
    if (timeSinceLastActivity > 2000) { // 2 seconds of inactivity
        console.log('🔕 Network activity ceased, stopping analysis');
        stopAnalysis();
    }
};

// In the network event handlers, update activity timestamp
if (method === 'Network.requestWillBeSent' || method === 'Network.responseReceived') {
    lastNetworkActivity = Date.now();
    // Reset/start activity monitoring
    if (activityCheckTimer) clearInterval(activityCheckTimer);
    activityCheckTimer = setInterval(checkNetworkActivity, 1000);
}

2. Request Completion Tracking

Stop when all active requests have completed:

let pendingRequests = new Set();

// Track pending requests
if (method === 'Network.requestWillBeSent') {
    pendingRequests.add(params.requestId);
}

if (method === 'Network.responseReceived' || method === 'Network.loadingFinished') {
    pendingRequests.delete(params.requestId);
    if (pendingRequests.size === 0) {
        console.log('✅ All requests completed, stopping analysis');
        setTimeout(() => stopAnalysis(), 1000); // Small delay to catch any final requests
    }
}

3. Content-Based Triggers

Stop when specific content appears on the page:

// In the injected content script
const checkForTargetContent = () => {
    // Look for specific elements or content
    const targetSelectors = [
        '[data-testid="subscription-info"]',
        '.membership-tier',
        '.account-details',
        '[class*="subscription"]'
    ];
    
    const foundContent = targetSelectors.some(selector => 
        document.querySelector(selector)
    );
    
    if (foundContent) {
        chrome.runtime.sendMessage({
            type: 'targetContentFound',
            data: { found: true }
        });
    }
};

// In background.js
if (request.type === 'targetContentFound') {
    console.log('🎯 Target content found, stopping analysis');
    stopAnalysis();
}

4. User-Configurable Timeout

Allow users to set their own timeout:

// In config.js
export const NETWORK_CONFIG = {
    ANALYSIS_TIMEOUT_MS: null, // Will be set from user preferences
    MIN_TIMEOUT_MS: 2000,
    MAX_TIMEOUT_MS: 30000,
    DEFAULT_TIMEOUT_MS: 5000
};

// Load user preference
const getAnalysisTimeout = async () => {
    const result = await chrome.storage.sync.get(['analysisTimeout']);
    return result.analysisTimeout || NETWORK_CONFIG.DEFAULT_TIMEOUT_MS;
};

5. Request Count Threshold

Stop after capturing a certain number of relevant requests:

// In config.js
export const NETWORK_CONFIG = {
    MAX_REQUESTS_TO_CAPTURE: 50,
    MIN_REQUESTS_BEFORE_STOP: 5
};

// In background.js
if (requestsMap.size >= NETWORK_CONFIG.MAX_REQUESTS_TO_CAPTURE) {
    console.log(`📊 Captured ${requestsMap.size} requests, stopping analysis`);
    stopAnalysis();
}

6. Smart Timeout Based on Activity

Dynamically adjust timeout based on ongoing network activity:

let dynamicTimeout = NETWORK_CONFIG.ANALYSIS_TIMEOUT_MS;
let extensionCount = 0;

// Extend timeout if there's ongoing activity
if (method === 'Network.requestWillBeSent') {
    if (extensionCount < 3) { // Max 3 extensions
        dynamicTimeout += 2000; // Add 2 seconds
        extensionCount++;
        console.log(`⏰ Extended timeout to ${dynamicTimeout/1000}s due to activity`);
        
        // Reset the timer
        if (analysisTimer) clearTimeout(analysisTimer);
        analysisTimer = setTimeout(() => stopAnalysis(), dynamicTimeout);
    }
}

7. Manual Stop Button

Add a user-controlled stop mechanism:

// Add to manifest.json permissions
"commands": {
    "stop-analysis": {
        "suggested_key": {
            "default": "Ctrl+Shift+S"
        },
        "description": "Stop network analysis"
    }
}

// In background.js
chrome.commands.onCommand.addListener((command) => {
    if (command === 'stop-analysis' && tabId) {
        console.log('🛑 Manual stop requested');
        stopAnalysis();
    }
});

8. Error-Based Triggers

Stop if critical requests fail or return certain status codes:

if (method === 'Network.responseReceived') {
    if (params.response.status === 403 || params.response.status === 401) {
        console.log(`🚫 Access denied (${params.response.status}), stopping analysis`);
        stopAnalysis();
    }
}

Recommended Approach

I'd suggest combining multiple triggers:

  1. Primary: Network activity monitoring (stop after 2-3 seconds of inactivity)
  2. Fallback: Maximum timeout (configurable, default 10-15 seconds)
  3. Early termination: Request count threshold or specific content detection
  4. User control: Manual stop option

This would make the extension more intelligent and responsive to different types of sites while still providing safety bounds.

// Track recent network activity
let lastNetworkActivity = Date.now();
let activityCheckTimer = null;

const checkNetworkActivity = () => {
    const timeSinceLastActivity = Date.now() - lastNetworkActivity;
    if (timeSinceLastActivity > 2000) { // 2 seconds of inactivity
        console.log('🔕 Network activity ceased, stopping analysis');
        stopAnalysis();
    }
};

// In the network event handlers, update activity timestamp
if (method === 'Network.requestWillBeSent' || method === 'Network.responseReceived') {
    lastNetworkActivity = Date.now();
    // Reset/start activity monitoring
    if (activityCheckTimer) clearInterval(activityCheckTimer);
    activityCheckTimer = setInterval(checkNetworkActivity, 1000);
}
let pendingRequests = new Set();

// Track pending requests
if (method === 'Network.requestWillBeSent') {
    pendingRequests.add(params.requestId);
}

if (method === 'Network.responseReceived' || method === 'Network.loadingFinished') {
    pendingRequests.delete(params.requestId);
    if (pendingRequests.size === 0) {
        console.log('✅ All requests completed, stopping analysis');
        setTimeout(() => stopAnalysis(), 1000); // Small delay to catch any final requests
    }
}
// In the injected content script
const checkForTargetContent = () => {
    // Look for specific elements or content
    const targetSelectors = [
        '[data-testid="subscription-info"]',
        '.membership-tier',
        '.account-details',
        '[class*="subscription"]'
    ];
    
    const foundContent = targetSelectors.some(selector => 
        document.querySelector(selector)
    );
    
    if (foundContent) {
        chrome.runtime.sendMessage({
            type: 'targetContentFound',
            data: { found: true }
        });
    }
};

// In background.js
if (request.type === 'targetContentFound') {
    console.log('🎯 Target content found, stopping analysis');
    stopAnalysis();
}
// In config.js
export const NETWORK_CONFIG = {
    ANALYSIS_TIMEOUT_MS: null, // Will be set from user preferences
    MIN_TIMEOUT_MS: 2000,
    MAX_TIMEOUT_MS: 30000,
    DEFAULT_TIMEOUT_MS: 5000
};

// Load user preference
const getAnalysisTimeout = async () => {
    const result = await chrome.storage.sync.get(['analysisTimeout']);
    return result.analysisTimeout || NETWORK_CONFIG.DEFAULT_TIMEOUT_MS;
};
// In config.js
export const NETWORK_CONFIG = {
    MAX_REQUESTS_TO_CAPTURE: 50,
    MIN_REQUESTS_BEFORE_STOP: 5
};

// In background.js
if (requestsMap.size >= NETWORK_CONFIG.MAX_REQUESTS_TO_CAPTURE) {
    console.log(`📊 Captured ${requestsMap.size} requests, stopping analysis`);
    stopAnalysis();
}
let dynamicTimeout = NETWORK_CONFIG.ANALYSIS_TIMEOUT_MS;
let extensionCount = 0;

// Extend timeout if there's ongoing activity
if (method === 'Network.requestWillBeSent') {
    if (extensionCount < 3) { // Max 3 extensions
        dynamicTimeout += 2000; // Add 2 seconds
        extensionCount++;
        console.log(`⏰ Extended timeout to ${dynamicTimeout/1000}s due to activity`);
        
        // Reset the timer
        if (analysisTimer) clearTimeout(analysisTimer);
        analysisTimer = setTimeout(() => stopAnalysis(), dynamicTimeout);
    }
}
// Add to manifest.json permissions
"commands": {
    "stop-analysis": {
        "suggested_key": {
            "default": "Ctrl+Shift+S"
        },
        "description": "Stop network analysis"
    }
}

// In background.js
chrome.commands.onCommand.addListener((command) => {
    if (command === 'stop-analysis' && tabId) {
        console.log('🛑 Manual stop requested');
        stopAnalysis();
    }
});
if (method === 'Network.responseReceived') {
    if (params.response.status === 403 || params.response.status === 401) {
        console.log(`🚫 Access denied (${params.response.status}), stopping analysis`);
        stopAnalysis();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions