declarative_net_request that redirects xmlhttprequest issue

118 Views Asked by At

I am having trouble getting a chrome extension that redirects REST API calls from one host to another. Specifically, trying to redirect POSTs that go to https://api.old.com/endpoint to get redirected to https://api.new.com/newapi.

The onRuleMatchedDebug never gets called here, and I have not found any solutions or example on the web so far. Anyone have any ideas?

manifest.json:

{
  "name": "Foo",
  "version": "1.0",
  "manifest_version": 3,
  "declarative_net_request": {
    "rule_resources": [
      {
        "id": "ruleset_1",
        "enabled": true,
        "path": "rules_1.json"
      }
    ]
  },
  "permissions": [
    "declarativeNetRequest",
    "declarativeNetRequestFeedback",
    "declarativeNetRequestWithHostAccess"
  ],
  "background": {
    "service_worker": "serviceWorker.js"
  },
  "host_permissions": ["https://api.old.com/*", "https://api.new.com/*"]
}

rules_1.json:

[
  {
    "id": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "url": "https://api.new.com/newapi"
      }
    },
    "condition": {
      "urlFilter": "https://api.old.com/endpoint",
      "resourceTypes": ["xmlhttprequest"],
      "requestMethods": ["post"]
    }
  }
]

Service worker:

'use strict';

chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((e) => {
  const msg = `Navigation to ${e.request.url} redirected on tab ${e.request.tabId}.`;
  console.log(msg, e);
});

console.log('Service worker started!.');
0

There are 0 best solutions below