A provider-agnostic JavaScript logging and traffic-observability library for Chrome extensions that normalizes domain telemetry, enriches API responses, and guarantees resilient fallback behavior.
Note
This library is currently shipped as a Chrome Manifest V3 extension package and can be consumed as a modular logging/analytics runtime in extension-based architectures.
- Table of Contents
- Features
- Tech Stack & Architecture
- Getting Started
- Testing
- Deployment
- Usage
- Configuration
- License
- Support the Project
- Structured, provider-driven logging pipeline with configurable
baseUrl, endpoints, and auth headers. - Domain canonicalization primitives (
normalizeDomain,getHostFromUrl) to prevent malformed telemetry dimensions. - Asynchronous dual-stream retrieval model for visits and geography channels.
- Resilient failover semantics:
- explicit mock mode (
useMock) - automatic mock failover when API key is absent
- automatic fallback envelope on transport and API errors
- explicit mock mode (
- Runtime response harmonization for multi-provider schemas (
visits | totalVisits | value,topCountries | countries). - Message-based worker orchestration (
ANALYZE_DOMAIN) that decouples UI rendering from network execution. - Persistent configuration surface backed by
chrome.storage.sync. - Extension-native UX integration through popup and options interfaces.
- Deterministic demo capability for onboarding, QA smoke tests, and API quota outage scenarios.
Important
The fallback-first design ensures logs and traffic insights remain available even during provider outages or misconfiguration.
- JavaScript (ES Modules)
- Chrome Extension APIs (Manifest V3)
- Service Worker background runtime
- HTML/CSS popup and options interfaces
- RapidAPI-compatible external provider integration
chrome.runtimefor message dispatch and request orchestrationchrome.storage.syncfor persisted runtime configurationchrome.tabsfor active-tab domain extractionfetchfor provider API transport
Traffic-Stats-Analyzer/
ββ background/
β ββ service-worker.js
ββ popup/
β ββ popup.html
β ββ popup.css
β ββ popup.js
ββ options/
β ββ options.html
β ββ options.css
β ββ options.js
ββ shared/
β ββ constants.js
β ββ domain.js
β ββ mock-data.js
ββ styles/
β ββ base.css
ββ manifest.json
ββ API_REFERENCE.md
ββ CONTRIBUTING.md
ββ LICENSE
- Provider schema abstraction: Provider metadata is externalized to configuration and not hardcoded into UI logic.
- Canonicalization boundary: Domain normalization happens before outbound requests to preserve metric consistency.
- Fault-tolerant envelopes: Every call resolves with a predictable response shape, even on error paths.
- Parallel fetch strategy: Visits and geography are collected concurrently to reduce end-to-end latency.
- UI/worker separation: UI modules focus on interaction and rendering, while networking stays in the service worker.
flowchart LR
A[Popup or Options UI] --> B[chrome.runtime.sendMessage]
B --> C[Service Worker analyzeDomain]
C --> D[normalizeDomain]
C --> E[buildHeaders]
E --> F[Visits Endpoint]
E --> G[Top Countries Endpoint]
F --> H[Envelope Normalization]
G --> H
H --> I[Success: api source]
H --> J[Failure: fallback-mock source]
I --> K[UI Rendering and Logging]
J --> K
Tip
When adding additional providers, preserve envelope contracts first and adapt provider fields second.
- Google Chrome (latest stable)
- Git
- Optional: valid RapidAPI key for a Similarweb-compatible provider
- Optional local tooling for QA:
nodeandnpm(for lint/packaging checks)
git clone https://github.com/<your-org>/Traffic-Stats-Analyzer.git
cd Traffic-Stats-Analyzer# Open Chrome extension manager
# chrome://extensions- Enable Developer Mode.
- Click Load unpacked.
- Select the repository root.
- Open popup and configure provider credentials.
- Analyze the active tab domain or enter a custom domain.
Warning
Never hardcode production API keys in source files. Use runtime options storage only.
This repository currently has no committed automated unit/integration test suite, so validation is done via linting and deterministic manual checks.
npx --yes web-ext lint --source-dir .npm run lint
npm run test
npm run test:integration- Verify domain auto-detection on an active browser tab.
- Enable
useMockand confirm stable demo payload rendering. - Disable
useMock, set API key, and verify live provider output. - Intentionally break endpoint URL and confirm
fallback-mockhandling. - Save custom provider values and reopen popup to validate persistence.
Caution
A green manual run does not guarantee provider contract compatibility across all third-party API versions.
- Bump
manifest.jsonversion for release. - Validate permission scope and host patterns.
- Confirm provider endpoints and headers in options defaults.
- Run extension linting and full manual validation matrix.
- Create release archive excluding VCS metadata.
zip -r traffic-stats-analyzer.zip . -x '.git/*'- Trigger release workflow on tags (
v*). - Run
web-ext lintin CI before packaging. - Publish zip artifact for release review and store upload.
name: release-extension
on:
push:
tags: ["v*"]
jobs:
package:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npx --yes web-ext lint --source-dir .
- run: zip -r traffic-stats-analyzer.zip . -x '.git/*'// Send a domain analysis request to the background logging pipeline.
const response = await chrome.runtime.sendMessage({
type: 'ANALYZE_DOMAIN',
domain: 'example.com' // Input can be raw URL or domain-like value.
});
// Normalize visits from multi-provider response contracts.
const visits = response.visits?.data?.visits
?? response.visits?.data?.totalVisits
?? response.visits?.data?.value
?? 0;
console.log('Domain:', response.domain);
console.log('Visits:', visits);
console.log('Top countries:', response.geography?.data?.topCountries || []);import { normalizeDomain, getHostFromUrl } from './shared/domain.js';
const d1 = normalizeDomain('https://www.example.com/blog/post'); // example.com
const d2 = getHostFromUrl('https://news.example.com/path'); // news.example.com
console.log(d1, d2);provider.name: Logical provider label.provider.baseUrl: Provider API base URL.provider.endpoints.visits: Visits endpoint prefix.provider.endpoints.geography: Geography endpoint prefix.provider.headers.X-RapidAPI-Key: Placeholder token expanded withprovider.apiKey.provider.headers.X-RapidAPI-Host: Provider host header.provider.apiKey: Runtime API key entered by user.useMock: Boolean switch for demo/fallback behavior.
{
"provider": {
"name": "RapidAPI Similarweb",
"baseUrl": "https://similarweb12.p.rapidapi.com",
"endpoints": {
"visits": "/v1/website/visits?domain=",
"geography": "/v1/website/top-countries?domain="
},
"headers": {
"X-RapidAPI-Key": "__REPLACE__",
"X-RapidAPI-Host": "similarweb12.p.rapidapi.com"
},
"apiKey": ""
},
"useMock": true
}This project does not currently ship with .env ingestion or CLI startup flags. Configuration is runtime-driven through Chrome storage and the options UI.
If desired, CI environments can inject provider defaults by generating a patched shared/constants.js during build-time release workflows.
Licensed under the Apache License 2.0. See LICENSE for full legal terms.
If you find this tool useful, consider leaving a star on GitHub or supporting the author directly.