简体   繁体   中英

How to record the 301 Redirects URL?

I enter to browser this link https://google.com.vn ; Google redirect to https://www.google.com.vn ; I want alert full url redirect. I used this code:

 processNewURL: function(aURI) {
       var tabIndex = gBrowser.tabContainer.selectedIndex;
       var referredFromURI = gBrowser.tabContainer.childNodes[tabIndex].linkedBrowser.webNavigation.referringURI.spec;
        alert(referredFromURI);
},

But it always alert https://www.google.com.vn , and I tested with some short link example bit.ly/R9j52J . It isn't ok. Please help me.

this works, i also show 2 methods to get to webNavigation. the second method is just longed winded way to teach other stuff, recommended way is method 1.

var processNewURL = function(e) {
  console.log('e:', e);
       var win = e.originalTarget.defaultView;

     //start - method 1 to get to webNav:
     var webNav = win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation);
     var referredFromURI = webNav.referringURI;
     //end - method 1

     //start - method 2 long winded way:
     /*
       var domWin = win.QueryInterface(Ci.nsIInterfaceRequestor)
                         .getInterface(Ci.nsIWebNavigation)
                         .QueryInterface(Ci.nsIDocShellTreeItem)
                         .rootTreeItem
                         .QueryInterface(Ci.nsIInterfaceRequestor)
                         .getInterface(Ci.nsIDOMWindow);
       var tab = domWin.gBrowser._getTabForContentWindow(win);
       //console.log('tab:', tab);
       var referredFromURI = tab.linkedBrowser.webNavigation.referringURI;
       */
     //end - method 2

     if (referredFromURI != null) {
          win.alert('referred from:' + referredFromURI.spec);
     } else {
          win.alert('not redirected');
     }
}

gBrowser.addEventListener('DOMContentLoaded', processNewURL, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM