修改请求标头
实施修改 HTTP 请求标头的规则,为发出的请求实现自定义行为或身份验证
修改请求标头 是 CoddyKit 上的免费 Browser Extensions Development (Chrome & Edge) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Browser Extensions Development (Chrome & Edge) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What are Request Headers?
When your browser talks to a website, it sends messages called HTTP requests. These requests carry extra info, like metadata, in something called headers.
Headers tell the server things like what browser you're using (User-Agent), what kind of content you expect (Accept), or even authentication tokens (Authorization).
Efficient Header Control
We learned about declarativeNetRequest for blocking and redirecting. It's powerful because it lets the browser handle rules efficiently, without your extension's JavaScript always running.
Today, we'll use it to modify these request headers, adding, changing, or removing them before a request even leaves your browser!
Introducing `modifyHeaders`
To change headers, we use a specific actionType within our declarativeNetRequest rules: "modifyHeaders".
This action takes an array of requestHeaders. Each item in this array describes a single header modification you want to make.
Header Operations Explained
For each header modification, you specify an operation:
"set": Replaces an existing header's value or adds it if it doesn't exist."append": Adds a new value to a header. If the header already exists, it will have multiple values. If not, it adds it."remove": Deletes a header entirely from the request.
Essential Permissions
To use declarativeNetRequest for header modification, your extension needs specific permissions in its manifest.json file:
"declarativeNetRequest": The basic permission to use the API."declarativeNetRequestWithHostAccess": Needed if your rules modify headers on specific websites (hosts). This is usually the case!
Add a Custom Header
Let's add a custom header called X-CoddyKit-User to all outgoing requests. This could be useful for identifying requests from your extension.
This code goes into your extension's background.js file.
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1],
addRules: [{
id: 1,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [{
header: "X-CoddyKit-User",
operation: "set",
value: "CoddyKitStudent"
}]
},
condition: {
urlFilter: "<all_urls>",
resourceTypes: ["main_frame", "sub_frame", "xmlhttprequest"]
}
}]
});
// In a real extension, this runs once on install/update.
// You'd observe the header in your browser's DevTools Network tab.Dissecting the Rule
The code uses chrome.declarativeNetRequest.updateDynamicRules to manage rules. Here's a quick breakdown:
id: A unique number for your rule.priority: Determines which rule wins if multiple rules apply.action: Defines what to do. Here,"modifyHeaders"with ourrequestHeaders.condition: Specifies when the rule applies (e.g.,<all_urls>for everywhere).
Change the User-Agent
You can use "set" to change standard headers too. Here, we'll modify the User-Agent header for requests going to example.com. This spoofs your browser's identity for that site.
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [2],
addRules: [{
id: 2,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [{
header: "User-Agent",
operation: "set",
value: "CoddyKitBrowser/1.0 (Extension)"
}]
},
condition: {
urlFilter: "*://example.com/*",
resourceTypes: ["main_frame", "sub_frame"]
}
}]
});
// This rule would change the User-Agent for example.com.Remove a Header
Sometimes, you might want to remove a header for privacy or to prevent certain tracking. Let's remove the Referer header for all image requests.
The Referer header tells a website where you came from.
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [3],
addRules: [{
id: 3,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [{
header: "Referer",
operation: "remove"
}]
},
condition: {
urlFilter: "<all_urls>",
resourceTypes: ["image"]
}
}]
});
// This rule would remove the Referer header for image requests.Quick Check: Header Operations
You want to add an API key as a custom header, X-API-Key, to a request. If the header somehow already exists, you want to make sure your key is also present, possibly as a second value, rather than replacing an existing one.
Recap & Next Steps
Great job! You've learned how to powerfully modify HTTP request headers using declarativeNetRequest.
- We explored the
"modifyHeaders"action. - Understood the
"set","append", and"remove"operations. - Saw how to add custom headers, change user agents, and remove sensitive info.
- Remember the necessary permissions:
declarativeNetRequestanddeclarativeNetRequestWithHostAccess.
Next, we'll dive into redirecting and rewriting URLs!
常见问题解答
「修改请求标头」课时是免费的吗?
是的 — 「修改请求标头」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Browser Extensions Development (Chrome & Edge) 课程的其余内容,请升级到 CoddyKit PRO。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。
「修改请求标头」这节课中我会学到什么?
实施修改 HTTP 请求标头的规则,为发出的请求实现自定义行为或身份验证 你通过在浏览器中直接运行的动手代码来练习 Browser Extensions Development (Chrome & Edge),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Browser Extensions Development (Chrome & Edge) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Browser Extensions Development (Chrome & Edge) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「修改请求标头」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Browser Extensions Development (Chrome & Edge) 课中编写并运行代码吗?
能。每节 Browser Extensions Development (Chrome & Edge) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。