browser-storage-exporter
v1.0.1
Published
Export browser storage states (cookies, localStorage) using Playwright
Maintainers
Readme
Browser Storage Exporter
A Node.js CLI tool to export browser storage states (cookies, localStorage, sessionStorage) using Playwright. This tool can connect to a running browser instance and extract authentication data for testing and development purposes.
Features
- Export cookies and localStorage from running browser instances
- Support for multiple browser contexts and pages
- Configurable debug endpoint URL
- Direct page navigation and export
- JSON output format compatible with Playwright's
storageState
Installation
npm install browser-storage-exporterOr use directly with npx:
npx browser-storage-exporterConfiguration
Environment Variables
Create a .env file in your project root based on .env.example:
cp .env.example .envEdit .env to configure your browser debug URL:
# Browser Debug Configuration
DEBUG_URL=http://localhost:52686/json/versionCommon debug URLs:
http://localhost:52686/json/version(default)http://localhost:9222/json/version(standard Chrome debug port)http://localhost:9223/json/version(alternative port)
Usage
Basic Usage
# Connect to local browser using default/env debug URL
npx browser-storage-exporter
# Specify output directory
npx browser-storage-exporter -o ./exports
# Use custom debug URL
npx browser-storage-exporter -d http://localhost:9222/json/versionAdvanced Usage
# Connect using WebSocket URL directly
npx browser-storage-exporter ws://localhost:9222/devtools/browser/your-browser-id
# Open specific page and export its storage
npx browser-storage-exporter https://example.com
# Combine options
npx browser-storage-exporter -o ./my-exports -d http://localhost:9222/json/versionCommand Line Options
Options:
-h, --help Show help information
-o, --output <dir> Specify output directory (default: current directory)
-d, --debug-url <url> Browser debug endpoint URL (default: from .env or http://localhost:52686/json/version)
Configuration:
Create a .env file based on .env.example to set default DEBUG_URLBrowser Setup
For Chrome/Chromium
Start Chrome with remote debugging enabled:
# macOS
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
# Linux
google-chrome --remote-debugging-port=9222
# Windows
"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222For Microsoft Edge
# Windows
"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222Output Format
The tool generates a JSON file containing:
{
"cookies": [
{
"name": "session_id",
"value": "abc123",
"domain": ".example.com",
"path": "/",
"expires": 1640995200,
"httpOnly": true,
"secure": true,
"sameSite": "Lax"
}
],
"origins": [
{
"origin": "https://example.com",
"localStorage": [
{
"name": "user_preferences",
"value": "{\"theme\":\"dark\"}"
}
]
}
]
}Using with Playwright
You can use the exported storage state with Playwright:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext({
storageState: './browser-auth-complete.json'
});
const page = await context.newPage();
await page.goto('https://example.com');
// You should now be logged in with the exported session
await browser.close();
})();Troubleshooting
Common Issues
"No browser contexts found"
- Make sure your browser is running with remote debugging enabled
- Check that the debug URL is accessible (try opening it in a browser)
- Verify the port number matches your browser's debug port
"Error processing page"
- Some pages may block script execution
- Try refreshing the target page
- Ensure the page has finished loading
Connection refused
- Verify the browser is running with
--remote-debugging-port - Check if another application is using the debug port
- Try a different port number
- Verify the browser is running with
Debug URLs
You can verify your browser's debug endpoint by visiting:
http://localhost:9222/json/version(or your configured port)
This should return JSON with browser information including the WebSocket debug URL.
Development
# Clone the repository
git clone <repository-url>
cd browser-storage-exporter
# Install dependencies
npm install
# Run locally
node bin/cli.js --helpLicense
MIT
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
