为什么这个JavaScript页面重定向这么慢?

时间:2014-08-15 13:30:20

标签: javascript javascript-events firefox-addon

我正在实施一个Firefox插件。在插件的工具栏中,我捕获了当前页面,并在用户尝试访问荷兰Google时将其重定向到英国Google。但是,此代码需要永远完成。当我输入" google.nl"或者" google.com/nl" ;,我的浏览器会显示荷兰页面,并且在重定向之前至少需要1秒钟。

有没有办法让重定向更快?理想情况下,我希望用户根本看不到荷兰的Google网页。

function loadURL(url) {
    window._content.document.location = url;
    window.content.focus();
}

var redirected = false;

function onChange()
{

    var url = gBrowser.selectedBrowser.currentURI.spec;
    url = encodeURIComponent(url);

    if(url.indexOf("google.nl") !=-1 || url.indexOf("hl%3Dnl") !=-1){
        if (!redirected){
            redirected = true;
            loadURL("https://www.google.co.uk/");
            return;
        }
    }else{
        redirected = false;
    }
}

注意:onChange()

会触发container.addEventListener('DOMSubtreeModified',onChange, false);

1 个答案:

答案 0 :(得分:3)

第一次观察

永远不要使用DOMSubtreeModified那个本应被删除的MutationEvent。您应该使用MutationObserver

因此,在您的情况下,而不是container.addEventListener('DOMSubtreeModified, onChange, false);执行此操作:

const gMutationConfig = {
    subtree: true,
    childList: true
};

var gMutationFunc = function(ms) {
    for (let m of ms) {
        console.log(m.type, m);
        //if (mutation.addedNodes && mutation.addedNodes.length > 0) { //this test if elements added
        onChange();
    }
};

var gMutationObserver = new this.DOMWindow.MutationObserver(container);

第二件事是

您可能应该使用nsIHTTPChannel将重定向设置为最快的

const { Ci, Cu, Cc, Cr } = require('chrome'); //const {interfaces: Ci, utils: Cu, classes: Cc, results: Cr } = Components;
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/devtools/Console.jsm');

var observers = {
    'http-on-modify-request': {
        observe: function (aSubject, aTopic, aData) {
            console.info('http-on-modify-request: aSubject = ' + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData);
            var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
            var requestUrl = httpChannel.URI.spec
            if (requestUrl.indexOf('google.com') > -1) {
               //httpChannel.cancel(Cr.NS_BINDING_ABORTED); //this aborts the load
               httpChannel.redirectTo(Services.io.newURI('data:text,url_blocked', null, null)); //can redirect with this line, if dont want to redirect and just block, then uncomment this line and comment out line above (line 17)
            }
        },
        reg: function () {
            Services.obs.addObserver(observers['http-on-modify-request'], 'http-on-modify-request', false);
        },
        unreg: function () {
            Services.obs.removeObserver(observers['http-on-modify-request'], 'http-on-modify-request');
        }
    }
};

开始观察

要开始观察所有请求,请执行此操作(例如,在启动插件时)

for (var o in observers) {
    observers[o].reg();
}

停止观察

重要的是要停止观察(确保至少在关闭插件时运行它,你不想让观察者因记忆原因而注册)

for (var o in observers) {
    observers[o].unreg();
}

阻止/重定向网址的观察员服务的完整工作示例:https://github.com/Noitidart/PortableTester/tree/block-urls