Plasmo 浏览器插件开发框架
Plasmo 是一个可插拔的企业级浏览器插件框架,基于 Webpack + Vite 构建,支持约定式路由、插件体系、微前端等企业级特性。
框架介绍
Plasmo 是一个专为现代浏览器扩展开发设计的框架,它简化了浏览器插件的开发流程,提供了一套完整的工具链和最佳实践。Plasmo 框架采用 React 作为 UI 层,同时支持 TypeScript,让开发者能够使用熟悉的技术栈构建高质量的浏览器扩展。
核心理念
- 开发体验优先:提供热更新、TypeScript 支持和现代化开发工具链
- 约定大于配置:通过文件系统路由和智能默认值减少配置负担
- 企业级可扩展性:支持插件系统、微前端架构和多浏览器兼容
- 性能优化:内置代码分割、Tree-shaking 和资源优化
与其他框架对比
| 特性 | Plasmo | 传统扩展开发 | 其他框架 |
|---|---|---|---|
| 开发体验 | 热更新、TypeScript | 手动刷新、原生JS | 部分支持热更新 |
| 路由系统 | 约定式路由 | 手动配置 | 多种配置方式 |
| 构建系统 | Webpack + Vite | 手动打包 | 多种构建工具 |
| 跨浏览器支持 | 内置适配 | 手动适配 | 部分支持 |
| 微前端支持 | 原生支持 | 不支持 | 少数支持 |
| 社区生态 | 活跃成长中 | 分散 | 依赖框架而定 |
安装与使用
快速开始
# 安装 Plasmo CLI
npm install -g plasmo
# 创建新项目
plasmo init my-extension
# 进入项目目录
cd my-extension
# 启动开发服务器
npm run dev
项目结构
my-extension/
├── .plasmo/ # 构建缓存和临时文件
├── assets/ # 静态资源
├── node_modules/ # 依赖包
├── popup/ # 弹出窗口相关组件
├── background.ts # 后台脚本
├── content.ts # 内容脚本
├── options.tsx # 选项页面
├── package.json # 项目配置
└── plasmo.config.ts # Plasmo 配置文件
部署与发布
# 构建生产版本
npm run build
# 打包为 zip 文件
npm run package
生成的扩展包可以上传到 Chrome Web Store、Firefox Add-ons 或其他浏览器的扩展商店。
核心功能
1. 开发模式
// plasmo.config.ts
export default {
manifest: {
name: "我的浏览器插件",
version: "1.0.0",
permissions: ["storage", "activeTab"]
},
// 支持热更新
dev: {
hmr: true
}
}
2. 构建配置
// 支持多入口配置
build: {
outDir: "dist",
sourcemap: process.env.NODE_ENV === "development",
minify: !process.env.DEBUG
}
3. 约定式路由
├── popup.tsx # 弹出窗口 UI
├── options.tsx # 选项页面
├── background.ts # 后台脚本
├── content.ts # 内容脚本
└── assets/ # 静态资源
通过文件系统自动生成对应的扩展组件,无需手动配置路由。
4. 插件体系
// 示例:内容脚本插件
import { defineContentScript } from "plasmo"
export default defineContentScript({
matches: ["https://*.example.com/*"],
css: ["styles.css"],
main: () => {
console.log("内容脚本已加载")
}
})
5. 状态管理
// 使用内置状态管理
import { Storage } from "@plasmohq/storage"
const storage = new Storage()
// 存储数据
await storage.set("key", "value")
// 读取数据
const value = await storage.get("key")
// 监听变化
storage.watch({
key: "key",
callback: (newValue) => {
console.log("值已更新:", newValue)
}
})
典型使用场景
1. 跨浏览器支持
// manifest.json
{
"manifest_version": 3,
"browser_specific_settings": {
"gecko": {
"id": "extension@example.com",
"strict_min_version": "91.0"
}
}
}
2. 微前端集成
// 远程组件加载
import { loadComponent } from "plasmo"
const RemoteButton = await loadComponent(
"https://cdn.example.com/button.js"
)
3. 国际化支持
// i18n.ts
import { createI18n } from "@plasmohq/i18n"
export const i18n = createI18n({
en: {
hello: "Hello",
welcome: "Welcome to my extension"
},
zh: {
hello: "你好",
welcome: "欢迎使用我的扩展"
}
})
// 在组件中使用
import { i18n } from "./i18n"
function Popup() {
return (
<div>
<h1>{i18n.t("hello")}</h1>
<p>{i18n.t("welcome")}</p>
</div>
)
}
浏览器存储与安全
1. 浏览器存储 API
localStorage 和 sessionStorage
// 使用原生 Web Storage API
const saveToLocalStorage = () => {
// 存储数据
localStorage.setItem("user_preference", JSON.stringify({
theme: "dark",
fontSize: "medium"
}))
// 读取数据
const preferences = JSON.parse(localStorage.getItem("user_preference") || "{}")
// 删除数据
localStorage.removeItem("user_preference")
// 清空所有数据
localStorage.clear()
}
// 使用 Plasmo 封装的存储 API
import { Storage } from "@plasmohq/storage"
const localStorage = new Storage({ area: "local" })
const sessionStorage = new Storage({ area: "session" })
// 存储和获取数据
await localStorage.set("key", { complex: "value" })
const value = await localStorage.get("key")