extension-dev

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Extension Dev

Chrome扩展开发

Detect the stack, find docs, implement the feature, debug. Do NOT just explain — execute the workflow.
检测技术栈、查找文档、实现功能、调试。请勿仅做解释——请执行以下工作流程。

Workflow (Execute This)

工作流程(请执行以下步骤)

Step 1: Detect framework and UI stack

步骤1:检测框架与UI技术栈

Run detection (see
references/framework-detection.md
):
bash
ls wxt.config.ts plasmo.config.ts vite.config.ts manifest.json 2>/dev/null
cat package.json | grep -E '"wxt|plasmo|crxjs|react|vue|svelte"'
File foundFramework
wxt.config.ts
WXT
package.json
has
plasmo
Plasmo
vite.config
has
@crxjs
CRXJS
manifest.json
in root, none above
Vanilla
运行检测脚本(详见
references/framework-detection.md
):
bash
ls wxt.config.ts plasmo.config.ts vite.config.ts manifest.json 2>/dev/null
cat package.json | grep -E '"wxt|plasmo|crxjs|react|vue|svelte"'
找到的文件框架
wxt.config.ts
WXT
package.json
中包含
plasmo
Plasmo
vite.config
中包含
@crxjs
CRXJS
根目录下有
manifest.json
,且无上述文件
原生(Vanilla)

Step 2: Fetch framework docs and style guides

步骤2:获取框架文档与风格指南

Step 3: Implement the feature

步骤3:实现功能

Follow framework conventions:
  • WXT: add entrypoints in
    entrypoints/
    , configure
    wxt.config.ts
  • Plasmo: add pages/tabs/contents, use CSUI for content UI
  • CRXJS: standard Vite + manifest-based setup
  • Vanilla: edit
    manifest.json
    , add JS files directly
For Chrome API usage, check
references/chrome-api-quick-reference.md
. For messaging between contexts, check
references/message-passing-patterns.md
.
遵循框架约定:
  • WXT: 在
    entrypoints/
    目录下添加入口文件,配置
    wxt.config.ts
  • Plasmo: 添加pages/tabs/contents,使用CSUI构建内容UI
  • CRXJS: 标准Vite + 基于manifest的配置
  • 原生(Vanilla): 编辑
    manifest.json
    ,直接添加JS文件
关于Chrome API的使用,请查看
references/chrome-api-quick-reference.md
。 关于上下文间的消息传递,请查看
references/message-passing-patterns.md

Step 4: Debug

步骤4:调试

See
references/debugging-guide.md
for context-specific DevTools access.
Quick map:
ContextHow to inspect
Service worker
chrome://extensions
→ "inspect" link
Content scriptPage DevTools → Console → select extension context
PopupRight-click popup → Inspect
Options / Side panelOpen page → F12
查看
references/debugging-guide.md
获取各上下文环境的DevTools访问方法。
快速指南:
上下文环境检查方法
Service worker
chrome://extensions
→ 点击“inspect”链接
Content script页面DevTools → 控制台 → 选择扩展上下文
Popup右键点击popup → 检查
选项 / 侧边栏打开页面 → 按F12

Step 5: Hot reload setup

步骤5:热重载配置

FrameworkCommandNotes
WXT
pnpm dev
Full HMR, auto-reloads extension
Plasmo
pnpm dev
HMR for popup/content
CRXJS
vite dev
Vite HMR
VanillaManualReload at
chrome://extensions

框架命令说明
WXT
pnpm dev
完整HMR,自动重载扩展
Plasmo
pnpm dev
支持popup/content的HMR
CRXJS
vite dev
Vite HMR
原生(Vanilla)手动操作
chrome://extensions
页面重载

Chrome API Quick Reference (Top 20)

Chrome API速查(Top 20)

APIPurposePermission
chrome.tabs
Query, create, update tabs
tabs
chrome.windows
Manage browser windows
windows
chrome.storage.local
Persist data locally
storage
chrome.storage.sync
Sync data across devices
storage
chrome.storage.session
Session-only data
storage
chrome.action
Toolbar icon, popup, badge
chrome.contextMenus
Right-click menu items
contextMenus
chrome.sidePanel
Side panel UI
sidePanel
chrome.notifications
Desktop notifications
notifications
chrome.scripting
Inject scripts/CSS into pages
scripting
chrome.runtime
Messaging, lifecycle, manifest
chrome.webRequest
Intercept/modify network requests
webRequest
chrome.declarativeNetRequest
Block/redirect requests declaratively
declarativeNetRequest
chrome.webNavigation
Track page navigation events
webNavigation
chrome.identity
OAuth2, Google Sign-In
identity
chrome.alarms
Schedule periodic tasks
alarms
chrome.commands
Keyboard shortcuts
commands
chrome.cookies
Read/write cookies
cookies
chrome.history
Browser history access
history
chrome.bookmarks
Read/write bookmarks
bookmarks

API用途权限
chrome.tabs
查询、创建、更新标签页
tabs
chrome.windows
管理浏览器窗口
windows
chrome.storage.local
本地持久化存储数据
storage
chrome.storage.sync
跨设备同步数据
storage
chrome.storage.session
会话级临时数据存储
storage
chrome.action
工具栏图标、popup、徽章
chrome.contextMenus
右键菜单项
contextMenus
chrome.sidePanel
侧边栏UI
sidePanel
chrome.notifications
桌面通知
notifications
chrome.scripting
向页面注入脚本/CSS
scripting
chrome.runtime
消息传递、生命周期、manifest
chrome.webRequest
拦截/修改网络请求
webRequest
chrome.declarativeNetRequest
声明式阻止/重定向请求
declarativeNetRequest
chrome.webNavigation
跟踪页面导航事件
webNavigation
chrome.identity
OAuth2、Google登录
identity
chrome.alarms
调度周期性任务
alarms
chrome.commands
键盘快捷键
commands
chrome.cookies
读写Cookie
cookies
chrome.history
访问浏览器历史
history
chrome.bookmarks
读写书签
bookmarks

Message Passing (Quick Pattern)

消息传递(快速模式)

One-time (popup → service worker):
ts
// sender
const response = await chrome.runtime.sendMessage({ type: 'GET_DATA' });

// receiver (background)
chrome.runtime.onMessage.addListener((msg, sender, reply) => {
  if (msg.type === 'GET_DATA') { reply({ data: '...' }); return true; }
});
Content script → service worker: same pattern,
return true
for async. Tab-targeted (background → content):
ts
chrome.tabs.sendMessage(tabId, { type: 'ACTION' });
Full patterns in
references/message-passing-patterns.md
.

一次性消息(popup → service worker):
ts
// 发送方
const response = await chrome.runtime.sendMessage({ type: 'GET_DATA' });

// 接收方(后台)
chrome.runtime.onMessage.addListener((msg, sender, reply) => {
  if (msg.type === 'GET_DATA') { reply({ data: '...' }); return true; }
});
Content script → service worker: 模式相同,异步场景需
return true
定向标签页(后台 → content):
ts
chrome.tabs.sendMessage(tabId, { type: 'ACTION' });
完整模式请查看
references/message-passing-patterns.md

References

参考资料

  • references/framework-detection.md
    — detect WXT/Plasmo/CRXJS/vanilla, dev commands
  • references/framework-styleguides.md
    — UI framework docs, style guides, extension framework docs
  • references/chrome-api-quick-reference.md
    — 30+ APIs with permissions and doc links
  • references/message-passing-patterns.md
    — all messaging patterns with TypeScript examples
  • references/debugging-guide.md
    — DevTools per context, common errors, tips
  • references/framework-detection.md
    — 检测WXT/Plasmo/CRXJS/原生扩展,开发命令
  • references/framework-styleguides.md
    — UI框架文档、风格指南、扩展框架文档
  • references/chrome-api-quick-reference.md
    — 30+个API,包含权限与文档链接
  • references/message-passing-patterns.md
    — 所有消息传递模式,附TypeScript示例
  • references/debugging-guide.md
    — 各上下文环境的DevTools使用、常见错误与技巧

Related Skills

相关技能

  • extension-create
    — scaffold new extension with WXT
  • extension-manifest
    — generate/validate manifest.json
  • extension-test
    — write and run extension tests
  • extension-analyze
    — analyze existing extension codebase
  • extension-create
    — 使用WXT快速搭建新扩展
  • extension-manifest
    — 生成/验证manifest.json
  • extension-test
    — 编写并运行扩展测试
  • extension-analyze
    — 分析现有扩展代码库