加载新页面时快速重定向选项卡的方法

时间:2014-07-20 10:04:03

标签: javascript firefox-addon firefox-addon-sdk

我尝试将标签重定向到新页面,当网址完成加载之前匹配我的模式时。我提出的方法是在页面的大部分内容完成加载后进行重定向。

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);

有没有一种好的方法可以提前调用setTabURL (...)

2 个答案:

答案 0 :(得分:3)

终于找到了最好的方法:

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);
}

信用到期的信用:answer matagusquestion Andrew询问)

[1]:链接:Intercepting Page Loads
[2]:Noitidart:'来自主题:How can I change the User Agent in just one tab of Firefox?Is it possible to know the target DOMWindow for an HTTPRequest?'

答案 1 :(得分:0)

之前从未使用sdk/tabs,但您可以加载隐藏的内容。

页面加载后,您的logShow功能将会运行。

然后在这个功能中构建一些"显示身体"功能。