MozillaZine

Seeking help for restoring an old URL-rewriting script

Discussion about official Mozilla Firefox builds
janakaud

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 http://www.example.com/test.js?v=abcdef to http://www.example.com/test.js) using nsIObserverService like so (full source at https://github.com/janakaud/js-on-firefox/blob/master/keyconfig/ToggleOffline.js):

Code: Select all
Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService).addObserver({
   observe: function(aSubject, aTopic, aData) {
      var channel = aSubject.QueryInterface(Ci.nsIHttpChannel);
      if (channel.URI.spec == "http://www.example.com/test.js?v=abcdef") {
         channel.URI.spec = "http://www.example.com/test.js";
      }
   }
}, "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 https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIURI 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() (https://developer.mozilla.org/en-US/doc ... edirectTo()) instead:

Code: Select all
         channel.redirectTo(channel.URI.mutate().setSpec("http://www.example.com/test.js").finalize());


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 https://assets-cdn.github.com/assets/mobile-fe9d20f9494a.js. (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.

LIMPET235
Moderator

User avatar
 
Posts: 38287
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-57.0/T-bird-2.0.0.24/SnagIt-v10.0.1/MWP-7.11.0.
RadioYachting. (Always choose the "Custom" Install.)

wisniewskit
 
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: https://bugzilla.mozilla.org/show_bug.cgi?id=1431204
It seems that in JS you now have to do this: uri.mutate().setSpec(newSpec).finalize()

pintassilgo
 
Posts: 146
Joined: August 30th, 2013, 3:50 pm

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

What I use:
https://github.com/xiaoxiaoflood/firefo ... 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).

janakaud

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
channel.redirectTo(channel.URI.mutate().setSpec("newUrl").finalize());


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

I tried simply doing

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


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.

janakaud

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 (https://github.com/xiaoxiaoflood/firefox-scripts/blob/master/chrome/redirector.uc.js), 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 (https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIWebNavigation), 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 4 guests