declarativeNetRequest - redirect action type not supported

I am trying to write a URL-redirection safari web extension.

manifest.json

"declarative_net_request": {
        "rule_resources": [
            {
                "id": "ruleset",
                "enabled": true,
                "path": "rules.json"
            }
        ]
    },
    "permissions": [
        "declarativeNetRequest",
        "webNavigation"
    ]
}

Rules.json

[
    {
        "id": 1,
        "priority": 1,
        "action": {
            "type": "redirect",
            "redirect": {
                "regexSubstitution": "https://example.com/"
            }
        },
        "condition": {
            "regexFilter": "http://*/",
            "resourceTypes": [
                "main_frame"
            ]
        }
    }
]

Expectation - any search should redirect to example.com

Getting this error Non-fatal issue: Rule with id 1 is invalid. redirect is not a supported action type.

The same code works fine in chrome as a chrome extension, giving error in safari browser only.

Is redirect not supported by safari? If no, what other options do i have to achieve it?

Replies

There are other ways to accomplish a redirect within the extension, but none will likely have as good UX by doing it the way you described.

Building out the declarativeNetRequest api in Safari would be really beneficial to developers and I am sure filing feedback would help get that point across.

browser.webRequest could be used, however it is only supported for persistent background pages, which is incompatible with iOS and manifest v3 project. - https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest

You could use a content script to redirect, however depending on the amount of logic in your app, this could be less optimal UX.

You could use browser.tabs.update, but again, less than optimal user experience: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/update

Hopefully Apple will build out the rest of this API for Safari. Here's a somewhat related thread: https://developer.apple.com/forums/thread/682541

  • @jw732

    onBeforeRequest - To cancel or redirect the request, first include "blocking", but blocking is again not supported by safari.browser.tabs.update - I tried using it, please take a look https://developer.apple.com/forums/thread/700800, do you see something wrong with the code due to which its not working.

    Thanks!

Add a Comment