wxt-browser-extensions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity WXT Browser Extensions Best Practices
社区WXT浏览器扩展最佳实践
Comprehensive performance optimization guide for WXT browser extension development. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
WXT浏览器扩展开发综合性能优化指南。包含8个类别共45条规则,按影响优先级排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new WXT browser extension code
- Implementing service worker background scripts
- Injecting content scripts into web pages
- Setting up messaging between extension contexts
- Configuring manifest permissions and resources
在以下场景中参考本指南:
- 编写新的WXT浏览器扩展代码
- 实现Service Worker后台脚本
- 向网页中注入content scripts(内容脚本)
- 配置扩展上下文之间的消息通信
- 配置清单权限与资源
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Service Worker Lifecycle | CRITICAL | |
| 2 | Content Script Injection | CRITICAL | |
| 3 | Messaging Architecture | HIGH | |
| 4 | Storage Patterns | HIGH | |
| 5 | Bundle Optimization | MEDIUM-HIGH | |
| 6 | Manifest Configuration | MEDIUM | |
| 7 | UI Performance | MEDIUM | |
| 8 | TypeScript Patterns | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | Service Worker生命周期 | 关键 | |
| 2 | Content Script注入 | 关键 | |
| 3 | 消息通信架构 | 高 | |
| 4 | 存储模式 | 高 | |
| 5 | 打包优化 | 中高 | |
| 6 | 清单配置 | 中 | |
| 7 | UI性能 | 中 | |
| 8 | TypeScript模式 | 中低 | |
Quick Reference
快速参考
1. Service Worker Lifecycle (CRITICAL)
1. Service Worker生命周期(关键)
- - Register listeners synchronously to prevent missed events
svc-register-listeners-synchronously - - Use storage instead of in-memory state
svc-avoid-global-state - - Keep service worker alive for long operations
svc-keep-alive-patterns - - Handle install and update lifecycle events
svc-handle-install-update - - Use offscreen documents for DOM operations
svc-offscreen-documents - - Use declarative rules for network blocking
svc-declarative-net-request
- - 同步注册监听器以避免错过事件
svc-register-listeners-synchronously - - 使用存储而非内存状态
svc-avoid-global-state - - 保持Service Worker存活以处理长时间操作
svc-keep-alive-patterns - - 处理安装与更新生命周期事件
svc-handle-install-update - - 使用离屏文档处理DOM操作
svc-offscreen-documents - - 使用声明式规则拦截网络请求
svc-declarative-net-request
2. Content Script Injection (CRITICAL)
2. Content Script注入(关键)
- - Place runtime code inside main() function
inject-use-main-function - - Select ISOLATED or MAIN world appropriately
inject-choose-correct-world - - Configure appropriate runAt timing
inject-run-at-timing - - Handle context invalidation on update
inject-use-ctx-invalidated - - Use runtime registration for conditional injection
inject-dynamic-registration - - Configure allFrames for iframe handling
inject-all-frames
- - 将运行时代码放入main()函数中
inject-use-main-function - - 合理选择ISOLATED或MAIN环境
inject-choose-correct-world - - 配置合适的runAt时机
inject-run-at-timing - - 在更新时处理上下文失效问题
inject-use-ctx-invalidated - - 使用运行时注册实现条件注入
inject-dynamic-registration - - 配置allFrames以处理iframe
inject-all-frames
3. Messaging Architecture (HIGH)
3. 消息通信架构(高)
- - Return true for async message handlers
msg-return-true-for-async - - Define type-safe message protocols
msg-type-safe-messaging - - Use tabs.sendMessage for content scripts
msg-use-tabs-sendmessage - - Use ports for streaming communication
msg-use-ports-for-streams - - Handle missing message receivers
msg-handle-no-receiver - - Prevent circular message loops
msg-avoid-circular-messages
- - 异步消息处理函数返回true
msg-return-true-for-async - - 定义类型安全的消息协议
msg-type-safe-messaging - - 对content scripts使用tabs.sendMessage
msg-use-tabs-sendmessage - - 使用端口实现流式通信
msg-use-ports-for-streams - - 处理消息接收方不存在的情况
msg-handle-no-receiver - - 避免消息循环
msg-avoid-circular-messages
4. Storage Patterns (HIGH)
4. 存储模式(高)
- - Use storage.defineItem for type-safe access
store-use-define-item - - Select appropriate storage area
store-choose-storage-area - - Batch storage operations
store-batch-operations - - Use watch() for reactive updates
store-watch-for-changes - - Handle storage quota errors
store-handle-quota-errors
- - 使用storage.defineItem实现类型安全的访问
store-use-define-item - - 选择合适的存储区域
store-choose-storage-area - - 批量处理存储操作
store-batch-operations - - 使用watch()实现响应式更新
store-watch-for-changes - - 处理存储配额错误
store-handle-quota-errors
5. Bundle Optimization (MEDIUM-HIGH)
5. 打包优化(中高)
- - Split code by entrypoint
bundle-split-entrypoints - - Analyze and monitor bundle size
bundle-analyze-size - - Use direct imports for icon libraries
bundle-tree-shake-icons - - Load WASM dynamically
bundle-externalize-wasm - - Minimize content script size
bundle-minify-content-scripts
- - 按入口点拆分代码
bundle-split-entrypoints - - 分析并监控打包体积
bundle-analyze-size - - 直接导入图标库以实现树摇优化
bundle-tree-shake-icons - - 动态加载WASM
bundle-externalize-wasm - - 最小化content scripts体积
bundle-minify-content-scripts
6. Manifest Configuration (MEDIUM)
6. 清单配置(中)
- - Request minimal permissions
manifest-minimal-permissions - - Use optional permissions progressively
manifest-use-optional-permissions - - Scope web accessible resources
manifest-web-accessible-resources - - Configure CSP correctly
manifest-content-security-policy - - Support multiple browsers
manifest-cross-browser-compatibility
- - 请求最小权限
manifest-minimal-permissions - - 逐步使用可选权限
manifest-use-optional-permissions - - 限制可访问的Web资源范围
manifest-web-accessible-resources - - 正确配置CSP
manifest-content-security-policy - - 支持多浏览器
manifest-cross-browser-compatibility
7. UI Performance (MEDIUM)
7. UI性能(中)
- - Use Shadow DOM for injected UI
ui-use-shadow-dom - - Defer popup rendering until needed
ui-defer-rendering - - Clean up UI on unmount
ui-cleanup-on-unmount - - Preserve sidepanel state
ui-sidepanel-persistence - - Use iframe for complex UI
ui-position-fixed-iframe - - Batch DOM reads and writes
ui-avoid-layout-thrashing
- - 使用Shadow DOM实现注入式UI
ui-use-shadow-dom - - 延迟弹窗渲染直到需要时
ui-defer-rendering - - 在卸载时清理UI
ui-cleanup-on-unmount - - 保留侧边栏状态
ui-sidepanel-persistence - - 使用iframe实现复杂UI
ui-position-fixed-iframe - - 批量处理DOM读写以避免布局抖动
ui-avoid-layout-thrashing
8. TypeScript Patterns (LOW-MEDIUM)
8. TypeScript模式(中低)
- - Use browser namespace over chrome
ts-use-browser-not-chrome - - Type entrypoint options explicitly
ts-type-entrypoint-options - - Augment types for missing APIs
ts-augment-browser-types - - Enable strict null checks
ts-strict-null-checks - - Use import.meta for build info
ts-import-meta-env - - Avoid any type in handlers
ts-avoid-any - - Use path aliases for imports
ts-path-aliases
- - 使用browser命名空间而非chrome
ts-use-browser-not-chrome - - 显式为入口点选项添加类型
ts-type-entrypoint-options - - 扩展缺失API的类型定义
ts-augment-browser-types - - 启用严格空值检查
ts-strict-null-checks - - 使用import.meta获取构建信息
ts-import-meta-env - - 在处理函数中避免使用any类型
ts-avoid-any - - 使用路径别名简化导入
ts-path-aliases
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
阅读单个参考文件获取详细说明与代码示例:
- 章节定义 - 类别结构与影响级别
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |