当用户访问特定网站时,Firefox 自动重定向

发布于 2024-12-19 08:59:04 字数 1549 浏览 3 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云不回头 2024-12-26 08:59:04

当然,这只是更改 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...?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文