A powerful, colorized logging system with:
- Multiple log levels: debug, info, warn, error
- Context tagging: background, panel, devtools, storage, etc.
- Colorized output: Different colors for different contexts
- Filtering: Can filter by level and context
- Grouping: Collapse related logs
- Timing: Performance measurement
Key logs added:
- ✅ Service worker lifecycle (load, init)
- ✅ webRequest listener registration with endpoints
- ✅ Request interception (detailed flow with collapsible groups)
- ✅ Request body decoding
- ✅ Payload parsing
- ✅ Provider detection
- ✅ Event extraction
- ✅ Storage operations (memory + chrome.storage)
- ✅ Message broadcasting to listeners
- ✅ Message handling (GET_EVENTS, CLEAR_EVENTS, etc.)
- ✅ Storage restoration on startup
This was the missing piece! The panel wasn't syncing with background script.
The hook:
- Fetches initial events from background on mount
- Listens for
EVENTS_CAPTUREDmessages - Filters events by tab ID
- Adds events to Zustand store
- Comprehensive logging of the sync process
- Added logging for mount/unmount
- Added
useEventSynchook to sync with background - Added clear handler that syncs with background storage
- Logs event count changes
- Logs store creation/reuse
- Logs event additions
- Logs event clearing
- Logs store initialization
- Logs panel creation
- Logs inspected tab ID
- Logs React rendering
Background Script DevTools Panel
↓ ↓
Captures events Opens empty panel
↓ ↓
Stores in: Stores in:
storage.local['events'] storage.local['tab-123']
↓ ↓
Sends message ❌ NOT LISTENING
↓ ↓
❌ Nobody receives Shows 0 events
Background Script DevTools Panel
↓ ↓
Captures events Opens panel
↓ useEventSync starts
Stores in: ↓
storage.local['events'] ✅ Fetches initial events
↓ ✅ Registers message listener
Sends message ↓
↓ ✅ Receives message
✅ Message delivered ────────→ ✅ Adds to store
✅ Shows events
yarn build:chrome- Go to
chrome://extensions/ - Find "Segment Analytics X-Ray"
- Click reload button
- In
chrome://extensions/ - Find "Segment Analytics X-Ray"
- Click "service worker" link (blue text)
- Console will open showing background logs
You should see:
[segment-analytics-x-ray] [background] 🚀 Background service worker loaded
[segment-analytics-x-ray] [background] 🔧 Initializing background script...
[segment-analytics-x-ray] [background] 🔄 Restoring events from storage...
[segment-analytics-x-ray] [background] ✅ Restored X total events across Y tab(s)
[segment-analytics-x-ray] [background] 📡 webRequest listener registered for endpoints: Array(4) [ ... ]
[segment-analytics-x-ray] [background] ✅ Background script initialization complete
Good test sites:
- segment.com
- Any site using Segment analytics (check Network tab for api.segment.io requests)
Background console should show:
[segment-analytics-x-ray] [background] 🎯 Request intercepted (tabId: 123)
URL: https://api.segment.io/v1/t
Body decoded (1234 chars)
Provider: segment
[segment-analytics-x-ray] [background] ✅ Captured 2 event(s) from segment: (2) ["Page View", "Click"]
[segment-analytics-x-ray] [background] 💾 Stored 2 event(s) in memory (total: 2 for tab 123)
[segment-analytics-x-ray] [background] 💾 Persisted 2 event(s) to storage.local['events'][123]
[segment-analytics-x-ray] [background] 📤 Sending EVENTS_CAPTURED message (tabId: 123, 2 event(s))
- Open Chrome DevTools (F12 or Cmd+Option+I)
- Click "Segment Analytics X-Ray" tab
- Panel should show events
- With the Segment Analytics X-Ray panel open
- Right-click anywhere in the panel
- Click "Inspect"
- OR: Press
Cmd+Option+I(Mac) /Ctrl+Shift+I(Windows) while panel is focused
Panel console should show:
[segment-analytics-x-ray] [devtools] 🔧 DevTools script loading...
[segment-analytics-x-ray] [devtools] Inspected tab ID: 123
[segment-analytics-x-ray] [devtools] ✅ DevTools panel created successfully
[segment-analytics-x-ray] [devtools] ✅ React app rendered
[segment-analytics-x-ray] [storage] 🏗️ Creating new tab store for tab 123 (maxEvents: 500)
[segment-analytics-x-ray] [panel] 🎨 Panel mounted for tab 123
[segment-analytics-x-ray] [panel] Current event count in store: 0
[segment-analytics-x-ray] [panel] 🔄 Setting up event sync for tab 123
[segment-analytics-x-ray] [panel] 📥 Fetching initial events for tab 123...
[segment-analytics-x-ray] [panel] 👂 Message listener registered
[segment-analytics-x-ray] [panel] ✅ Received 2 initial events from background
[Table showing events]
[segment-analytics-x-ray] [storage] ➕ Adding event to store (tabId: 123): Page View track
[segment-analytics-x-ray] [storage] Total events in store: 1
[segment-analytics-x-ray] [storage] ➕ Adding event to store (tabId: 123): Click track
[segment-analytics-x-ray] [storage] Total events in store: 2
[segment-analytics-x-ray] [panel] 📊 Event count changed: 2
While watching BOTH consoles (background + panel):
- Click around on the page
- Navigate to different pages
- Interact with the site
Background should show:
🎯 Request intercepted✅ Captured X event(s)📤 Sending message
Panel should show:
📬 Received EVENTS_CAPTURED message✅ Adding X new event(s) to store📊 Event count changed
If events don't show:
- Open background console
- Navigate to test page
- See
🎯 Request interceptedlogs? - See
✅ Captured X event(s)logs?
If NO → Check manifest permissions and webRequest setup
- Background shows
💾 Stored X event(s)? - Background shows
💾 Persisted X event(s)?
Run in background console:
chrome.storage.local.get('events', console.log);Should show events object.
- Background shows
📤 Sending EVENTS_CAPTURED message? - Shows
✅ Message deliveredor⚠️ No listeners?
If "No listeners" → Panel isn't open or didn't register listener
- Open panel console (inspect the panel)
- See
👂 Message listener registered? - See
📬 Received EVENTS_CAPTURED message?
If NO → useEventSync hook may not be running
- Panel shows
📥 Fetching initial events? - Shows
✅ Received X initial events?
If NO → Background message handler may not be responding
- Background:
tabId: XXXin logs - Panel:
Inspected tab ID: XXXmatches?
If NO → Inspecting wrong tab or tab ID mismatch
- Storage shows
➕ Adding event to store? - Shows
Total events in store: X? - Panel shows
📊 Event count changed?
If NO → Store may not be connected to UI
Cause: Panel isn't open or message listener not registered
Fix:
- Make sure DevTools panel is open
- Check panel console for
👂 Message listener registered - If missing,
useEventSynchook may not be running
Cause: DevTools inspecting different tab than where events are captured
Fix:
- Check background log:
tabId: XXX - Check panel log:
Inspected tab ID: XXX - Make sure you're inspecting the correct tab
Cause: Message passing broken
Fix:
- Check both consoles side-by-side
- Verify message is sent (background)
- Verify message is received (panel)
- Check tab IDs match
- ✅
src/lib/logger.ts- NEW (centralized logger) - ✅
src/pages/background/index.ts- Enhanced logging - ✅
src/pages/devtools/hooks/useEventSync.ts- NEW (sync hook) - ✅
src/pages/devtools/Panel.tsx- Added useEventSync, logging - ✅
src/pages/devtools/index.tsx- Added logging - ✅
src/stores/tabStore.ts- Added logging - ✅
DEBUGGING.md- NEW (debugging guide)
- Build and reload the extension
- Open both consoles (background + panel)
- Navigate to a test site with Segment
- Watch the logs flow through both consoles
- Identify where the flow breaks (if it does)
- Use the debugging guide to troubleshoot
The logging will tell you exactly where the issue is! 🎯