Chrome Extension Development
This skill provides expert-level guidance for Chrome extension development, covering JavaScript/TypeScript, browser extension APIs, and modern web development practices.
Workflow: Building a Chrome Extension from Scratch
- Initialize the project — Create the directory structure with
manifest.json, background service worker, content scripts, and popup files.
- Configure the manifest — Define permissions, content script matches, service worker registration, and action settings in Manifest V3 format.
- Implement the background service worker — Set up event listeners for extension lifecycle, messaging, and alarms using the
chrome.* API.
- Build content scripts — Write scripts that interact with web page DOM, communicate with the background worker via
chrome.runtime.sendMessage, and respect CSP.
- Create the popup UI — Design the popup HTML/CSS and wire up interactivity with the background and content scripts.
- Add storage and state management — Use
chrome.storage.local or chrome.storage.sync to persist user settings and extension state.
- Test and debug — Load the extension unpacked via
chrome://extensions, use Chrome DevTools to inspect the service worker and content scripts, and run unit tests.
- Package and publish — Prepare store assets (icons, screenshots, description), create a privacy policy, and submit to the Chrome Web Store.
Example: Minimal Manifest V3 Configuration
{
"manifest_version": 3,
"name": "My Extension",
"version": "1.0.0",
"description": "A sample Chrome extension using Manifest V3",
"permissions": ["storage", "activeTab"],
"action": {
"default_popup": "popup/popup.html",
"default_icon": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
}
},
"background": {
"service_worker": "background/service-worker.js",
"type": "module"
},
"content_scripts": [
{
"matches": ["https://*.example.com/*"],
"js": ["content/content-script.js"],
"css": ["content/styles.css"]
}
]
}
Example: Background Service Worker with Messaging
// background/service-worker.ts
// Listen for extension install or update
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
chrome.storage.local.set({ initialized: true, count: 0 });
console.log('Extension installed');
}
});
// Handle messages from content scripts or popup
chrome.runtime.onMessage.addListener(
(message: { type: string; payload?: unknown }, sender, sendResponse) => {
if (message.type === 'GET_COUNT') {
chrome.storage.local.get('count', (result) => {
sendResponse({ count: result.count ?? 0 });
});
return true; // keep message channel open for async response
}
if (message.type === 'INCREMENT') {
chrome.storage.local.get('count', (result) => {
const newCount = (result.count ?? 0) + 1;
chrome.storage.local.set({ count: newCount }, () => {
sendResponse({ count: newCount });
});
});
return true;
}
}
);
// Schedule periodic tasks with chrome.alarms
chrome.alarms.create('sync-data', { periodInMinutes: 30 });
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'sync-data') {
console.log('Running scheduled sync');
}
});
Code Style and Structure
- Write clear, modular TypeScript code with proper type definitions
- Follow functional programming patterns; avoid classes
- Use descriptive variable names (e.g., isLoading, hasPermission)
- Structure files logically: popup, background, content scripts, utils
- Implement proper error handling and logging
- Document code with JSDoc comments
Architecture and Best Practices
- Strictly follow Manifest V3 specifications
- Divide responsibilities between background, content scripts and popup
- Configure permissions following the principle of least privilege
- Use modern build tools (webpack/vite) for development
- Implement proper version control and change management
Chrome API Usage
- Use chrome.* APIs correctly (storage, tabs, runtime, etc.)
- Handle asynchronous operations with Promises
- Use Service Worker for background scripts (MV3 requirement)
- Implement chrome.alarms for scheduled tasks
- Use chrome.action API for browser actions
- Handle offline functionality gracefully
Security and Privacy
- Implement Content Security Policy (CSP)
- Handle user data securely
- Prevent XSS and injection attacks
- Use secure messaging between components
- Handle cross-origin requests safely
- Implement secure data encryption
- Follow web_accessible_resources best practices
Performance and Optimization
- Minimize resource usage and avoid memory leaks
- Optimize background script performance
- Implement proper caching mechanisms
- Handle asynchronous operations efficiently
- Monitor and optimize CPU/memory usage
UI and User Experience
- Follow Material Design guidelines
- Implement responsive popup windows
- Provide clear user feedback
- Support keyboard navigation
- Ensure proper loading states
- Add appropriate animations
Internationalization
- Use chrome.i18n API for translations
- Follow _locales structure
- Support RTL languages
- Handle regional formats
Accessibility
- Implement ARIA labels
- Ensure sufficient color contrast
- Support screen readers
- Add keyboard shortcuts
Testing and Debugging
- Use Chrome DevTools effectively
- Write unit and integration tests
- Test cross-browser compatibility
- Monitor performance metrics
- Handle error scenarios
Publishing and Maintenance
- Prepare store listings and screenshots
- Write clear privacy policies
- Implement update mechanisms
- Handle user feedback
- Maintain documentation
Follow Official Documentation
- Refer to Chrome Extension documentation
- Stay updated with Manifest V3 changes
- Follow Chrome Web Store guidelines
- Monitor Chrome platform updates
Output Expectations
- Provide clear, working code examples
- Include necessary error handling
- Follow security best practices
- Ensure cross-browser compatibility
- Write maintainable and scalable code
1---2name: chrome-extension-development3description: "Expert guidelines for Chrome extension development with Manifest V3, covering security, performance, and best practices. Use when building browser extensions, creating popup UIs, implementing content scripts, working with Chrome APIs, managing extension permissions, or publishing to Chrome Web Store."4metadata:5 maintainer: Mindrally6 source: https://skillmd.com/skills/mindrally/chrome-extension-development7 upstream: https://github.com/Mindrally/skills8---910# Chrome Extension Development1112This skill provides expert-level guidance for Chrome extension development, covering JavaScript/TypeScript, browser extension APIs, and modern web development practices.1314## Workflow: Building a Chrome Extension from Scratch15161. **Initialize the project** — Create the directory structure with `manifest.json`, background service worker, content scripts, and popup files.172. **Configure the manifest** — Define permissions, content script matches, service worker registration, and action settings in Manifest V3 format.183. **Implement the background service worker** — Set up event listeners for extension lifecycle, messaging, and alarms using the `chrome.*` API.194. **Build content scripts** — Write scripts that interact with web page DOM, communicate with the background worker via `chrome.runtime.sendMessage`, and respect CSP.205. **Create the popup UI** — Design the popup HTML/CSS and wire up interactivity with the background and content scripts.216. **Add storage and state management** — Use `chrome.storage.local` or `chrome.storage.sync` to persist user settings and extension state.227. **Test and debug** — Load the extension unpacked via `chrome://extensions`, use Chrome DevTools to inspect the service worker and content scripts, and run unit tests.238. **Package and publish** — Prepare store assets (icons, screenshots, description), create a privacy policy, and submit to the Chrome Web Store.2425## Example: Minimal Manifest V3 Configuration2627```json28{29 "manifest_version": 3,30 "name": "My Extension",31 "version": "1.0.0",32 "description": "A sample Chrome extension using Manifest V3",33 "permissions": ["storage", "activeTab"],34 "action": {35 "default_popup": "popup/popup.html",36 "default_icon": {37 "16": "icons/icon16.png",38 "48": "icons/icon48.png",39 "128": "icons/icon128.png"40 }41 },42 "background": {43 "service_worker": "background/service-worker.js",44 "type": "module"45 },46 "content_scripts": [47 {48 "matches": ["https://*.example.com/*"],49 "js": ["content/content-script.js"],50 "css": ["content/styles.css"]51 }52 ]53}54```5556## Example: Background Service Worker with Messaging5758```typescript59// background/service-worker.ts6061// Listen for extension install or update62chrome.runtime.onInstalled.addListener((details) => {63 if (details.reason === 'install') {64 chrome.storage.local.set({ initialized: true, count: 0 });65 console.log('Extension installed');66 }67});6869// Handle messages from content scripts or popup70chrome.runtime.onMessage.addListener(71 (message: { type: string; payload?: unknown }, sender, sendResponse) => {72 if (message.type === 'GET_COUNT') {73 chrome.storage.local.get('count', (result) => {74 sendResponse({ count: result.count ?? 0 });75 });76 return true; // keep message channel open for async response77 }7879 if (message.type === 'INCREMENT') {80 chrome.storage.local.get('count', (result) => {81 const newCount = (result.count ?? 0) + 1;82 chrome.storage.local.set({ count: newCount }, () => {83 sendResponse({ count: newCount });84 });85 });86 return true;87 }88 }89);9091// Schedule periodic tasks with chrome.alarms92chrome.alarms.create('sync-data', { periodInMinutes: 30 });93chrome.alarms.onAlarm.addListener((alarm) => {94 if (alarm.name === 'sync-data') {95 console.log('Running scheduled sync');96 }97});98```99100## Code Style and Structure101102- Write clear, modular TypeScript code with proper type definitions103- Follow functional programming patterns; avoid classes104- Use descriptive variable names (e.g., isLoading, hasPermission)105- Structure files logically: popup, background, content scripts, utils106- Implement proper error handling and logging107- Document code with JSDoc comments108109## Architecture and Best Practices110111- Strictly follow Manifest V3 specifications112- Divide responsibilities between background, content scripts and popup113- Configure permissions following the principle of least privilege114- Use modern build tools (webpack/vite) for development115- Implement proper version control and change management116117## Chrome API Usage118119- Use chrome.* APIs correctly (storage, tabs, runtime, etc.)120- Handle asynchronous operations with Promises121- Use Service Worker for background scripts (MV3 requirement)122- Implement chrome.alarms for scheduled tasks123- Use chrome.action API for browser actions124- Handle offline functionality gracefully125126## Security and Privacy127128- Implement Content Security Policy (CSP)129- Handle user data securely130- Prevent XSS and injection attacks131- Use secure messaging between components132- Handle cross-origin requests safely133- Implement secure data encryption134- Follow web_accessible_resources best practices135136## Performance and Optimization137138- Minimize resource usage and avoid memory leaks139- Optimize background script performance140- Implement proper caching mechanisms141- Handle asynchronous operations efficiently142- Monitor and optimize CPU/memory usage143144## UI and User Experience145146- Follow Material Design guidelines147- Implement responsive popup windows148- Provide clear user feedback149- Support keyboard navigation150- Ensure proper loading states151- Add appropriate animations152153## Internationalization154155- Use chrome.i18n API for translations156- Follow _locales structure157- Support RTL languages158- Handle regional formats159160## Accessibility161162- Implement ARIA labels163- Ensure sufficient color contrast164- Support screen readers165- Add keyboard shortcuts166167## Testing and Debugging168169- Use Chrome DevTools effectively170- Write unit and integration tests171- Test cross-browser compatibility172- Monitor performance metrics173- Handle error scenarios174175## Publishing and Maintenance176177- Prepare store listings and screenshots178- Write clear privacy policies179- Implement update mechanisms180- Handle user feedback181- Maintain documentation182183## Follow Official Documentation184185- Refer to Chrome Extension documentation186- Stay updated with Manifest V3 changes187- Follow Chrome Web Store guidelines188- Monitor Chrome platform updates189190## Output Expectations191192- Provide clear, working code examples193- Include necessary error handling194- Follow security best practices195- Ensure cross-browser compatibility196- Write maintainable and scalable code