1
votes

I'm trying to call an API when the "http-on-modify-request" fire in my firefox addon.

So for now, here is my code :

            Request({
              url: "https://myAPI.com/?q="+query,
              onComplete: function (response) {
                var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
                httpChannel.redirectTo(ioService.newURI(response.json.Redirect, null, null));
              }
            }).get();

Unfortunately, as it's asynchronous, the first query which fire the observer is already done, so it can't redirect.

Is there another way I can do that ?

1
Why not abort the redirecting and then do the redirect? So like httpChannel.cancel(Cr.NS_BINDING_ABORTED); see here: stackoverflow.com/questions/25327282/… - Noitidart
I can't do that because the httpChannel will be canceled, so I can't redirect it later... - hw-f-nico
You're right. So abort the channel and then replace it with new channel. That should work. But actually: I thought though that httpChannel.redirectTo aborts whatever its doing and then sends it elsewhere? - Noitidart
Thank you Noitidart, I tried like that but it doesn't work either - hw-f-nico
Don't do newChannel just to channel.redirectTo like in this topic: stackoverflow.com/questions/25327282/… - Noitidart

1 Answers

0
votes

You should instead fetch a list of redirects periodically from myAPI.com, then cache the list locally and use that when "http-on-modify-request" fires.