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

标签 javascript firefox-addon firefox-addon-sdk

当 URL 与我的模式匹配时,我正在尝试将选项卡重定向到新页面,然后它已完成加载。我想出的方法是在页面的大部分加载完成后进行重定向。

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(...)的好方法?

最佳答案

终于找到了最好的方法:

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通过 matagus (在 question 上由 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? '

关于javascript - 加载新页面时快速重定向选项卡的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24849385/

相关文章:

javascript - "Twitching"当使用 JQuery hover 来剪辑我的 Bootstrap 卡时

javascript - 将函数附加到左键单击 <listbox> 项目

javascript - 在 Firefox SDK main.js 中获取某些文件的内容

linux - 通过 jpm 生成签名的 XPI 失败

javascript - 如何在 async.each 内具有 async.waterfall 的外部函数中调用 async.each

javascript - 如何在 Jest 中重置模拟值?

javascript - Firefox:如何从电解 (e10s) 下的 JS 模块获取 nsIMessageManager 实例?

javascript - 从 Firefox 附加 SDK 的上下文菜单中打开一个新选项卡

javascript - 如何检查在 Javascript 中作为参数传递的回调函数是否有参数?

CSS 在我的 Firefox 扩展工具栏上不起作用