declarativenetrequest not working with manifest v3

Viewed 18

I want to redirect the website using chrome Extension manifest version v3. I went though multiple articles but not getting a working solution.

For simplicity let us say if someone opens google.com I want to redirect to bing.com. I tried below code , but it's not working.

Can someone help me with the fix.

empty_rules.json

[{
  "id": 1,
  "priority": 1,
  "action": {
    "type": "redirect",
    "redirect": {
      "url" : "http://www.yahoo.com"
    }
  },
  "condition": {
    "urlFilter": "google.com",
    "resourceTypes": ["main_frame"]
  }
}]

manifest.json

background": {
    "service_worker": "background.js"
  },
"permissions": ["nativeMessaging", "declarativeNetRequest","declarativeNetRequestFeedback"],
"host_permissions": [
    "http://*/*", "https://*/*"
  ],
"manifest_version": 3,
"declarative_net_request" : {
    "rule_resources" : [{
      "id": "ruleset_1",
      "enabled": true,
      "path": "empty_rules.json"
  }]
}

Wrote the code the check the rules been executed in background.js as

chrome.declarativeNetRequest.onRuleMatchedDebug.addListener(
    (e) => console.log("onRuleMatchedDebug" + JSON.stringify(e))
);

and got the output as below :

{"request": {"documentLifecycle":"active","frameId":0,"frameType":"outermost_frame","method":"GET","parentFrameId":-1,"requestId":"642","tabId":78917798,"type":"main_frame","url":"https://www.google.com/"},"rule":{"ruleId":2,"rulesetId":"_dynamic"}}

Wondering how come the matching rule id is coming as "rule":{"ruleId":2,"rulesetId":"_dynamic"}} , ideally it should match "ruleid","1"

0 Answers
Related