Skip to content

feat: add audio tracking for background tabs#609

Merged
sheepzh merged 2 commits intosheepzh:mainfrom
mrfragger:feature/audio-tracking
Nov 10, 2025
Merged

feat: add audio tracking for background tabs#609
sheepzh merged 2 commits intosheepzh:mainfrom
mrfragger:feature/audio-tracking

Conversation

@mrfragger
Copy link
Contributor

@mrfragger mrfragger commented Nov 9, 2025

Adds audio tracking for background tabs using the browser's tab.audible API to track media playback when tabs are inactive. Currently, the extension only tracks time when users actively interact with a tab (via content scripts). When users play media (like YouTube videos) and switch to another tab, that background playback time is not tracked. Addresses #415

  • Monitor tab.audible property changes via onTabUpdated listener
  • Track the active/inactive state of tabs during audio playback
  • Record time segments only when tabs are inactive (background)
  • Active tab time continues to be tracked by existing content script mechanism
  1. When audio starts playing, records whether tab is currently active/inactive
  2. When user switches tabs, records time segment with previous state
  3. Only notifies time tracking for inactive segments (prevents double counting)
  4. Active tab segments are ignored (content scripts handle those)
  • Video plays for 3s while tab is active → content script tracks (audio listener skips)
  • User switches away, video plays for 7s in background → audio listener tracks those 7s
  • User switches back, video plays for 2s while active → content script tracks (audio listener skips)

Tested on Firefox/LibreWolf with YouTube videos:

  • Background tab audio tracking works correctly
  • Active tab segments are skipped (no double counting with content scripts)
  • Tab state changes during playback are handled properly
  • Whitelist is respected
  • Tab closure properly stops tracking and records final segment

- Add AudioTabListener to track audio playback in inactive tabs
- Track tab state changes during playback to record only background time
- Prevent double counting by skipping active tab segments
- Add onTabRemoved wrapper to chrome tab API

When users play media (e.g., YouTube) and switch to another tab, the audio
continues playing in the background. This feature tracks that background
playback time separately from active tab time tracked by content scripts.
@sheepzh
Copy link
Owner

sheepzh commented Nov 10, 2025

@mrfragger hi, thanks for your PR.

there may be not accurate on Chrome and Edge. since they all don't promise the service worker always alive, which causes all audio states loss if background script is unloaded randomly. so I think it's better to store the audio state of each tab in the contentscript and report them with payload of tracking event, while I don't find any API to do that. but we can still monitor tabs by extension API, and send them to the tab's contentscript

- Store audio playback state in chrome.storage.local every 5 seconds
- Restore state on service worker restart
- Ensures tracking survives service worker termination in Manifest V3 browsers
- Minimal overhead: only persists when tabs are actively playing audio

Tested on Chrome/Iron with manual service worker termination.
@sheepzh sheepzh merged commit 058a796 into sheepzh:main Nov 10, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants