Seeking help for restoring an old URL-rewriting script

Discussion about official Mozilla Firefox builds

User avatar
Posts: 6
Joined: July 8th, 2015, 9:27 am
Location: Sri Lanka

Post Posted February 10th, 2018, 5:43 am

I used to utilize some URL "rewriting" mechanisms to modify some URLs being loaded by FF (e.g. removing certain query parameters, like converting to using nsIObserverService like so (full source at

Code: Select all
   observe: function(aSubject, aTopic, aData) {
      var channel = aSubject.QueryInterface(Ci.nsIHttpChannel);
      if (channel.URI.spec == "") {
         channel.URI.spec = "";
}, "http-on-modify-request", false);

This has been working fine for a long time (even in 59.x nightly with Quantum), until I upgraded to FF 60.0a1 (nightly) a few days ago.

Now, although the code does not throw an error, channel.URI.spec seems to remain unchanged despite reassignment. Inspecting the channel.URI object only shows a getter for spec so it's possible that spec was made read-only (although I could not find any public info relevant to such a change).

I checked the nsIURI docs at and, although some attributes like originCharset are indeed marked as read-only, spec is not marked as such; but then again, the docs haven't been updated since late 2015, so a lot might have changed since then.

I tried to use channel.redirectTo() ( ... edirectTo()) instead:

Code: Select all

but this seems to cause CORS errors for many website resources (e.g. GitHub, as a publicly accessible example):

Code: Select all
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

The channel.redirectTo call is probably resulting in some access control headers not being contained in the response.

Any suggestions as to either
* disabling CORS for a specific set of domains,
* getting the channel.URI.spec = "alternative-url" approach working back again (without downgrading FF), if at all possible, or
* a different way to dynamically rewrite URLs being requested by pages, from within the browser itself
would be much appreciated :)
Last edited by janakaud on February 10th, 2018, 5:50 am, edited 1 time in total.
Please disregard the weird User Agent; trying to save a few bytes.


User avatar
Posts: 39294
Joined: October 19th, 2007, 1:53 am
Location: The South Coast of N.S.W. Oz.

Post Posted February 10th, 2018, 5:47 am

Moving to the Builds forum...
Ancient Amateur Astronomer
Win-7-HP/Intel® DualCore-2.0GHz/500G HDD/4 Gig Ram/550Watt PSU/350WattUPS/Firefox-20.0-62.0-70.0/T-bird-
(Always choose the "Custom" Install.)

Posts: 1
Joined: February 11th, 2018, 7:55 pm

Post Posted February 11th, 2018, 7:58 pm

I think that recently changed as part of the bigger effort to try to make URI parsing threadsafe, in:
It seems that in JS you now have to do this: uri.mutate().setSpec(newSpec).finalize()

Posts: 187
Joined: August 30th, 2013, 3:50 pm

Post Posted February 11th, 2018, 9:41 pm

What I use: ... ctor.uc.js

It uses nsIContentPolicy, it redirects as early as possible, even before http-on-modify-request.

Tested in Fx 59.0b8 (current Developer Edition).


User avatar
Posts: 6
Joined: July 8th, 2015, 9:27 am
Location: Sri Lanka

Post Posted April 1st, 2018, 5:39 am

@wisniewskit Thanks! (And sorry about not seeing your answer this far; I thought MZ would send me an email or something once my question received an answer, but seems like it's not the case :( )

However, I cannot figure out how to reassign the mutated URI back to the original channel.

By the time of posting the question, I had already tried

Code: Select all

which caused the aforementioned CORS issues (e.g. on GitHub pages).

I tried simply doing

Code: Select all

but that didn't do anything (the original URL got loaded, as if no change was made).

Code: Select all
channel.URI = channel.URI.mutate().setSpec("newUrl").finalize();

had the same effect (no change; original URL loads as usual).
Please disregard the weird User Agent; trying to save a few bytes.


User avatar
Posts: 6
Joined: July 8th, 2015, 9:27 am
Location: Sri Lanka

Post Posted April 1st, 2018, 8:51 am

@pintassilgo: I had a look at the suggested script (, but from what I understood it's more about redirecting a browser page to a different URL IIUC:

Code: Select all
webNav = callback.getInterface(Ci.nsILoadContext).associatedWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation);
// ...
webNav.loadURI(redirectUrl, null, null, null, null);

As per the MDN docs (, nsIWebNavigation is for web navigation (e.g. in a browser window); confirmed by its other available functionalities (goBack, goForward, gotoIndex, reload etc.) as well.

Whereas in my case, I'm redirecting sub-resources (CSS, JS, images etc.) loaded by a webpage.

Anyway, thanks for the support! Would you, by any chance, happen to know any other redirection mechanism that can be applied for subresources (or nested; not sure about the word) like CSS, JS etc. loaded by a webpage?

(I have yet to try out the WebExtensions API (browser.webRequest) but seems that you cannot simply try it out via copy-paste on the browser console; probably you need to package an add-on and install it on the browser, or load it via web-ext, whatever.)
Please disregard the weird User Agent; trying to save a few bytes.

Return to Firefox Builds

Who is online

Users browsing this forum: No registered users and 1 guest