webNavigate doesn't work in safari web extension with tabs.update

I am trying to write a safari web extension that redirects users to Y URL if they type X URL without ever loading the X URL.

The piece of code that I have attached below works smoothly on chrome, but not on safari.

background.js

function onBeforeNavigate(event) {
    const redirectURL = "https://google.com/"
    chrome.tabs.update(event.tabId, { url: redirectURL })
}

chrome.webNavigation.onBeforeNavigate.addListener(onBeforeNavigate,{
    url: [
      {urlMatches: "https://girlcodeit.com/*"},
      {urlMatches: "http://girlcodeit.com/*"}
    ]
  })

manifest.json

    "manifest_version": 2,
    "name": "",
    "description": "",
    "version": "1.0",
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "permissions": [
        "webNavigation",
        "tabs"
    ]
}

I have tried writing browser.tabs.update and just tabs.update in place of chrome.tabs.update in safari version, no change.

I want to achieve the redirection anyhow through a safari web extension, please suggest changes in this one or share any other approaches.

webRequestBlocking is not supported by Safari, so that doesn't work either.

Post not yet marked as solved Up vote post of mt2107 Down vote post of mt2107
1.3k views

Replies

I have a similar issue, but with chrome.webNavigation.onDOMContentLoaded.addListener. The callback is never triggered. On Chrome and Firefox works well.

Have you granted the extension permission to access girlcodeit.com?

Please file a bug report on https://feedbackassistant.apple.com and include a sample extension that reproduces this issue.

I have the same issue

chrome.webNavigation.onDOMContentLoaded and chrome.webNavigation.onCommitted does not trigger events when navigating from the address bar or from a "Start Page" for frameId === 0

Page reload (Cmd + R) or navigating by links works fine.

Safari Version 17.2.1 (19617.1.17.11.12)

MacOS 14.2.1 (23C71)