0

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.

2
  • redirectUrl must be a proper URL while spotify:${type}:${identifier} doesnt appear to be.
    – erosman
    Commented Dec 10, 2020 at 19:02
  • changing the URL to google.com doesn't work either, I've updated the question to reflect that
    – relisher
    Commented Dec 11, 2020 at 19:47

1 Answer 1

0

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"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.