当用户访问特定网站时,Firefox 自动重定向
我正在编写一个 Firefox 扩展,当用户访问预定义站点的主页时,他们将自动重定向到同一站点上的不同页面。问题是它创建了一个重定向循环,因为它重定向特定站点上的每个页面而不仅仅是主页。我只想在用户访问主页时重定向用户,而不是每次访问同一网站上的任何页面时重定向用户。
var myExt_urlBarListener = {
QueryInterface: function(aIID)
{
if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
aIID.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_NOINTERFACE;
},
onLocationChange: function(aProgress, aRequest, aURI)
{
myExtension.processNewURL(aURI);
},
onStateChange: function(a, b, c, d) {},
onProgressChange: function(a, b, c, d, e, f) {},
onStatusChange: function(a, b, c, d) {},
onSecurityChange: function(a, b, c) {}
};
var myExtension = {
oldURL: null,
init: function() {
// Listen for webpage loads
gBrowser.addProgressListener(myExt_urlBarListener,
Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
},
uninit: function() {
gBrowser.removeProgressListener(myExt_urlBarListener);
},
processNewURL: function(aURI) {
if (aURI.spec == this.oldURL)
return;
// now we know the url is new...
//alert(aURI.spec);
if(aURI.spec.search("www.examplesite.com") > -1){
gBrowser.contentDocument.location.href = "http://www.examplesite.com/page1";
}
this.oldURL = aURI.spec;
}
};
window.addEventListener("load", function() {myExtension.init()}, false);
window.addEventListener("unload", function() {myExtension.uninit()}, false);
I am writing a firefox extension that when a user visits the homepage of a predefined site they will automatically be redirected to different page on that same site. The problem is that it creates a redirect loop because it redirects every page on particular site not just the homepage. I want to just redirect the user when they visit the homepage, not every time they visit any page on that same site.
var myExt_urlBarListener = {
QueryInterface: function(aIID)
{
if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
aIID.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_NOINTERFACE;
},
onLocationChange: function(aProgress, aRequest, aURI)
{
myExtension.processNewURL(aURI);
},
onStateChange: function(a, b, c, d) {},
onProgressChange: function(a, b, c, d, e, f) {},
onStatusChange: function(a, b, c, d) {},
onSecurityChange: function(a, b, c) {}
};
var myExtension = {
oldURL: null,
init: function() {
// Listen for webpage loads
gBrowser.addProgressListener(myExt_urlBarListener,
Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
},
uninit: function() {
gBrowser.removeProgressListener(myExt_urlBarListener);
},
processNewURL: function(aURI) {
if (aURI.spec == this.oldURL)
return;
// now we know the url is new...
//alert(aURI.spec);
if(aURI.spec.search("www.examplesite.com") > -1){
gBrowser.contentDocument.location.href = "http://www.examplesite.com/page1";
}
this.oldURL = aURI.spec;
}
};
window.addEventListener("load", function() {myExtension.init()}, false);
window.addEventListener("unload", function() {myExtension.uninit()}, false);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,这只是更改
if(aURI.spec.search("www.examplesite.com") > -1){
的问题,以便您可以比较实际的特定 URL。 .?Surely this is just a matter of changing
if(aURI.spec.search("www.examplesite.com") > -1){
so that you're comparing for the actual specific URL...?