diff --git a/.cursor/rules/main.mdc b/.cursor/rules/main.mdc new file mode 100644 index 0000000..0014815 --- /dev/null +++ b/.cursor/rules/main.mdc @@ -0,0 +1,578 @@ +--- +description: 使用 WXT、React 和 TypeScript 构建代理管理扩展的指南 +globs: +alwaysApply: false +--- +--- +description: 使用 WXT、React 和 TypeScript 构建代理管理扩展的指南 +globs: "**/*.ts, **/*.tsx, **/*.js, **/*.jsx" +--- + +## 概览 + +](https://wxt.dev/) 是一个为浏览器扩展开发提供现代开发体验的框架。本指南将帮助您使用 WXT、React 和 TypeScript 重构类似 SwitchyOmega 的代理管理扩展。 + +## 项目结构 + +推荐使用以下项目结构: +``` +. +├── .output/ +├── .wxt/ +├── modules/ +├── public/ # 包含要复制到输出文件夹的任何文件,而无需WXT处理 +├── ord/ # 需要重构的旧代码 +├── src/ +│ │ ├── assets/ +│ │ ├── components/ +│ │ ├── composables/ +│ │ ├── entrypoints/ # 包含所有被捆绑到扩展名的入口点 +│ │ ├── hooks/ # 默认自动导入,包含项目用于 React 和 Solid 的钩子的源代码 +│ │ ├── utils/ +├── .env +├── .env.publish +├── app.config.ts +├── package.json +├── tsconfig.json +├── web-ext.config.ts # 配置浏览器启动 +├── wxt.config.ts # WXT项目的主要配置文件 + +``` + + +Different browsers provide different global variables for accessing the extension APIs (chrome provides chrome, firefox provides browser, etc). + +WXT merges these two into a unified API accessed through the browser variable. + +``` +import { browser } from 'wxt/browser'; + +browser.action.onClicked.addListener(() => { +// ... +}); +``` +TIP + +With auto-imports enabled, you don't even need to import this variable from wxt/browser! + +The browser variable WXT provides is a simple export of the browser or chrome globals provided by the browser at runtime: + + +export const browser = globalThis.browser?.runtime?.id +? globalThis.browser +: globalThis.chrome; +This means you can use the promise-style API for both MV2 and MV3, and it will work across all browsers (Chromium, Firefox, Safari, etc). + +Accessing Types +All types can be accessed via WXT's Browser namespace: + +``` +import { type Browser } from 'wxt/browser'; + +function handleMessage(message: any, sender: Browser.runtime.MessageSender) { +// ... +} +``` + +## 入口点设置 + +### 后台脚本 + +```typescript +// entrypoints/background/index.ts +import { defineBackground } from 'wxt/background'; +import { setupProxyManagement } from './proxy'; + +export default defineBackground({ + // 设置清单选项 + type: 'module', + + main() { + // 初始化代理管理 + setupProxyManagement(); + + // 监听消息 + browser.runtime.onMessage.addListener((message, sender) => { + if (message.type === 'SWITCH_PROXY') { + return handleProxySwitch(message.proxyId); + } + }); + }, +}); +``` + +### 弹出窗口 + +```html + + + +
+ + +链接面板功能将在未来版本中实现
+