1

So I'm trying to redirect the browser to another webpage when the page he is attempting to load matches my conditions (regex). Currently it looks like this: (came up with it here)

function listener(event) {
    var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
    var url = event.subject.URI.spec;

    if (isToBeRedirected(url)) {
        // replace url
    }
}

exports.main = function() {
    events.on("http-on-modify-request", listener);
}

But the problem is, that this will also replace urls to images for example, which are embedded in the page. My question would be, is there a way of varyfying that the http request is made by typing in a url or clicking a link to a page? So simply everytime a url shows up in the address-bar.
I thought about reading the url of the current tab and comparing it to the request url, but I wasnt able to find out exactly how yet.

intika
  • 8,448
  • 5
  • 36
  • 55
yspreen
  • 1,759
  • 2
  • 20
  • 44
  • I thought about comparing the request url with the tab url, by reading the url of the browser object which we get through the channel of the request, but at the time the http-on-modify-request event fires the uri property hasnt changed yet. (It's still the uri of the website before attempting to change the url) – yspreen Jul 22 '14 at 16:08

1 Answers1

3

I have an idea.

If you do console.log(Ci.nsIHttpChannel) you see it has a bunch of flags, see img at bottom.

So I'm thinking test if it's the main load, meaning its not in a sub frame, but its the whole document of the tab by testing for flag of Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI. If its the whole document, its likely from a link, or url bar, or search bar.

So try this:

function listener(event) {
    var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
    var url = event.subject.URI.spec;

    if (channel.loadFlags & Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI) {
            //the whole document of the tab is changing
            if (isToBeRedirected(url)) {
                // replace url
            }
    }
}

exports.main = function() {
    events.on("http-on-modify-request", listener);
}


My other idea, is if you want only url bar changes. Than add an event listener to change of the url bar value. And after change if user hits enter or clicks go, then it register the observer. Then after testing for Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI than unregister the observer.

Blagoh
  • 1,225
  • 1
  • 14
  • 29
  • Nice idea! I'll have it a go. Also I'd suggest changing your & to && for resource friendliness, since && only checks the second value if the first is true. Nitpicking, I know, but you cant suggest 1 character editing on stackoverflow – yspreen Jul 22 '14 at 17:07
  • 2
    Actually `&` is very differnt from `&&`. The single `&` is a bitwise operator. So make sure to use single `&` :) here is tutorial: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Example.3A_Flags_and_bitmasks – Blagoh Jul 22 '14 at 17:13
  • crap. I guess I was thinking about another language. – yspreen Jul 22 '14 at 17:24
  • 1
    So I tried your first method and it doesnt really seem to be working.. `if (channel & Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI) console.log("foo")` doesnt fire when I navigate to example.com – yspreen Jul 22 '14 at 17:32
  • 2
    Oh oops I forgot it's `channel.loadFlags & Ci.nsIHttp.....` Ill edit the post now. I was doing `channel & Ci....` which is wrong. Need that `loadFlags` parameter. – Blagoh Jul 22 '14 at 18:07