Make Chrome extension not work in a specific URL

204 Views Asked by At

I have this manifest.json

{
    "name": "Redirect Example",
    "description": "Help with redirection errors!",
    "version": "1.0",
    "manifest_version": 3,
    "background": {
      "service_worker": "background.js"
    },
    "permissions": [
        "declarativeNetRequest"
    ],
    "host_permissions": [
      "https://www.example.com/*",
      "http://example.com/*",
      "https://example.com/*"
    ],
    "declarative_net_request": {
      "rule_resources" : [{
          "id": "rules",
          "enabled": true,
          "path": "rules.json"
      }]
      }
  }

and this rules.json

[
    {
        "id" : 1,
        "priority": 1,
        "action" : {
            "type" : "redirect",
            "redirect" : {
                "transform": { "scheme": "http", "host": "www.example.com" }
            }
        },
        "condition" : {
            "urlFilter" : "https?://example.com/",
            "resourceTypes" : ["main_frame", "sub_frame", "xmlhttprequest"]
        }
    },
    {
        "id" : 3,
        "priority": 3,
        "action" : {
            "type" : "redirect",
            "redirect" : {
                "transform": { "scheme": "http" }
            }
        },
        "condition" : {
            "urlFilter" : "https://www.example.com/",
            "resourceTypes" : ["main_frame", "sub_frame", "xmlhttprequest"]
        }
    }
]

Whoever runs the site is really bad at making things work with https so I redirect always to http://www.example.com and it works 99% of the time.

But there's a specific URL where the website enforces https so I'm in a redirection loop, is there any way to exclude that URL so that the extension either ignores that URL or simply doesn't load?

Edit:

I've tried this in background.js

if (location.href.indexOf("problematic/url") !== -1) {
    chrome.declarativeNetRequest.updateEnabledRulesets({"disableRulesetIds": ["rules"]});
} else {
    chrome.declarativeNetRequest.updateEnabledRulesets({"enableRulesetIds": ["rules"]});
}
chrome.declarativeNetRequest.getEnabledRulesets(rulesetIds => console.log(rulesetIds));

But I believe that by the time the js would be executed the redirection loop has already started.

0

There are 0 best solutions below