Chrome扩展程序清单版本3自定义用户代理未设置。

huangapple go评论79阅读模式
英文:

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

{
    &quot;name&quot;: &quot;ua-customizer&quot;,
    &quot;version&quot;: &quot;1&quot;,
    &quot;manifest_version&quot;: 3,
    &quot;permissions&quot;: [
      &quot;declarativeNetRequest&quot;
    ],
    &quot;host_permissions&quot;: [
      &quot;&lt;all_urls&gt;&quot;
    ], 
    &quot;background&quot;: {
      &quot;service_worker&quot;: &quot;background.js&quot;
    }
  }

background.js

const rules = {
  removeRuleIds: [1],
  addRules: [
    {
      id: 1,
      priority: 1,
      action: {
        type: &#39;modifyHeaders&#39;,
        requestHeaders: [
          {
            header: &#39;user-agent&#39;,
            operation: &#39;set&#39;,
            value: `my-custom-user-agent`,
          },
        ],
      },
      condition: {
        urlFilter: &#39;&lt;all_urls&gt;&#39;
      },
    },
  ],
}
chrome.declarativeNetRequest.updateDynamicRules(rules);

What is it missing?

答案1

得分: 2

Changes:

  • urlFilter: &#39;&lt;all_urls&gt;&#39;替换为urlFilter: &#39;*&#39;,因为&lt;all_urls&gt;在urlFilter中不被支持。请查看chrome.declarativeNetRequest > RuleCondition > urlFilter
  • 在RuleCondition中添加resourceTypes: [ &#39;main_frame&#39; ]。根据您在扩展中使用的网站,您可能需要添加其他资源类型,但对于https://www.whatismybrowser.com/detect/what-is-my-user-agent/,仅使用"main_frame"即可。

更改后的代码:

condition: {
	resourceTypes: [ &#39;main_frame&#39; ],
	urlFilter: &#39;*&#39;
},

这两个更改都是必要的。如果您只进行其中一个更改,扩展将不会修改用户代理字符串。

此外,chrome.declarativeNetRequest.updateDynamicRules(rules);只需要执行一次。请参考chrome.declarativeNetRequest > updateDynamicRules: "这些规则在浏览器会话和扩展更新之间保持不变。"我建议将其放在chrome.runtime.onInstalled处理程序中:

chrome.runtime.onInstalled.addListener(function(details) {
	chrome.declarativeNetRequest.updateDynamicRules(rules);
});
英文:

Changes:

Changed code:

condition: {
	resourceTypes: [ &#39;main_frame&#39; ],
	urlFilter: &#39;*&#39;
},

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);
});

huangapple
  • 本文由 发表于 2023年5月7日 04:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定