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.
Architecture Overview
Tab Closer is built as a Chrome extension using three core components:- Background Script (
background.js) - Monitors tab creation and enforces limits - Popup UI (
popup.html+style.css) - User interface for configuration - Settings Script (
script.js) - Handles saving and loading user preferences
Core Mechanism
Tab Creation Monitoring
The extension listens for thechrome.tabs.onCreated event, which fires every time a new tab is opened:
This event-based approach ensures immediate response when tabs are created, without requiring background polling or timers.
Complete Tab Management Flow
Here’s the full implementation frombackground.js:
Check Feature Switch
Retrieve the
fs (feature switch) value from storage to determine if the extension is enabled:Complete Background Script
Here’s the fullbackground.js implementation with all logic combined:
Tab Closing Algorithm
Which Tabs Get Closed?
The extension uses a FIFO (First In, First Out) approach:- Visual Example
- Code Logic
- Edge Cases
Scenario: Tab limit is set to 5, user currently has 7 tabs open
Chrome Storage API Usage
The extension useschrome.storage.local for persistent configuration:
Storage API Methods
Saving Data
Saving Data
set() method stores key-value pairs and returns a Promise that resolves when the operation completes.Reading Data
Reading Data
get() method retrieves values by key using a callback function.Storage Persistence
Storage Persistence
Key characteristics of
chrome.storage.local:- Data persists across browser sessions
- Not cleared when the user clears cookies/cache
- Maximum storage: 10 MB (more than sufficient for this extension)
- Synchronous API with async callbacks
- Available to all extension contexts (background, popup, content scripts)
Unlike
localStorage, chrome.storage.local is specifically designed for extensions and provides better reliability.Error Handling
The extension implements basic error handling for tab removal:Popup Script Implementation
Thescript.js file manages the popup UI interaction:
Performance Considerations
Event Listener Efficiency
Event Listener Efficiency
The
onCreated event listener is highly efficient because:- It only fires when a tab is actually created (not on every user action)
- The
tabs.query()call is fast, typically completing in less than 10ms - Storage reads are asynchronous and don’t block the UI
- Tab removal operations are batched in a single loop
Memory Usage
Memory Usage
The extension has minimal memory footprint:
- Background script: ~1-2 MB
- No persistent timers or intervals
- Storage: less than 1 KB (only two values stored)
- Event listeners are passive until triggered
Storage Access Patterns
Storage Access Patterns
Storage is read on every tab creation event. This is acceptable because:The Chrome storage API is optimized for frequent reads and the small data size ensures minimal latency.
Chrome APIs Used
This extension uses three primary Chrome Extension APIs:chrome.tabs
Methods Used:
onCreated.addListener()- Monitor tab creationquery()- Get all open tabsremove()- Close specific tabs
chrome.storage
Methods Used:
local.set()- Save settingslocal.get()- Retrieve settings
fs(boolean)tabsallow(number)
DOM APIs
Standard APIs:
getElementById()- Element accessaddEventListener()- Event handlingdocument- DOM manipulation
Execution Timeline
Here’s what happens from installation to tab closing:Extension Installation
- Extension is loaded into Chrome
background.jsstarts running persistently- Event listeners are registered
- No storage values exist yet (defaults apply)
First Configuration
- User clicks extension icon
popup.htmlopens,script.jsexecutesload()function runs, reading storage (finds no values)- Defaults: checkbox checked, tab limit = 2
- User adjusts settings and clicks Save
save()function writesfsandtabsallowto storage
Tab Creation Event
- User opens a new tab (via Ctrl+T, clicking link, etc.)
chrome.tabs.onCreatedevent fires immediately- Background script executes full validation flow
Debugging and Development
Viewing Background Script Logs
Viewing Background Script Logs
- Navigate to
chrome://extensions/ - Find “Tab Closer” extension
- Click “Inspect views: background page”
- Open the Console tab
console.log() statements from background.js appear here, including error messages from the catch block.Inspecting Popup Scripts
Inspecting Popup Scripts
- Click the extension icon to open the popup
- Right-click anywhere in the popup
- Select “Inspect”
- View Console for
script.jslogs - Use Elements tab to inspect HTML/CSS
Testing Tab Closing Logic
Testing Tab Closing Logic
Manual Testing:Automated Testing:
Open multiple tabs programmatically:
Limitations and Considerations
Security and Permissions
The extension requires minimal permissions:Why These Permissions?
Why These Permissions?
tabs permission:- Required to listen to
onCreatedevents - Needed to query tab information
- Allows removal of tabs
storage permission:- Enables
chrome.storage.localAPI - Required to persist user settings
The extension does NOT require:
- Host permissions (doesn’t access page content)
- Network access
- Clipboard access
- Downloads or file system access
Summary
Tab Closer implements a simple but effective tab management system:- Event-Driven: Responds instantly to tab creation
- User-Controlled: Fully configurable enable/disable and limits
- Lightweight: Minimal resource usage and permissions
- Persistent: Settings survive browser restarts
- Safe: Enforces minimum limits and error handling