File contents Chrome Extension Best Practices
Comprehensive performance and code quality guide for Chrome Extensions (Manifest V3). Contains 67 rules across 12 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
Writing new Chrome extension code
Migrating from Manifest V2 to Manifest V3
Optimizing service worker lifecycle and state management
Implementing content scripts for page interaction
Debugging performance issues in extensions
Rule Categories by Priority
Priority
Category
Impact
Prefix
1
Service Worker Lifecycle
CRITICAL
sw-
2
Content Script Optimization
CRITICAL
content-
3
Message Passing Efficiency
HIGH
msg-
4
Storage Operations
HIGH
storage-
5
Network & Permissions
MEDIUM-HIGH
net-
6
Memory Management
MEDIUM
mem-
7
UI Performance
MEDIUM
ui-
8
API Usage Patterns
LOW-MEDIUM
api-
9
Code Style & Naming
MEDIUM
style-
10
Component Patterns
MEDIUM
comp-
11
Error Handling
HIGH
err-
12
Testing Patterns
MEDIUM
test-
Quick Reference
1. Service Worker Lifecycle (CRITICAL)
sw-persist-state-storage - Persist state with chrome.storage instead of global variables
sw-avoid-keepalive - Avoid artificial service worker keep-alive patterns
sw-use-alarms-api - Use chrome.alarms instead of setTimeout/setInterval
sw-return-true-async - Return true from message listeners for async responses
sw-register-listeners-toplevel - Register event listeners at top level
sw-use-offscreen-for-dom - Use offscreen documents for DOM APIs
2. Content Script Optimization (CRITICAL)
content-use-specific-matches - Use specific URL match patterns
content-use-document-idle - Use document_idle for content script injection
content-programmatic-injection - Prefer programmatic injection over manifest declaration
content-minimize-script-size - Minimize content script bundle size
content-batch-dom-operations - Batch DOM operations to minimize reflows
content-use-mutation-observer - Use MutationObserver instead of polling
3. Message Passing Efficiency (HIGH)
msg-use-ports-for-frequent - Use port connections for frequent message exchange
msg-minimize-payload-size - Minimize message payload size
msg-debounce-frequent-events - Debounce high-frequency events before messaging
msg-check-lasterror - Always check chrome.runtime.lastError
msg-avoid-broadcast-to-all-tabs - Avoid broadcasting messages to all tabs
4. Storage Operations (HIGH)
storage-batch-operations - Batch storage operations instead of individual calls
storage-choose-correct-type - Choose the correct storage type for your use case
storage-cache-frequently-accessed - Cache frequently accessed storage values
storage-use-session-for-temp - Use storage.session for temporary runtime data
storage-avoid-storing-large-blobs - Avoid storing large binary blobs
5. Network & Permissions (MEDIUM-HIGH)
net-use-declarativenetrequest - Use declarativeNetRequest instead of webRequest
net-request-minimal-permissions - Request minimal required permissions
net-use-activetab - Use activeTab permission instead of broad host permissions
net-limit-csp-modifications - Avoid modifying Content Security Policy headers
6. Memory Management (MEDIUM)
mem-cleanup-event-listeners - Clean up event listeners when content script unloads
mem-avoid-detached-dom - Avoid holding references to detached DOM nodes
mem-avoid-closure-leaks - Avoid accidental closure memory leaks
mem-clear-intervals-timeouts - Clear intervals and timeouts on cleanup
mem-use-weak-collections - Use WeakMap and WeakSet for DOM element references
7. UI Performance (MEDIUM)
ui-minimize-popup-bundle - Minimize popup bundle size for fast startup
ui-render-with-cached-data - Render popup UI with cached data first
ui-batch-badge-updates - Batch badge updates to avoid flicker
ui-use-options-page-lazy - Lazy load options page sections
8. API Usage Patterns (LOW-MEDIUM)
api-use-promises-over-callbacks - Use promise-based API calls over callbacks
api-query-tabs-efficiently - Query tabs with specific filters
api-avoid-redundant-api-calls - Avoid redundant API calls in loops
api-use-alarms-minperiod - Respect alarms API minimum period
api-handle-context-invalidated - Handle extension context invalidated errors
api-use-declarative-content - Use declarative content API for page actions
9. Code Style & Naming (MEDIUM)
style-boolean-naming - Use is/has/should prefixes for boolean variables
style-cache-naming - Use consistent cache variable naming
style-constants - Define constants for magic values
style-directory-structure - Organize code by feature/layer
style-file-naming - Use consistent file naming conventions
style-function-naming - Use descriptive function names
style-import-type - Use type-only imports for types
style-index-entry-points - Use index files for module entry points
style-message-enums - Use enums for message types
style-type-naming - Use PascalCase for types and interfaces
10. Component Patterns (MEDIUM)
comp-adapter-interface - Use adapter pattern for browser APIs
comp-content-script-structure - Structure content scripts consistently
comp-css-class-patterns - Use BEM or prefixed CSS classes
comp-manager-class - Use manager classes for complex state
comp-type-guards - Use type guards for runtime validation
comp-ui-components - Create reusable UI components
11. Error Handling (HIGH)
err-context-invalidation - Handle extension context invalidation
err-early-return - Use early returns for error handling
err-null-coalescing - Use nullish coalescing for defaults
err-promise-barrier - Use promise barriers for coordination
err-storage-operations - Handle storage operation failures
err-url-parsing - Safely parse URLs with try/catch
err-validation-pattern - Validate inputs at boundaries
12. Testing Patterns (MEDIUM)
test-browser-api-mocking - Mock chrome APIs in tests
test-organization - Organize tests by feature
test-validation-functions - Test validation functions thoroughly
How to Use
Read individual reference files for detailed explanations and code examples:
Section definitions - Category structure and impact levels
Rule template - Template for adding new rules
Full Compiled Document
For a complete guide with all rules in a single document, see AGENTS.md.
Reference Files
File
Description
AGENTS.md
Complete compiled guide with all rules
references/_sections.md
Category definitions and ordering
assets/templates/_template.md
Template for new rules
metadata.json
Version and reference information
1 --- 2 name: chrome-extension 3 description: Chrome Extension Best Practices 4 --- 5 # Chrome Extension Best Practices 6 7 Comprehensive performance and code quality guide for Chrome Extensions (Manifest V3). Contains 67 rules across 12 categories, prioritized by impact to guide automated refactoring and code generation. 8 9 ## When to Apply 10 11 Reference these guidelines when: 12 - Writing new Chrome extension code 13 - Migrating from Manifest V2 to Manifest V3 14 - Optimizing service worker lifecycle and state management 15 - Implementing content scripts for page interaction 16 - Debugging performance issues in extensions 17 18 ## Rule Categories by Priority 19 20 | Priority | Category | Impact | Prefix | 21 |----------|----------|--------|--------| 22 | 1 | Service Worker Lifecycle | CRITICAL | `sw-` | 23 | 2 | Content Script Optimization | CRITICAL | `content-` | 24 | 3 | Message Passing Efficiency | HIGH | `msg-` | 25 | 4 | Storage Operations | HIGH | `storage-` | 26 | 5 | Network & Permissions | MEDIUM-HIGH | `net-` | 27 | 6 | Memory Management | MEDIUM | `mem-` | 28 | 7 | UI Performance | MEDIUM | `ui-` | 29 | 8 | API Usage Patterns | LOW-MEDIUM | `api-` | 30 | 9 | Code Style & Naming | MEDIUM | `style-` | 31 | 10 | Component Patterns | MEDIUM | `comp-` | 32 | 11 | Error Handling | HIGH | `err-` | 33 | 12 | Testing Patterns | MEDIUM | `test-` | 34 35 ## Quick Reference 36 37 ### 1. Service Worker Lifecycle (CRITICAL) 38 39 - [`sw-persist-state-storage`](references/sw-persist-state-storage.md) - Persist state with chrome.storage instead of global variables 40 - [`sw-avoid-keepalive`](references/sw-avoid-keepalive.md) - Avoid artificial service worker keep-alive patterns 41 - [`sw-use-alarms-api`](references/sw-use-alarms-api.md) - Use chrome.alarms instead of setTimeout/setInterval 42 - [`sw-return-true-async`](references/sw-return-true-async.md) - Return true from message listeners for async responses 43 - [`sw-register-listeners-toplevel`](references/sw-register-listeners-toplevel.md) - Register event listeners at top level 44 - [`sw-use-offscreen-for-dom`](references/sw-use-offscreen-for-dom.md) - Use offscreen documents for DOM APIs 45 46 ### 2. Content Script Optimization (CRITICAL) 47 48 - [`content-use-specific-matches`](references/content-use-specific-matches.md) - Use specific URL match patterns 49 - [`content-use-document-idle`](references/content-use-document-idle.md) - Use document_idle for content script injection 50 - [`content-programmatic-injection`](references/content-programmatic-injection.md) - Prefer programmatic injection over manifest declaration 51 - [`content-minimize-script-size`](references/content-minimize-script-size.md) - Minimize content script bundle size 52 - [`content-batch-dom-operations`](references/content-batch-dom-operations.md) - Batch DOM operations to minimize reflows 53 - [`content-use-mutation-observer`](references/content-use-mutation-observer.md) - Use MutationObserver instead of polling 54 55 ### 3. Message Passing Efficiency (HIGH) 56 57 - [`msg-use-ports-for-frequent`](references/msg-use-ports-for-frequent.md) - Use port connections for frequent message exchange 58 - [`msg-minimize-payload-size`](references/msg-minimize-payload-size.md) - Minimize message payload size 59 - [`msg-debounce-frequent-events`](references/msg-debounce-frequent-events.md) - Debounce high-frequency events before messaging 60 - [`msg-check-lasterror`](references/msg-check-lasterror.md) - Always check chrome.runtime.lastError 61 - [`msg-avoid-broadcast-to-all-tabs`](references/msg-avoid-broadcast-to-all-tabs.md) - Avoid broadcasting messages to all tabs 62 63 ### 4. Storage Operations (HIGH) 64 65 - [`storage-batch-operations`](references/storage-batch-operations.md) - Batch storage operations instead of individual calls 66 - [`storage-choose-correct-type`](references/storage-choose-correct-type.md) - Choose the correct storage type for your use case 67 - [`storage-cache-frequently-accessed`](references/storage-cache-frequently-accessed.md) - Cache frequently accessed storage values 68 - [`storage-use-session-for-temp`](references/storage-use-session-for-temp.md) - Use storage.session for temporary runtime data 69 - [`storage-avoid-storing-large-blobs`](references/storage-avoid-storing-large-blobs.md) - Avoid storing large binary blobs 70 71 ### 5. Network & Permissions (MEDIUM-HIGH) 72 73 - [`net-use-declarativenetrequest`](references/net-use-declarativenetrequest.md) - Use declarativeNetRequest instead of webRequest 74 - [`net-request-minimal-permissions`](references/net-request-minimal-permissions.md) - Request minimal required permissions 75 - [`net-use-activetab`](references/net-use-activetab.md) - Use activeTab permission instead of broad host permissions 76 - [`net-limit-csp-modifications`](references/net-limit-csp-modifications.md) - Avoid modifying Content Security Policy headers 77 78 ### 6. Memory Management (MEDIUM) 79 80 - [`mem-cleanup-event-listeners`](references/mem-cleanup-event-listeners.md) - Clean up event listeners when content script unloads 81 - [`mem-avoid-detached-dom`](references/mem-avoid-detached-dom.md) - Avoid holding references to detached DOM nodes 82 - [`mem-avoid-closure-leaks`](references/mem-avoid-closure-leaks.md) - Avoid accidental closure memory leaks 83 - [`mem-clear-intervals-timeouts`](references/mem-clear-intervals-timeouts.md) - Clear intervals and timeouts on cleanup 84 - [`mem-use-weak-collections`](references/mem-use-weak-collections.md) - Use WeakMap and WeakSet for DOM element references 85 86 ### 7. UI Performance (MEDIUM) 87 88 - [`ui-minimize-popup-bundle`](references/ui-minimize-popup-bundle.md) - Minimize popup bundle size for fast startup 89 - [`ui-render-with-cached-data`](references/ui-render-with-cached-data.md) - Render popup UI with cached data first 90 - [`ui-batch-badge-updates`](references/ui-batch-badge-updates.md) - Batch badge updates to avoid flicker 91 - [`ui-use-options-page-lazy`](references/ui-use-options-page-lazy.md) - Lazy load options page sections 92 93 ### 8. API Usage Patterns (LOW-MEDIUM) 94 95 - [`api-use-promises-over-callbacks`](references/api-use-promises-over-callbacks.md) - Use promise-based API calls over callbacks 96 - [`api-query-tabs-efficiently`](references/api-query-tabs-efficiently.md) - Query tabs with specific filters 97 - [`api-avoid-redundant-api-calls`](references/api-avoid-redundant-api-calls.md) - Avoid redundant API calls in loops 98 - [`api-use-alarms-minperiod`](references/api-use-alarms-minperiod.md) - Respect alarms API minimum period 99 - [`api-handle-context-invalidated`](references/api-handle-context-invalidated.md) - Handle extension context invalidated errors 100 - [`api-use-declarative-content`](references/api-use-declarative-content.md) - Use declarative content API for page actions 101 102 ### 9. Code Style & Naming (MEDIUM) 103 104 - [`style-boolean-naming`](references/style-boolean-naming.md) - Use is/has/should prefixes for boolean variables 105 - [`style-cache-naming`](references/style-cache-naming.md) - Use consistent cache variable naming 106 - [`style-constants`](references/style-constants.md) - Define constants for magic values 107 - [`style-directory-structure`](references/style-directory-structure.md) - Organize code by feature/layer 108 - [`style-file-naming`](references/style-file-naming.md) - Use consistent file naming conventions 109 - [`style-function-naming`](references/style-function-naming.md) - Use descriptive function names 110 - [`style-import-type`](references/style-import-type.md) - Use type-only imports for types 111 - [`style-index-entry-points`](references/style-index-entry-points.md) - Use index files for module entry points 112 - [`style-message-enums`](references/style-message-enums.md) - Use enums for message types 113 - [`style-type-naming`](references/style-type-naming.md) - Use PascalCase for types and interfaces 114 115 ### 10. Component Patterns (MEDIUM) 116 117 - [`comp-adapter-interface`](references/comp-adapter-interface.md) - Use adapter pattern for browser APIs 118 - [`comp-content-script-structure`](references/comp-content-script-structure.md) - Structure content scripts consistently 119 - [`comp-css-class-patterns`](references/comp-css-class-patterns.md) - Use BEM or prefixed CSS classes 120 - [`comp-manager-class`](references/comp-manager-class.md) - Use manager classes for complex state 121 - [`comp-type-guards`](references/comp-type-guards.md) - Use type guards for runtime validation 122 - [`comp-ui-components`](references/comp-ui-components.md) - Create reusable UI components 123 124 ### 11. Error Handling (HIGH) 125 126 - [`err-context-invalidation`](references/err-context-invalidation.md) - Handle extension context invalidation 127 - [`err-early-return`](references/err-early-return.md) - Use early returns for error handling 128 - [`err-null-coalescing`](references/err-null-coalescing.md) - Use nullish coalescing for defaults 129 - [`err-promise-barrier`](references/err-promise-barrier.md) - Use promise barriers for coordination 130 - [`err-storage-operations`](references/err-storage-operations.md) - Handle storage operation failures 131 - [`err-url-parsing`](references/err-url-parsing.md) - Safely parse URLs with try/catch 132 - [`err-validation-pattern`](references/err-validation-pattern.md) - Validate inputs at boundaries 133 134 ### 12. Testing Patterns (MEDIUM) 135 136 - [`test-browser-api-mocking`](references/test-browser-api-mocking.md) - Mock chrome APIs in tests 137 - [`test-organization`](references/test-organization.md) - Organize tests by feature 138 - [`test-validation-functions`](references/test-validation-functions.md) - Test validation functions thoroughly 139 140 ## How to Use 141 142 Read individual reference files for detailed explanations and code examples: 143 144 - [Section definitions](references/_sections.md) - Category structure and impact levels 145 - [Rule template](assets/templates/_template.md) - Template for adding new rules 146 147 ## Full Compiled Document 148 149 For a complete guide with all rules in a single document, see [AGENTS.md](AGENTS.md). 150 151 ## Reference Files 152 153 | File | Description | 154 |------|-------------| 155 | [AGENTS.md](AGENTS.md) | Complete compiled guide with all rules | 156 | [references/_sections.md](references/_sections.md) | Category definitions and ordering | 157 | [assets/templates/_template.md](assets/templates/_template.md) | Template for new rules | 158 | [metadata.json](metadata.json) | Version and reference information |
ComeOnOliver/skillshub/tree/main/skills/pproenca/dot-skills/chrome-extension commit 60dac0b470
Frequently asked questions How do I install the Chrome Extension skill? Run npx skillmds@latest add comeonoliver/chrome-extension in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Chrome Extension skill do? Chrome Extension Best Practices It is listed under Coding & Dev Tools on SkillMD.
Is Chrome Extension safe to use? This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Chrome Extension? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Chrome Extension free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Chrome Extension? ComeOnOliver (@comeonoliver) published this skill. Their other Agent Skills are listed on their SkillMD profile.