# chrome-rs **Repository Path**: lo-ferris/chrome-rs ## Basic Information - **Project Name**: chrome-rs - **Description**: Chrome 扩展 MV3 全量 API 的rust 绑定 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-21 - **Last Updated**: 2026-05-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 一、完整项目目录结构 ```plaintext src/ ├── chrome/ │ ├── mod.rs # 总导出 │ ├── runtime.rs # chrome.runtime │ ├── storage.rs # chrome.storage │ ├── tabs.rs # chrome.tabs │ ├── alarms.rs # chrome.alarms │ ├── windows.rs # chrome.windows │ ├── i18n.rs # chrome.i18n │ ├── scripting.rs # chrome.scripting │ ├── web_navigation.rs # chrome.webNavigation │ ├── context_menus.rs # chrome.contextMenus │ ├── notifications.rs # chrome.notifications │ ├── downloads.rs # chrome.downloads │ ├── bookmarks.rs # chrome.bookmarks │ ├── history.rs # chrome.history │ ├── idle.rs # chrome.idle │ ├── permissions.rs # chrome.permissions │ └── action.rs # chrome.action └── lib.rs ``` 二、要求: 1、✅ 100% 官方标准 wasm-bindgen 写法。 2、✅ 全部使用 #[wasm_bindgen(js_namespace = ...)] 3、✅ 官方标准的 Chrome 扩展 MV3 全量 API 封装,不遗漏任何一个API。 4、✅ 完全模块化,一个 Chrome 命名空间对应一个 Rust 文件,比如 chrome.runtime 对应 chrome/runtime.rs,以后新增 API 只需往对应模块追加。 5、✅ 生成的rust代码,函数名是蛇形命名。 6、✅ 100% 类型安全,编译期检查 7、✅ 只要生成的函数,不需要对象。 8、✅ extern "C" 前面加 unsafe,每个函数前面都加 unsafe,每个函数都是 pub unsafe fn 9、✅ 可直接编译为 WASM 运行在 Chrome 扩展后台 10、✅ 核心规则: 1、每个文件只写对应命名空间的 API 2、格式: ```rust use wasm_bindgen::prelude::*; use js_sys::{Function, Promise, JsValue}; use wasm_bindgen::prelude::*; use js_sys::{Object, Promise}; #[wasm_bindgen] unsafe extern "C" { /** 对应的 js 模块路径, 例如 chrome.runtime.getURL 对应的中文注释,例如 发送消息 */ #[wasm_bindgen(js_namespace = ["chrome", "模块名"], js_name = 原JS名)] pub unsafe fn 原JS名,蛇形命名(参数) -> 返回值; } ``` 三、请严格按照上面的要求,生成对应的rust 代码。 关键点说明 事件监听器方法:命名为 on_xxx_add_listener,清晰表达是添加监听器 事件对象:同时导出 ON_XXX 静态对象,便于后续添加 removeListener 等方法 JS 路径:js_namespace = ["chrome", "runtime", "onMessage"] 表示访问 chrome.runtime.onMessage 对象 函数名:js_name = addListener 表示调用该对象的 addListener 方法 ````bash # 下面这些命令有坑,建议使用 oxichrome 中的 打包命令 一行 JS 模块路径 + 一行中文注释,而不是只有 JS 路径。 wasm-pack build --target web --out-dir pkg --verbose # 在项目根目录执行 wasm-pack build --target web --out-dir pkg --release # 或者使用自定义输出路径 wasm-pack build --target web --out-dir pkg --release --out-name chrome_rs ``` 编译成 wasm 建议使用 下面这个库 https://github.com/0xsouravm/oxichrome