Search code examples
google-chrome-extension

Why does URL redirect permission work in Chrome Manifest v2 but not v3?


I'm trying to redirect a URL using the Chrome declarativeWebRequest API but it does not work.

The match pattern in the "permissions" key worked with Manifest V2 but it's now throwing a Permission '*://www.youtube.com/*' is unknown or URL pattern is malformed error in V3.

manifest.json:

{
  "manifest_version": 3,
  "name": "Redirect Test",
  "version": "0.0.1",
  "permissions": [
    "declarativeNetRequest",
    "*://www.youtube.com/*"
  ],
  "host_permissions": [
    "*://www.youtube.com/*"
  ],
  "declarative_net_request": {
    "rule_resources": [
      {
        "id": "1",
        "enabled": true,
        "path": "rules.json"
      }
    ]
  }
}

rules.json:

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

I'm using Chrome 88.0.4324.104


Solution

  • The manifest documentation for declarative net requests at the time of posting isn't exactly accurate.

    This is the Permissions key in manifest.json from the documentation:

    "permissions": [
        "declarativeNetRequest",
        "declarativeNetRequestFeedback",
        "*://example.com/*"
      ],
    

    However, the site should be specified in just host_permissions instead:

    "host_permissions": [
        "*://example.com/*"
      ],
    

    It should be noted that this explicit declaration is only necessary if action.redirect is specified like in this example:

    {
            "id": 1,
            "priority": 3,
            "action": {
                "type": "redirect",
                "redirect": {
                    "regexSubstitution": "www.youtube.com/embed/"
                }
            },
            "condition": {
                "regexFilter": "(www\\.youtube\\.com\/watch\\?v=)",
                "resourceTypes": [
                    "main_frame"
                ]
            }
    }