gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:39:49 25 4
gpt4 key购买 nike

当 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com