gpt4 book ai didi

javascript - 重温 - 在 Firefox 中加载顶级页面之前修改 URL

转载 作者:行者123 更新时间:2023-11-28 00:55:49 30 4
gpt4 key购买 nike

我想知道如何使用 Firefox 插件在加载页面时修改页面的 URL。我收到了诸如the following之类的建议。具体来说,当用户通过地址栏访问“https://www.google.com”时,我想重定向到“https://www.google.co.uk”。

对于我的特定目的,上述解决方案几乎有效,但它重定向了太多 URL。我发现的一个问题如下:虽然我每次都可以重定向,但有时我会在不想重定向时进行重定向。例如,在 Windows 中,我不仅在当前选项卡的 URL 是常规 Google 时进行重定向,而且在页面进行的某些查询包含上述链接(例如新闻文章)时也会进行重定向。

有没有办法使用 Mozilla 的观察者框架通过仅检查当前选项卡的 URL 来进行重定向?如果没有,是否有一些仍然使用 Mozilla 的附加 SDK 的替代解决方案?

编辑:下面是我有问题的源代码。

var myObserver = {
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, TOPIC_MODIFY_REQUEST, false);
},
//observe function to capture the changed event
observe : function(aSubject, aTopic, aData) {
if (TOPIC_MODIFY_REQUEST == aTopic ) {
var url;
aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);

url = aSubject.URI.spec;
url = encodeURIComponent(url);


if (url.indexOf("google.nl") !=-1 || url.indexOf("hl%3Dnl") !=-1
|| url.indexOf("google.com%2Fnl") !=-1 || url == "https%3A%2F%2Fwww.google.com"
|| url == "https%3A%2F%2Fwww.google.com%2Fncr" || url ==
"https%3A%2F%2Fwww.google.com%2F%3Fgws_rd%3Dssl")
{
aSubject.cancel(Components.results.NS_BINDING_ABORTED);
loadURL("https://www.google.co.uk/");
}
}
}

function loadURL(url) {
// Set the browser window's location to the incoming URL
window._content.document.location = url;
// Make sure that we get the focus
window.content.focus();
}

最佳答案

检查LOAD_INITIAL_DOCUMENT_URI标志,如果有,则中止/重定向它。否则不要。如果此标志存在,则它是顶级页面。

编辑:根据您的代码,您需要测试 QueryInterfaced 的 aSubject 是否具有如下所示的标志:

var oHttp = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
if (oHttp.loadFlags & Components.interfaces.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI) {
//is top level load
}

关于javascript - 重温 - 在 Firefox 中加载顶级页面之前修改 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26240124/

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