Platform Detection & Integration
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Platform Detection & Integration is a core architectural component of MarketSage designed to identify, categorize, and interface with various e-commerce marketplaces. The system enables the extension to dynamically adapt its data extraction and analysis logic based on the specific website the user is visiting. By providing a unified interface for disparate platforms, it allows higher-level services like Advanced Threat Detection and Smart Shopping Assistant to operate on normalized data regardless of the source marketplace.
The system primarily targets major Chinese marketplaces, including Temu, AliExpress, and DHGate, providing specialized configurations for each to ensure accurate scraping of product details, pricing, and seller information.
Sources: src/content/services/platformDetector.ts:7-14, src/manifest.json:11-20
Core Architecture
The integration architecture follows a provider-based pattern where a central detector identifies the platform and retrieves a specific configuration. This configuration contains the DOM selectors and API endpoints necessary for other services to interact with the page.
Platform Identification Flow
The detection process is triggered primarily during tab updates or initial page loads. The PlatformDetector analyzes the current window location to match it against a registry of supported base URLs.
The diagram shows the logic flow from a browser event to a specific platform configuration.
Sources: src/content/services/platformDetector.ts:89-112, src/background/background.ts:60-75
Platform Registry and Configuration
The system maintains a PLATFORM_CONFIGS registry. Each entry defines the structural identifiers (CSS selectors) required to locate product information on the page.
Supported Platforms
| Platform | Base URL | Price Selector | Title Selector | Seller Selector |
|---|---|---|---|---|
| TEMU | temu.com | .price-current, .product-price | .product-title, .item-title | .seller-name |
| AliExpress | aliexpress.com | .product-price-value | .product-title-text | .shop-name |
| DHGate | dhgate.com | .price, .price-now | .product-name | .seller-info |
Sources: src/content/services/platformDetector.ts:22-68, src/common/types/index.ts:2-7
Data Structures
The PlatformConfig interface ensures consistency across different integrations:
export interface PlatformConfig {
name: Platform;
baseUrl: string;
selectors: {
price: string;
title: string;
seller: string;
shipping: string;
description: string;
specifications: string;
};
apiEndpoints?: {
search?: string;
product?: string;
seller?: string;
};
}
Sources: src/common/types/index.ts:121-137
Integration with Extraction Services
Once a platform is detected, the ProductExtractor and PriceExtractor use the retrieved PlatformConfig to pull data from the DOM.
Extraction Sequence
The interaction between the background script and content services follows a request-response pattern via the Chrome Messaging API.
The sequence demonstrates how the background worker orchestrates platform detection before initiating data extraction.
Sources: src/background/background.ts:77-90, src/content/services/productExtractor.ts:31-50, tests/unit/services/platformDetector.test.ts:7-35
Extraction Logic
- Product ID Extraction: The
ProductExtractorattempts to find a unique ID via meta tags (product:id), URL parameters (?id=), or the URL pathname. - Currency Normalization: The
PriceExtractoranalyzes text nodes to identify currency symbols (e.g.,$,€,£) and maps them to standard ISO codes. If no symbol is found, it defaults to the platform's primary currency (usually USD). - Image Filtering: During extraction, placeholder images and empty strings are filtered out to ensure only relevant product gallery items are included.
Sources: src/content/services/productExtractor.ts:58-85, src/content/services/priceExtractor.test.ts:98-115, src/common/utils/dataProcessing.ts (referenced in tests/unit/utils/dataProcessing.test.ts:1-55)
Error Handling and Validation
The PlatformDetector utilizes the centralized ErrorHandler to log failures in detection or URL validation.
- URL Validation: The
validateUrlmethod safely checks if the browser's current URL contains the configuredbaseUrl. - Graceful Degradation: If a platform is marked as
UNKNOWN, the system returns a default empty configuration to prevent runtime crashes in dependent services. - Severity Mapping: Detection errors are typically logged with
ErrorSeverity.MEDIUMand categorized underErrorCategory.UIorErrorCategory.VALIDATION.
Sources: src/content/services/platformDetector.ts:113-130, src/content/services/productExtractor.ts:47-49
Conclusion
The Platform Detection & Integration module provides the necessary abstraction layer for MarketSage to function as a multi-marketplace assistant. By decoupling site-specific DOM structures from core analysis logic through the PlatformConfig registry, the system ensures high maintainability and easy extensibility for future marketplace support.