Background Services & Workers
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Background Services and Workers system in MarketSage acts as the central orchestration layer for the extension. It manages long-running tasks, facilitates communication between content scripts and the UI, and maintains the lifecycle of specialized analytics and monitoring services. These services operate independently of the browser's frontend, ensuring continuous marketplace analysis, threat detection, and data synchronization even when the popup is closed.
The architecture is built upon a Chrome Extension Manifest V3 service worker, which coordinates various core modules including social commerce analytics, real-time marketplace monitoring, and AI-driven shopping assistance. Sources: src/manifest.json, src/background/background.ts:1-10, src/service-worker.ts:1-5
Core Service Worker Architecture
MarketSage utilizes a service_worker defined in the manifest as the primary background entry point. This worker is responsible for installing static resources, caching AI models, and handling extension lifecycle events.
Extension Lifecycle Management
The background script listens for critical Chrome runtime events to ensure the environment is correctly initialized and updated.
- Installation/Update: Sets default settings (auto-analysis, notifications, price/security alerts) and clears the analysis cache. Sources: src/background/background.ts:6-21
- Tab Management: Monitors
chrome.tabs.onUpdatedto trigger page analysis when a user navigates to a supported marketplace. Sources: src/background/background.ts:24-28 - Message Orchestration: Acts as a central hub for messages from content scripts or the popup UI, delegating tasks to specific service managers. Sources: src/background/background.ts:101-105
The flow above illustrates how background events trigger initialization and reactive analysis. Sources: src/background/background.ts:24-85
Specialized Background Services
MarketSage implements several high-level services that run in the background context to provide continuous intelligence.
Social Commerce Analytics
This service provides advanced analytics for community engagement and viral content detection. It utilizes a BehaviorSubject to broadcast real-time metrics and maintains an interval-based collection cycle.
| Feature | Description |
|---|---|
| Real-time Collection | Metrics collected every 5 minutes (Engagement, Influence, Community, Commerce). |
| Virality Detection | Analyzes content with a virality score > 0.7 to identify trending products or reviews. |
| Community Health | Calculates a weighted health score based on engagement, quality, and moderation. |
Sources: src/core/social/SocialCommerceAnalytics.ts:316-324, src/core/social/SocialCommerceAnalytics.ts:401-415, src/core/social/SocialCommerceAnalytics.ts:503-518
Marketplace Monitoring Dashboard
This service manages the real-time monitoring of marketplace activities, including security threats and performance metrics.
The sequence above shows the periodic polling and alert propagation logic. Sources: src/core/monitoring/MarketplaceMonitoringDashboard.ts:153-176
Smart Shopping Assistant
The AI assistant processes natural language queries in the background, maintaining conversation contexts and extracting user intents (e.g., find_product, safety_check, price_inquiry). Sources: src/core/ai/SmartShoppingAssistant.ts:105-132
Resource and Model Caching
To ensure performance and offline capability, the service worker implements a caching strategy for static resources and machine learning models.
const STATIC_RESOURCES = [
'/popup.html',
'/popup.js',
'/content.js',
'/background.js'
];
const MODEL_URLS = [
'https://storage.marketsage.com/models/scam-detector-v1/model.json',
'https://storage.marketsage.com/models/seller-dna-v1/model.json'
];
Sources: src/service-worker.ts:4-15
Communication Protocol
The background service uses chrome.runtime.onMessage to handle requests from other parts of the extension.
| Message Type | Source | Purpose |
|---|---|---|
CHECK_PLATFORM | Background -> Content | Determines if the current URL is a supported Chinese marketplace. |
ANALYZE_CURRENT_PAGE | Background -> Content | Triggers DOM extraction and security analysis. |
DISCOVER_PRODUCTS | Popup -> Background | Initiates AI-driven product discovery based on user tier. |
Sources: src/background/background.ts:60-70, src/popup/components/scout/EnhancedScoutSection.tsx:112-120
Conclusion
Background Services & Workers form the backbone of the MarketSage extension, providing the necessary infrastructure for AI processing, real-time monitoring, and persistent data analysis. By leveraging Service Workers and specialized class-based services, the system maintains a high-performance environment that reacts to user navigation and system events without degrading the browser experience.