【问题标题】:MV3 declarativeNetRequest Block not replacing webRequest properlyMV3 declarativeNetRequest 块没有正确替换 webRequest
【发布时间】:2022-09-30 06:21:06
【问题描述】:

我有一个非常简单的 webRequest 阻塞操作,可以在 MV3 上完美运行,但是通过迁移到 MV3 declarativeNetRequestit 不起作用

对于这个简单的操作,declarativeNetRequest 上的 webRequest 的等价物是什么:

chrome.webRequest.onBeforeRequest.addListener(
  function(){ return {cancel: true}; },
  {
    urls: ["*://*.example.com/mwlite/service-worker.js", "*://*.example.com/mwlite/manifest.json"]
  },
  ["blocking"]
);

在 MV3 上,我有:

chrome.declarativeNetRequest.updateSessionRules(
    {addRules:[{
        "id": id,
        "priority": 1,
        "action": {     "type": "block"
        },
        "condition": {"urlFilter": "*://*.example.com/mwlite/service-worker.js", "resourceTypes": ["image",
        "media",
        "main_frame",
        "sub_frame",
        "stylesheet",
        "script",
        "font",
        "xmlhttprequest",
        "ping",
        "websocket",
        "other"], "tabIds" : tabMultiId }}    
        ],
        removeRuleIds: [id]
    },
);

我已经尝试过了,但它根本不起作用! 问候并非常感谢任何试图提供帮助的人。

【问题讨论】:

  • 在测试以前的代码时,您可能有旧规则的剩余部分,因此请尝试使用 getSessionRules/getDynamicRules + updateSessionRules/updateDynamicRules 删除它们。另请注意,Chrome 不会取消注册该站点当前注册的 Service Worker。您可以使用 chrome.browsingData API 手动完成。

标签: google-chrome google-chrome-extension firefox-addon


【解决方案1】:

请检查您的配置,因为它适用于以下配置。

清单.json

{
  "name": "hoge",
  "version": "1.0",
  "manifest_version": 3,
  "permissions": [
    "declarativeNetRequest",
    "declarativeNetRequestFeedback"
  ],
  "host_permissions": [
    "<all_urls>"
  ],
  "action": {
    "default_popup": "popup.html"
  }
}

popup.html

<!DOCTYPE html>
<html>

<body>
  <script src="myscript.js"></script>
</body>

</html>

myscript.js

const id = 1;

chrome.declarativeNetRequest.updateSessionRules(
  {
    addRules: [{
      "id": id,
      "priority": 1,
      "action": {
        "type": "block"
      },
      "condition": {
        "urlFilter": "*://*.yahoo.co.jp", "resourceTypes": ["image",
          "media",
          "main_frame",
          "sub_frame",
          "stylesheet",
          "script",
          "font",
          "xmlhttprequest",
          "ping",
          "websocket",
          "other"]
      }
    }
    ],
    removeRuleIds: [id]
  },
);

【讨论】:

    猜你喜欢
    • 2015-10-31
    • 2023-02-04
    • 2022-07-15
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多