1

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 ?

hw-f-nico
  • 13
  • 2
  • Why not abort the redirecting and then do the redirect? So like `httpChannel.cancel(Cr.NS_BINDING_ABORTED);` see here: http://stackoverflow.com/questions/25327282/why-is-this-javascript-page-redirect-so-slow/25328750#25328750 – Noitidart Nov 13 '14 at 06:16
  • I can't do that because the httpChannel will be canceled, so I can't redirect it later... – hw-f-nico Nov 14 '14 at 19:45
  • 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 Nov 14 '14 at 22:34
  • Thank you Noitidart, I tried like [that](http://pastebin.com/FE1mb9jC) but it doesn't work either – hw-f-nico Dec 01 '14 at 20:12
  • Don't do newChannel just to channel.redirectTo like in this topic: http://stackoverflow.com/questions/25327282/why-is-this-javascript-page-redirect-so-slow/25328750#25328750 – Noitidart Dec 01 '14 at 21:35

1 Answers1

0

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.

erikvold
  • 15,988
  • 11
  • 54
  • 98