本文介绍了快速加载新页面时重定向选项卡的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图将标签重定向到一个新的页面,当URL匹配我的模式之前完成加载。我想到的方法在页面的一大部分完成加载后进行重定向。

  var tabs = require( SDK /选项卡); 
var tab_utils = require(sdk / tabs / utils);

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

//也简单地用
替换这个位// tab.url =foo不会加快速度



tabs.on('load',logShow);

是否有调用 setTabURL(...)之前?

解决方案

我终于找到了最好的方法: / p>

 函数监听器(event){
var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
var url = event.subject.URI.spec;

//在这里你应该评估网址,并决定是否重定向或不。
if(pattern.test(url)){
//如果你想重定向到另一个url,
//你必须中止当前的请求,参见:[1] )
channel.cancel(Cr.NS_BINDING_ABORTED);

//设置当前的gbrowser对象(因为
//用户可能有多个窗口/标签)
var goodies = loadContextGoodies(channel);
var domWin = goodies.aDOMWindow; //
提示的方法var gBrowser = goodies.gBrowser; // Noitidart [2](以下链接)
var browser = goodies.browser; //参考下面的注释
var htmlWindow = goodies.contentWindow;

//并加载固定的URI
browser.loadURI(newUrl(url));
} else {
//什么也不做,让Firefox继续正常的流程
}
}

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

$ / code>

其中credit是由于:)() //stackoverflow.com/questions/13200058/how-to-make-a-redirect-in-firefox-page-mod\">问题由)

[1]:链接:

[2]::来自主题:和'


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?

解决方案

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?'

这篇关于快速加载新页面时重定向选项卡的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:06