英文:
Chrome Extension Manifest Version 3 custom user agent is not getting set
问题
以下是翻译好的部分:
我们有一个仅包含2个文件的扩展:manifest.json和background.js
浏览器(chrome版本112)没有报告任何错误,但用户代理未设置为my-custom-user-agent
。以下是完整的扩展代码:
manifest.json
{
"name": "ua-customizer",
"version": "1",
"manifest_version": 3,
"permissions": [
"declarativeNetRequest"
],
"host_permissions": [
"<all_urls>"
],
"background": {
"service_worker": "background.js"
}
}
background.js
const rules = {
removeRuleIds: [1],
addRules: [
{
id: 1,
priority: 1,
action: {
type: 'modifyHeaders',
requestHeaders: [
{
header: 'user-agent',
operation: 'set',
value: `my-custom-user-agent`,
},
],
},
condition: {
urlFilter: '<all_urls>'
},
},
],
}
chrome.declarativeNetRequest.updateDynamicRules(rules);
它缺少什么?
英文:
We have an extension with only 2 files: manifest.json and background.js
The browser (chrome version 112) doesn't report any error, but the user agent is not getting set to `my-custom-user-agent. Here is the complete extension code:
manifest.json
{
"name": "ua-customizer",
"version": "1",
"manifest_version": 3,
"permissions": [
"declarativeNetRequest"
],
"host_permissions": [
"<all_urls>"
],
"background": {
"service_worker": "background.js"
}
}
background.js
const rules = {
removeRuleIds: [1],
addRules: [
{
id: 1,
priority: 1,
action: {
type: 'modifyHeaders',
requestHeaders: [
{
header: 'user-agent',
operation: 'set',
value: `my-custom-user-agent`,
},
],
},
condition: {
urlFilter: '<all_urls>'
},
},
],
}
chrome.declarativeNetRequest.updateDynamicRules(rules);
What is it missing?
答案1
得分: 2
Changes:
- 将
urlFilter: '<all_urls>'
替换为urlFilter: '*'
,因为<all_urls>
在urlFilter中不被支持。请查看chrome.declarativeNetRequest > RuleCondition > urlFilter - 在RuleCondition中添加
resourceTypes: [ 'main_frame' ]
。根据您在扩展中使用的网站,您可能需要添加其他资源类型,但对于https://www.whatismybrowser.com/detect/what-is-my-user-agent/,仅使用"main_frame"即可。
更改后的代码:
condition: {
resourceTypes: [ 'main_frame' ],
urlFilter: '*'
},
这两个更改都是必要的。如果您只进行其中一个更改,扩展将不会修改用户代理字符串。
此外,chrome.declarativeNetRequest.updateDynamicRules(rules);
只需要执行一次。请参考chrome.declarativeNetRequest > updateDynamicRules: "这些规则在浏览器会话和扩展更新之间保持不变。"我建议将其放在chrome.runtime.onInstalled
处理程序中:
chrome.runtime.onInstalled.addListener(function(details) {
chrome.declarativeNetRequest.updateDynamicRules(rules);
});
英文:
Changes:
- Replace
urlFilter: '<all_urls>'
withurlFilter: '*'
, because <all_urls> is not a supported construct in urlFilter. See chrome.declarativeNetRequest > RuleCondition > urlFilter - Add
resourceTypes: [ 'main_frame' ]
to the RuleCondition. Maybe you need to add other resource types, depending on the websites where you're using the extension. But "main_frame" is enough for https://www.whatismybrowser.com/detect/what-is-my-user-agent/
Changed code:
condition: {
resourceTypes: [ 'main_frame' ],
urlFilter: '*'
},
Both changes are necessary.
If you make only one of the changes, the extension will not modify the user-agent string.
Also, chrome.declarativeNetRequest.updateDynamicRules(rules);
only needs to be executed once.
See chrome.declarativeNetRequest > updateDynamicRules: "These rules are persisted across browser sessions and across extension updates."
I recommend putting it in a chrome.runtime.onInstalled
handler:
chrome.runtime.onInstalled.addListener(function(details) {
chrome.declarativeNetRequest.updateDynamicRules(rules);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论