Skip to content
Advertisement

BlockingResponse in Firefox extension

I’m attempting to redirect a user in a Firefox extension like so:

browser.webRequest.onBeforeRequest.addListener(
  ({ url }) => {
    const [fullMatch, type, identifier] =
      url.match(
        /open.spotify.com/(track|album|artist|playlist|concert|episode|show|user)/([^&#/?]+)/i
      ) || [];

    return { redirectUrl: `spotify:${type}:${identifier}` };
  },
  {
    urls: ["*://open.spotify.com/track/*", "*://open.spotify.com/album/*",
  "*://open.spotify.com/artist/*", "*://open.spotify.com/playlist/*",
  "*://open.spotify.com/concert/*", "*://open.spotify.com/episode/*",
  "*://open.spotify.com/show/*", "*://open.spotify.com/user/*"],
    types: ["xmlhttprequest"],
  },
  ["blocking"]
);

I’ve added webRequest and webRequestBlocking permissions in the manifest. In the debugger, I see that I am reaching the return statement with the redirectUrl correctly set, but the webpage does not redirect. I would assume this should redirect based upon the webRequest documentation, however the temporary extension does not seem to redirect. Any ideas on how to get the extension to redirect? Changing the url to https://www.google.com, for example, doesn’t work either, so it seems the issue is not with the URL.

Advertisement

Answer

After some testing, I’ve determined the issue is with the types array. xmlhttprequest does not capture certain requests in Firefox, but does in Chrome. In order to still be able to redirect, the types should be as follows:

  types: [
  "main_frame",
  "xmlhttprequest"]
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement