Firefox Addon - 当域匹配某些模式时更改地址栏URL

时间:2015-02-17 14:56:08

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

我想知道当address bar url与某些domain匹配时如何更改pattern?加载网址后,网址必须更改。我尝试了以下代码。它有效,但有时它没有。也许取决于负载速度,我不知道。我的意思是这段代码不太一致。我认为它可以得到加强。也许使用observerhttp-on-examine-response如何?我正在寻找一致的代码。

var {getMostRecentBrowserWindow} = require("sdk/window/utils");
var {Cc, Ci, Cu} = require("chrome");

var domain = "yahoo.com";
var display_url = "http://www.mybeautifulemail.com"

function change_addressbar_url(url) {
    var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    var uri = ioService.newURI(url, null, null);
    var win = getMostRecentBrowserWindow();
    win.gBrowser.webNavigation.setCurrentURI(uri);
}

var win = getMostRecentBrowserWindow();
win.gBrowser.addEventListener("DOMContentLoaded", function () {
    var url_regexp = new RegExp(escape_regexp(domain)+"$", "i");
    if (this.currentURI.host.match(url_regexp))
        change_addressbar_url(display_url);
}, false);

function escape_regexp(str) {
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

非常感谢!

[CODE REVISION 001]

var {getMostRecentBrowserWindow} = require("sdk/window/utils");
var {Cc, Ci, Cu} = require("chrome");

var domain = "yahoo.com";
var display_url = "http://www.mybeautifulemail.com"

function escape_regexp(str) {
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

function change_addressbar_url(url, tab) {
    var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    var uri = ioService.newURI(url, null, null);
    tab.linkedBrowser.webNavigation.setCurrentURI(uri);
}

var win = getMostRecentBrowserWindow();
win.gBrowser.addEventListener("DOMContentLoaded", function (e) {
    var contWin = e.target;
    if (contWin.top) {
        return; // this is a frame load so ignore it
    }
    var contDoc = contWin.document;
    var DOMWin = contWin.QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIWebNavigation)
                    .QueryInterface(Ci.nsIDocShellTreeItem)
                    .rootTreeItem
                    .QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIDOMWindow);

    console.log("... CODE NEVER ARRIVES THIS POINT");

    var gBrowser = DOMWin.gBrowser;
    var tab = gBrowser._getTabForContentWindow(contWin);
    var url_regexp = new RegExp(escape_regexp(domain) + "$", "i");
    if (tab.linkedBrowser.currentURI.host.match(url_regexp)) {
        change_addressbar_url(display_url, tab);
        console.log("... host matched pattern!");
    }
}, false);

1 个答案:

答案 0 :(得分:2)

您的问题是行win.gBrowser.webNavigation.setCurrentURI以及if (this.currentURI.host.match(url_regexp))这些行在该窗口中当前选定的标签上工作。您需要做的是获取正确的选项卡元素,然后在该选项卡上设置uri,如下所示:win.gBrowser.tabContainer.childNode[0].linkedBrowser.webNavigation.setCurrentURI将设置选项卡1中的地址栏,注意.childNode[0]

所以这就是你得到正确标签的方式:

var win = getMostRecentBrowserWindow();
win.gBrowser.addEventListener("DOMContentLoaded", function (e) {
    var contWin = e.originalTarget.defaultView;
    if (contWin.fameElement) {
        // this is a frame load so ignore it
        return;
    }
    var contDoc = contWin.document;
    var DOMWin = contWin.QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIWebNavigation)
                    .QueryInterface(Ci.nsIDocShellTreeItem)
                    .rootTreeItem
                    .QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIDOMWindow);
    var gBrowser = DOMWin.gBrowser;

    var tab = gBrowser._getTabForContentWindow(contWin);
    var url_regexp = new RegExp(escape_regexp(domain)+"$", "i");
    if (tab.linkedBrowser.currentURI.host.match(url_regexp))
        change_addressbar_url(display_url, tab);
}, false);

function change_addressbar_url(url, tab) {
    var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    var uri = ioService.newURI(url, null, null);
    tab.linkedBrowser.webNavigation.setCurrentURI(uri);
}