The background package initializes the extension store, wires services together, and listens for browser events. It coordinates between the popup UI and core business logic.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/puemos/hls-downloader/llms.txt
Use this file to discover all available pages before exploring further.
Package information
@hls-downloader/backgroundsrc/background/src/src/background/src/index.tslib/index.jsInitialization
The background script is the first code that runs when the extension loads. Location:src/background/src/index.ts:10
Service implementations
The background script provides concrete implementations of core services.IndexedDBFS
File system implementation using IndexedDB for chunk storage. Location:src/background/src/services/indexedb-fs.ts:460
Creates a new storage bucket for download chunks
Retrieves an existing bucket by ID
Deletes a bucket and all its chunks
Triggers browser download dialog
Clears all stored data
Stores subtitle content
Retrieves stored subtitle content
IndexedDBBucket class
Location:src/background/src/services/indexedb-fs.ts:144
Handles storage and streaming of video/audio chunks.
Writes a chunk to the bucket
Creates a ReadableStream of all chunks in order
Generates a blob URL for download
Removes all data for this bucket
The bucket uses FFmpeg.wasm to mux video/audio streams into MP4 or MKV (with subtitles).
FetchLoader
HTTP client with retry logic for downloading playlists and chunks. Location:src/background/src/services/fetch-loader.ts:65
Downloads text content (playlists)
Downloads binary content (video/audio chunks)
Retry behavior
Location:src/background/src/services/fetch-loader.ts:14
- Retries on network errors (not HTTP errors)
- Exponential backoff: starts at 100ms, multiplies by 1.15 each retry
- HTTP errors throw immediately without retry
M3u8Parser
Parses HLS playlists to extract tracks and segments. Location:src/background/src/services/m3u8-parser.ts:9
Parses master playlist to extract quality levels and tracks
Parses media playlist to extract segments
Analyzes encryption used in a media playlistReturns:
methods: Array of encryption methods (e.g.,["AES-128"])keyUris: Array of key file URLsiv: Initialization vector if present
Track extraction
Location:src/background/src/services/m3u8-parser.ts:63
The parser extracts multiple track types from #EXT-X-MEDIA tags:
Video
Stream variants with resolution, bitrate, framerate
Audio
Language, channels, bitrate, default/auto-select flags
Subtitles
Language, name, characteristics, forced flag
CryptoDecryptor
Decrypts AES-128 encrypted segments. Location:src/background/src/services/crypto-decryptor.ts:1
Decrypts a segment using AES-CBC
crypto.subtle.decrypt) with AES-CBC mode.
Listeners
Background listeners react to browser events and dispatch Redux actions.addPlaylistListener
Location:src/background/src/listeners/addPlaylistListener.ts:11
Detects HLS playlists from network requests.
- Only monitors XMLHttpRequest types
- Matches URLs ending in
.m3u8 - Checks content-type header for HLS MIME types
- Blocks requests from domains in blocklist
setTabListener
Location:src/background/src/listeners/setTabListener.ts
Resets icon when navigating to a new page.
State persistence
Location:src/background/src/persistState.ts
Stores Redux state in browser storage to survive restarts.
Offscreen documents
Chrome only: Uses offscreen documents for FFmpeg processing when blob URLs must be created in a document context. Location:src/background/src/services/indexedb-fs.ts:470
Dependencies
- @hls-downloader/core - Business logic
- webext-redux 2.1.9 - Redux bridge
- webextension-polyfill 0.10.0 - Cross-browser API
- @ffmpeg/ffmpeg 0.12.10 - Video/audio muxing
- idb 6.1.2 - IndexedDB wrapper
- m3u8-parser 6.2.0 - HLS playlist parsing
- url-toolkit 2.1.6 - URL resolution
- filenamify 6.0.0 - Safe filename generation
Architecture principle
The background script should only coordinate use cases from
@hls-downloader/core. Keep business logic in the core package, not in background scripts.Development
- Build
- Watch mode
- Test
Use two-space indentation in all source files.