0

I have a very simple webRequest blocking operation that works perfectly on MV3 but by migrating to MV3 declarativeNetRequestit doesn't work

What is the equivalent of the webRequest on the declarativeNetRequest for this simple operation:

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

On MV3 I have:

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

I have tried this and it simply doesn't work! Greetings and thank you very much for anyone that try to help.

user1327579
  • 53
  • 1
  • 5
  • You may have leftovers of the old rules while you were testing previous code so try removing them using getSessionRules/getDynamicRules + updateSessionRules/updateDynamicRules. Also note that Chrome doesn't unregister the currently registered service worker of the site. You can do it manually using chrome.browsingData API. – wOxxOm Sep 30 '22 at 19:56
  • Hi @wOxxOm you are right it worked. Can you please take a look to this other issue: https://stackoverflow.com/questions/74391398/mv3-declarativenetrequest-and-x-frame-options-deny – user1327579 Nov 10 '22 at 15:24

1 Answers1

1

Please check your configuration as it works correctly with the following configuration.

manifest.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]
  },
);
Norio Yamamoto
  • 1,495
  • 2
  • 3
  • 10
  • Hi @Norio Yamamoto you example is right. It works. But my adventure with declarativeNetRequest. It is not working is this other case: https://stackoverflow.com/questions/74391398/mv3-declarativenetrequest-and-x-frame-options-deny – user1327579 Nov 10 '22 at 15:26