Seeking help for restoring an old URL-rewriting script

Discussion about official Mozilla Firefox builds
Post Reply
User avatar
janakaud
Posts: 6
Joined: July 8th, 2015, 9:27 am
Location: Sri Lanka

Seeking help for restoring an old URL-rewriting script

Post by janakaud »

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-firef ... Offline.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/doc ... ace/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 ... redirectTo()) 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.
User avatar
LIMPET235
Moderator
Posts: 39955
Joined: October 19th, 2007, 1:53 am
Location: The South Coast of N.S.W. Oz.

Re: Seeking help for restoring an old URL-rewriting script

Post by LIMPET235 »

Moving to the Builds forum...
[Ancient Amateur Astronomer.]
Win-10-H/64 bit/500G SSD/16 Gig Ram/450Watt PSU/350WattUPS/Firefox-115.0.2/T-bird-115.3.2./SnagIt-v10.0.1/MWP-7.12.125.

(Always choose the "Custom" Install.)
wisniewskit
Posts: 1
Joined: February 11th, 2018, 7:55 pm

Re: Seeking help for restoring an old URL-rewriting script

Post by wisniewskit »

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: 200
Joined: August 30th, 2013, 3:50 pm

Re: Seeking help for restoring an old URL-rewriting script

Post by pintassilgo »

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).
User avatar
janakaud
Posts: 6
Joined: July 8th, 2015, 9:27 am
Location: Sri Lanka

Re: Seeking help for restoring an old URL-rewriting script

Post by janakaud »

@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.
User avatar
janakaud
Posts: 6
Joined: July 8th, 2015, 9:27 am
Location: Sri Lanka

Re: Seeking help for restoring an old URL-rewriting script

Post by janakaud »

@pintassilgo: I had a look at the suggested script (https://github.com/xiaoxiaoflood/firefo ... ctor.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/doc ... Navigation), 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.
Post Reply