1

I'm trying to redirect a tab to a new page when the URL matches my pattern before it's done loading. The method I came up with does the redirection after a good part of the page is done loading yet.

var tabs = require("sdk/tabs");
var tab_utils = require("sdk/tabs/utils");

function logShow(tab) {
    console.log(tab.url + " is loaded; " + pattern.test(tab.url));
    if (pattern.test(tab.url)) {
        var lowLevelTab = viewFor(tab);
        console.log(tab_utils.setTabURL (lowLevelTab, newURL(tab.url)));

        // also simply replacing this bit with
        // tab.url = "foo" doesn't speed things up
    }
}

tabs.on('load', logShow);

Is there a good way of calling setTabURL (...) earlier?

yspreen
  • 1,759
  • 2
  • 20
  • 44

2 Answers2

3

I finally found the best way to do it:

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

    // Here you should evaluate the url and decide if make a redirect or not.
    if (pattern.test(url)) {
        // If you want to redirect to another url,
        // you have to abort current request, see: [1] (links below)
        channel.cancel(Cr.NS_BINDING_ABORTED);

        // Set the current gbrowser object (since
        // the user may have several windows/tabs)
        var goodies = loadContextGoodies(channel);
        var domWin = goodies.aDOMWindow;       // method suggested by
        var gBrowser = goodies.gBrowser;       // Noitidart [2] (links below)
        var browser = goodies.browser;         // for reference see comment below
        var htmlWindow = goodies.contentWindow;

        // and load the fixed URI
        browser.loadURI(newUrl(url));
    } else {
        // do nothing, let Firefox keep going on the normal flow
    }
}

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

credit where credit is due: answer by matagus (on question asked by Andrew)

[1]: Link: Intercepting Page Loads
[2]: Noitidart: 'from topics: How can I change the User Agent in just one tab of Firefox? and Is it possible to know the target DOMWindow for an HTTPRequest?'

Community
  • 1
  • 1
yspreen
  • 1,759
  • 2
  • 20
  • 44
  • 2
    Nice to share the solution. However this line: `var gBrowser = utils.getMostRecentBrowserWindow().gBrowser;` is not the correct way, because it gets selected tab of the most recent window (assuming only 1 win). So do this instead: `var goodies = loadContextGoodies(channel); var domWin = goodies.aDOMWindow; var gBrowser = goodies.gBrowser; var browser = goodies.browser; var htmlWindow = goodies.contentWindow;` from topics: How can I change the User Agent in just one tab of Firefox? and [Is it possible to know the target DOMWindow for an HTTPRequest?](http://stackoverflow.com/a/10719827/1828637) – Noitidart Jul 21 '14 at 02:53
  • Thanks for the tip, I'll adjust my solution. The second link also seems pretty useful for me, since I want to check if the request is made from the address bar.. Wouldn't a response different to null indicate such? – yspreen Jul 21 '14 at 13:54
  • I guess thats just rubbish. Since we get the DOMWindow through the goodies object. But seriously, is there any way to seperate addressbar request from in-page-request? At all? – yspreen Jul 21 '14 at 14:34
  • Hm make a seperate topic on that question. It's a good question. (The identify if from address bar) – Noitidart Jul 21 '14 at 21:14
  • [there](http://stackoverflow.com/questions/24886858/determine-http-request-location-origin-address-bar) you go – yspreen Jul 22 '14 at 13:46
  • 1
    Also instead of `cancel`ing and then `browser.loadURI`ing you can just use `channel.redirectTo`: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIHttpChannel#redirectTo%28%29 +1 for nice edits to your solution so it can help out people in future – Blagoh Jul 22 '14 at 17:22
  • 1
    At [this](http://forums.mozillazine.org/viewtopic.php?f=19&t=2787479) post it looks like this doesnt really work flawlessly.. – yspreen Jul 22 '14 at 17:52
  • That looks like a bug with the FireBug add-on. It doesn't look like it's a problem with `redirectTo`. – Noitidart Jul 22 '14 at 21:22
0

Never used sdk/tabs before, but you could load your content hidden.

Once your page has loaded your logShow function will run.

Then build into this function some "reveal body" functionality.

  • But how would this get me to redirect the tab to a different location before the old url is done loading? I'm just trying to redirect the tab, not displaying any additional content.. Or did I get you wrong entirely here? – yspreen Jul 20 '14 at 10:18