Documentation Index
Fetch the complete documentation index at: https://mintlify.com/adalidbori/Tab-Closer-Ext/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Tab Closer is a Chrome Extension built on Manifest V3 that automatically manages browser tabs by limiting the total number of open tabs. When the tab limit is exceeded, the extension automatically closes the oldest tabs to maintain the configured threshold.Architecture Pattern
The extension follows the standard Chrome Extension Manifest V3 architecture with three main components:Service Worker (background.js)
Runs in the background and listens for tab events using the
chrome.tabs APIPopup Interface (popup.html + script.js)
Provides a user interface for configuration and settings management
File Structure
Manifest V3 Configuration
The extension uses Chrome Extension Manifest V3, which introduces service workers instead of background pages.manifest.json
Manifest V3 replaces background pages with service workers, which are event-driven and don’t run continuously. This improves performance and resource usage.
Required Permissions
Grants access to the
chrome.tabs API for querying, creating, and removing tabsEnables use of
chrome.storage.local API for persisting user settingsService Worker Architecture
The service worker (background.js) implements the core tab management logic using an event-driven model.
Tab Creation Event Listener
The extension listens for thechrome.tabs.onCreated event to detect when new tabs are opened:
background.js
How the tab closing logic works
How the tab closing logic works
- Event Detection: When a new tab is created, the
onCreatedlistener fires - Storage Check: Retrieves the
fs(feature switch) value to see if the extension is enabled - Tab Count Check: Retrieves the
tabsallowvalue to get the user’s configured limit - Validation: Ensures
tabsallow >= 2to prevent closing all tabs - Tab Removal: If current tabs exceed the limit, removes the oldest tabs (lowest indices in the tabs array)
- Error Handling: Wraps tab removal in try-catch to handle potential API errors
Chrome APIs Used
chrome.tabs API
Fires when a tab is created. The listener receives the new tab object.
Retrieves all tabs matching specified criteria. Called with empty object
{} to get all tabs.Closes one or more tabs. Takes a tab ID or array of tab IDs.
chrome.storage API
Retrieves items from local storage. Used to access:
fs: Boolean indicating if the extension is enabledtabsallow: Number indicating the maximum allowed tabs
Stores items in local storage. Returns a Promise in the popup script.
Popup Interface Architecture
The popup provides a user-friendly interface for configuring the extension.HTML Structure
The popup is a compact 200x240px interface with:popup.html
Popup Script Logic
Thescript.js file handles user interactions and storage operations:
script.js
Popup initialization flow
Popup initialization flow
- DOM Ready: Script executes after popup HTML loads
- Load Settings:
load()function retrieves stored settings fromchrome.storage.local - Update UI: Checkbox and number input are populated with saved values
- User Interaction: When user clicks Save,
save()validates and stores settings - Validation: Ensures
tabsallow >= 2before saving - Feedback: Shows “Saved!” message on successful save
Data Flow Diagram
Key Design Decisions
Minimum tab limit of 2: The extension enforces a minimum of 2 allowed tabs to prevent users from accidentally closing all tabs and losing browser functionality.
Promise-based storage in popup: The popup script uses the Promise-based
chrome.storage.local.set().then() syntax, while the service worker uses callback-based syntax for compatibility.Performance Considerations
Service Worker Lifecycle
Manifest V3 service workers are event-driven and may be terminated by Chrome when idle:- The service worker activates when
chrome.tabs.onCreatedfires - Chrome automatically handles service worker lifecycle
- All state must be stored in
chrome.storage(not in-memory variables)
Storage Performance
chrome.storage.localis asynchronous and doesn’t block UI- Storage operations are relatively fast but should be minimized
- Current implementation queries storage on every tab creation (potential optimization opportunity)
Security Model
The extension follows Chrome’s security best practices:- Limited permissions: Only requests
tabsandstoragepermissions - No host permissions: Doesn’t access page content or network requests
- No eval(): All code is static (required by Manifest V3)
- CSP compliant: No inline scripts in HTML files
Extension Lifecycle
Debugging Tips
How to debug the service worker
How to debug the service worker
- Navigate to
chrome://extensions/ - Enable “Developer mode” in top right
- Find Tab Closer extension
- Click “Inspect views: service worker” to open DevTools
- Check Console for any errors or
console.log()output
How to debug the popup
How to debug the popup
- Click the extension icon to open popup
- Right-click inside popup
- Select “Inspect”
- DevTools opens for the popup context
Browser Compatibility
The extension is designed for Chrome and Chromium-based browsers:- Chrome: Version 88+ (Manifest V3 support)
- Edge: Version 88+ (Chromium-based)
- Brave: Recent versions with Manifest V3 support
- Opera: Version 74+ (Chromium-based)