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.4---56# Chrome Extension Development78This skill provides expert-level guidance for Chrome extension development, covering JavaScript/TypeScript, browser extension APIs, and modern web development practices.910## Workflow: Building a Chrome Extension from Scratch11121. **Initialize the project** — Create the directory structure with `manifest.json`, background service worker, content scripts, and popup files.132. **Configure the manifest** — Define permissions, content script matches, service worker registration, and action settings in Manifest V3 format.143. **Implement the background service worker** — Set up event listeners for extension lifecycle, messaging, and alarms using the `chrome.*` API.154. **Build content scripts** — Write scripts that interact with web page DOM, communicate with the background worker via `chrome.runtime.sendMessage`, and respect CSP.165. **Create the popup UI** — Design the popup HTML/CSS and wire up interactivity with the background and content scripts.176. **Add storage and state management** — Use `chrome.storage.local` or `chrome.storage.sync` to persist user settings and extension state.187. **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.198. **Package and publish** — Prepare store assets (icons, screenshots, description), create a privacy policy, and submit to the Chrome Web Store.2021## Example: Minimal Manifest V3 Configuration2223```json24{25 "manifest_version": 3,26 "name": "My Extension",27 "version": "1.0.0",28 "description": "A sample Chrome extension using Manifest V3",29 "permissions": ["storage", "activeTab"],30 "action": {31 "default_popup": "popup/popup.html",32 "default_icon": {33 "16": "icons/icon16.png",34 "48": "icons/icon48.png",35 "128": "icons/icon128.png"36 }37 },38 "background": {39 "service_worker": "background/service-worker.js",40 "type": "module"41 },42 "content_scripts": [43 {44 "matches": ["https://*.example.com/*"],45 "js": ["content/content-script.js"],46 "css": ["content/styles.css"]47 }48 ]49}50```5152## Example: Background Service Worker with Messaging5354```typescript55// background/service-worker.ts5657// Listen for extension install or update58chrome.runtime.onInstalled.addListener((details) => {59 if (details.reason === 'install') {60 chrome.storage.local.set({ initialized: true, count: 0 });61 console.log('Extension installed');62 }63});6465// Handle messages from content scripts or popup66chrome.runtime.onMessage.addListener(67 (message: { type: string; payload?: unknown }, sender, sendResponse) => {68 if (message.type === 'GET_COUNT') {69 chrome.storage.local.get('count', (result) => {70 sendResponse({ count: result.count ?? 0 });71 });72 return true; // keep message channel open for async response73 }7475 if (message.type === 'INCREMENT') {76 chrome.storage.local.get('count', (result) => {77 const newCount = (result.count ?? 0) + 1;78 chrome.storage.local.set({ count: newCount }, () => {79 sendResponse({ count: newCount });80 });81 });82 return true;83 }84 }85);8687// Schedule periodic tasks with chrome.alarms88chrome.alarms.create('sync-data', { periodInMinutes: 30 });89chrome.alarms.onAlarm.addListener((alarm) => {90 if (alarm.name === 'sync-data') {91 console.log('Running scheduled sync');92 }93});94```9596## Code Style and Structure9798- Write clear, modular TypeScript code with proper type definitions99- Follow functional programming patterns; avoid classes100- Use descriptive variable names (e.g., isLoading, hasPermission)101- Structure files logically: popup, background, content scripts, utils102- Implement proper error handling and logging103- Document code with JSDoc comments104105## Architecture and Best Practices106107- Strictly follow Manifest V3 specifications108- Divide responsibilities between background, content scripts and popup109- Configure permissions following the principle of least privilege110- Use modern build tools (webpack/vite) for development111- Implement proper version control and change management112113## Chrome API Usage114115- Use chrome.* APIs correctly (storage, tabs, runtime, etc.)116- Handle asynchronous operations with Promises117- Use Service Worker for background scripts (MV3 requirement)118- Implement chrome.alarms for scheduled tasks119- Use chrome.action API for browser actions120- Handle offline functionality gracefully121122## Security and Privacy123124- Implement Content Security Policy (CSP)125- Handle user data securely126- Prevent XSS and injection attacks127- Use secure messaging between components128- Handle cross-origin requests safely129- Implement secure data encryption130- Follow web_accessible_resources best practices131132## Performance and Optimization133134- Minimize resource usage and avoid memory leaks135- Optimize background script performance136- Implement proper caching mechanisms137- Handle asynchronous operations efficiently138- Monitor and optimize CPU/memory usage139140## UI and User Experience141142- Follow Material Design guidelines143- Implement responsive popup windows144- Provide clear user feedback145- Support keyboard navigation146- Ensure proper loading states147- Add appropriate animations148149## Internationalization150151- Use chrome.i18n API for translations152- Follow _locales structure153- Support RTL languages154- Handle regional formats155156## Accessibility157158- Implement ARIA labels159- Ensure sufficient color contrast160- Support screen readers161- Add keyboard shortcuts162163## Testing and Debugging164165- Use Chrome DevTools effectively166- Write unit and integration tests167- Test cross-browser compatibility168- Monitor performance metrics169- Handle error scenarios170171## Publishing and Maintenance172173- Prepare store listings and screenshots174- Write clear privacy policies175- Implement update mechanisms176- Handle user feedback177- Maintain documentation178179## Follow Official Documentation180181- Refer to Chrome Extension documentation182- Stay updated with Manifest V3 changes183- Follow Chrome Web Store guidelines184- Monitor Chrome platform updates185186## Output Expectations187188- Provide clear, working code examples189- Include necessary error handling190- Follow security best practices191- Ensure cross-browser compatibility192- Write maintainable and scalable code