迁移到 Manifest V3 并精简项目

- manifest.json 升级到 MV3:Service Worker + action,权限改用 declarativeNetRequest
- background.js 重写为 Service Worker,用 DNR 动态/session 规则修改请求/响应头,
  支持 URL 正则、资源类型过滤、Tab 锁、暂停;badge/右键菜单改用 chrome.action/onClicked
- main.js 存储层从 localStorage 迁移到 chrome.storage.local(Proxy 同步适配 + 手动
  bootstrap),并对旧 MV2 数据做一次性自动迁移;改动即时持久化
- popup.html 去掉 404 的 associate.js 与自动 ng-app;移除侧边栏无用入口
- 清理死代码与冗余资源:openCloudBackup、cloudbackupdialog 模板、推广 tips、
  _DS_Store、8 个未引用图标
- README 用中文重写,说明项目来源、MV3 改造与使用方法
This commit is contained in:
呈祥
2026-07-10 15:14:34 +08:00
parent f7e41765c1
commit 5df5fab875
15 changed files with 477 additions and 622 deletions
+64 -44
View File
@@ -1,59 +1,79 @@
# ModHeader browser extension
# ModHeaderManifest V3 版)
<h3 align="center">
ModHeader is a browser extension that allow you to modify HTTP request and response headers.
</h3>
<h3 align="center">
<img src="https://mod-header.appspot.com/android-icon-96x96.png" width="24px" alt="ModHeader" />
<a href="https://mod-header.appspot.com/">
Official Website
</a>
</h3>
<p align="center">
<a href="https://chrome.google.com/webstore/detail/modheader/idgpnmonknjnojddfkpgkljpfnnfcklj">
<img src="https://mod-header.appspot.com/images/chrome_1x.png" srcset="https://mod-header.appspot.com/images/chrome_2x.png 2x">
</a>
<a href="https://addons.mozilla.org/firefox/addon/modheader-firefox/">
<img src="https://mod-header.appspot.com/images/firefox_1x.png" srcset="https://mod-header.appspot.com/images/firefox_2x.png 2x">
</a>
</p>
一个用于**修改 HTTP 请求头和响应头**的浏览器扩展。
## Donation
## 项目来源
If you find ModHeader useful, please consider making a donation. If you use it for your company project, please ask your company to make a monthly donation!
本项目 fork/整理自开源扩展 [ModHeader](https://github.com/hao1300/modheader)(原作者 Hao Nguyen,MIT 许可)。原始代码基于 **Manifest V2**,依赖已被 Chrome 废弃的阻塞式 `webRequest` API。
[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=3XFKZ8PCRB8P6&currency_code=USD&amount=5&source=url)
<a href="https://www.buymeacoffee.com/scCieVdfj">
<img src="https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png" alt="Buy Me A Coffee">
</a>
本仓库在其早期源码基础上做了以下改造:
## Features
- **迁移到 Manifest V3**:后台页改为 Service Worker,请求/响应头修改改用 `declarativeNetRequest`DNR)动态规则实现。
- **存储层迁移**:状态从 `localStorage` 迁移到 `chrome.storage.local`MV3 的 Service Worker 无法访问 `localStorage`),并保留对旧数据的一次性自动迁移。
- **精简**:移除了云备份、捐赠、推广提示等无用功能与相关死代码、冗余资源。
* Add/modify/remove request headers and response headers
* Enable header modification based on URL/resource type
* Add comments to header
* Multiple different profiles
* Sorting headers by name, value, or comments
* Append value to existing request or response header
* Export and import header
* Clone profile
* Cloud backup
* Tab locking!
前端仍保留原有的 AngularJS + Angular Material 界面。
## Screenshots
## 功能
<img src="https://mod-header.appspot.com/images/ss1.png">
- 增加 / 修改 / 删除**请求头**与**响应头**
- 按 **URL 正则**或**资源类型**限定生效范围(Filter)
- 多套 **Profile** 配置,可快速切换
- 给每条 header 添加**备注**
- 按名称 / 值 / 备注**排序**
- 在已有 header 值后**追加**(拼接 / 逗号拼接)
- **导出 / 导入** ProfileJSON
- **克隆** Profile
- **Tab 锁**:仅对指定标签页生效
- **暂停**:一键临时停用全部修改
## Forking and contribution
## 安装 / 本地开发
Feel free to send pull requests to add new features to ModHeader. It will benefit everyone! That said, I reserve the rights to reject pull requests that does not seem useful, or if they add too much complexity for very little benefits.
无需任何构建工具,直接加载 `src` 目录即可:
You may fork and redistribute ModHeader for a small group of friends / colleagues, but please do not impersonate ModHeader, or try to sell it for a profit. If you use ModHeader in any commercial product, please let me know.
1. 浏览器打开 `chrome://extensions`
2. 打开右上角**开发者模式**
3. 点击**加载已解压的扩展程序**,选择本仓库的 `src` 目录
4. 修改代码后,回到该页面点扩展卡片上的**刷新(↻)**即可重新加载
## Installation
## 使用方法
ModHeader does not require any extra tool for building. Simply load the src directory into Chrome / Firefox to start local development.
1. 点击工具栏中的 ModHeader 图标打开弹窗。
2. 点右上角 **+**
- **Request header**:添加请求头(如 `User-Agent``Authorization` 等)
- **Response header**:添加响应头(如 `Access-Control-Allow-Origin`
- **Filter**:添加生效条件(URL 正则或资源类型)
3. 勾选每行左侧的复选框以**启用**该条修改;填写 Name / Value。
4. 打开目标网站并刷新,在 DevTools 的 **Network** 面板中即可确认头已被修改。
5. 图标上的**数字角标**表示当前生效的 header 数量。
## Selenium usage
### 生效范围说明
If you need to use ModHeader for Selenium tests, please visit: https://github.com/hao1300/modheader_selenium
- **不加 Filter**:对所有 URL 生效。
- **URL Pattern**:填写正则(如 `https://example\.com/.*`),仅匹配的请求生效;多条之间为「或」的关系。
- **Resource Type**:仅对选中的资源类型(main_frame、xmlhttprequest 等)生效。
- **Tab 锁**:点弹窗中的 “Tab lock”,修改将只作用于当前标签页。
## 目录结构
```
src/
├── manifest.json # MV3 清单
├── background.js # Service WorkerDNR 规则生成、角标、右键菜单
├── popup.html # 弹窗界面
├── scripts/
│ ├── main.js # AngularJS 逻辑 + storage.local 适配层
│ └── angular*.min.js # AngularJS 1.7.8 及其模块(官方,MIT
├── styles/ # 样式
├── images/ # 图标
└── *.tmpl.html # 各对话框模板
```
## 已知限制
- `declarativeNetRequest``append` 操作对部分请求头有限制,遇到不支持的 header 时该规则会失败并在 Service Worker 控制台打印错误。
- Tab 锁通过 DNR 的 **session 规则** + `tabIds` 实现,浏览器重启后会失效(符合预期)。
## 许可
沿用原项目的 **MIT** 许可。