home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / communicator / related / related-panel.js next >
Text File  |  2003-06-08  |  7KB  |  229 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/MPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is from Alexa Internet (www.alexa.com).
  14.  *
  15.  * The Initial Developer of the Original Code is Alexa Internet.
  16.  * Portions created by Alexa Internet are Copyright (C) 2000 Alexa
  17.  * Internet. All Rights Reserved.
  18.  *
  19.  * Contributor(s):
  20.  */
  21.  
  22. /*
  23.   Code for the Related Links Sidebar Panel
  24.  */
  25.  
  26.  
  27. var kUnknownReasonUrl        = 'about:blank';
  28. var kMAMIUrl                = 'http://xslt.alexa.com/data?cli=17&dat=nsa';
  29.  
  30. var kNoHTTPReasonUrl        = kMAMIUrl + 'req_type=secure_intranet';
  31. var kSkipDomainReasonUrl    = kMAMIUrl + 'req_type=blocked_list';
  32. var kDataPrefixUrl            = kMAMIUrl;
  33.  
  34. var oNavObserver = null;
  35.  
  36. // Our observer of Navigation Messages
  37. function NavObserver(oDisplayFrame,oContentWindow)
  38. {
  39.     this.m_oDisplayFrame = oDisplayFrame;
  40.     this.m_oContentWindow = oContentWindow;
  41.     this.m_sWindowID = ''+parseInt(Math.random()*32767);
  42.     this.m_sLastDataUrl = 'about:blank';    // The last url that we drove our display to.
  43. }
  44.  
  45. NavObserver.prototype.observe =
  46. function (oSubject, sMessage, sContextUrl)
  47. {
  48.     try {
  49.         if (oSubject != this.m_oContentWindow) {
  50.             // only pay attention to our client window.
  51.             return;
  52.         }
  53.         var bReferrer = (this.m_oContentWindow.document.referrer)?true:false;
  54.         if ((sMessage == 'EndDocumentLoad') 
  55.             && (sContextUrl != this.m_oContentWindow.location)) {
  56.                 // we were redirected...
  57.             sContextUrl = '' + this.m_oContentWindow.location;
  58.             bReferrer = true;
  59.         }
  60.         this.TrackContext(sContextUrl, bReferrer);
  61.     } catch(ex) {
  62.     }
  63. }
  64.  
  65. NavObserver.prototype.GetCDT =
  66. function (bReferrer)
  67. {
  68.     var sCDT = '';
  69.     sCDT += 't=' +(bReferrer?'1':'0');
  70.     sCDT += '&pane=nswr6';
  71.     sCDT += '&wid='+this.m_sWindowID;
  72.  
  73.     return escape(sCDT);
  74. }
  75.  
  76. NavObserver.prototype.TrackContext =
  77. function (sContextUrl, bReferrer)
  78. {
  79.     if (sContextUrl != this.m_sLastContextUrl && this.m_oDisplayFrame) {
  80.         var sDataUrl = this.TranslateContext(sContextUrl,bReferrer);
  81.         this.m_oDisplayFrame.setAttribute('src', sDataUrl);
  82.         this.m_sLastContextUrl = sContextUrl;
  83.     }
  84. }
  85.  
  86. NavObserver.prototype.TranslateContext =
  87. function (sUrl, bReferrer)
  88. {
  89.     if (!sUrl || ('string' != typeof(sUrl))
  90.         || ('' == sUrl) || sUrl == 'about:blank') {
  91.         return kUnknownReasonUrl;
  92.     }
  93.  
  94.     // Strip off any query strings (Don't want to be too nosy).
  95.     var nQueryPart = sUrl.indexOf('?');
  96.     if (nQueryPart != 1) {
  97.         sUrl = sUrl.slice(0, nQueryPart);
  98.     }
  99.  
  100.     // We can only get related links data on HTTP URLs
  101.     if (0 != sUrl.indexOf("http://")) {
  102.         return kNoHTTPReasonUrl;
  103.     }
  104.  
  105.     // ...and non-intranet sites(those that have a '.' in the domain)
  106.     var sUrlSuffix = sUrl.substr(7);            // strip off "http://" prefix
  107.  
  108.     var nFirstSlash = sUrlSuffix.indexOf('/');
  109.     var nFirstDot = sUrlSuffix.indexOf('.');
  110.  
  111.     if (-1 == nFirstDot)
  112.         return kNoHTTPReasonUrl;
  113.  
  114.     if ((nFirstSlash < nFirstDot) && (-1 != nFirstSlash))
  115.         return kNoHTTPReasonUrl;
  116.  
  117.     // url is http, non-intranet url: see if the domain is in their blocked list.
  118.  
  119.     var nPortOffset = sUrlSuffix.indexOf(":");
  120.     var nDomainEnd = (((nPortOffset >=0) && (nPortOffset <= nFirstSlash))
  121.                       ? nPortOffset : nFirstSlash);
  122.  
  123.     var sDomain = sUrlSuffix;
  124.     if (-1 != nDomainEnd) {
  125.         sDomain = sUrlSuffix.substr(0,nDomainEnd);
  126.     }
  127.  
  128.     if (DomainInSkipList(sDomain)) {
  129.         return kSkipDomainReasonUrl;
  130.     } else {
  131.         // ok! it is a good url!
  132.         var sFinalUrl = kDataPrefixUrl;
  133.         sFinalUrl += 'cdt='+this.GetCDT(bReferrer);
  134.         sFinalUrl += '&url='+sUrl;
  135.         return sFinalUrl;
  136.     }
  137. }
  138.  
  139.  
  140. function DomainInSkipList(sDomain)
  141. {
  142.     var bSkipDomainFlag = false;
  143.  
  144.     if ('/' == sDomain[sDomain.length - 1]) {
  145.         sDomain = sDomain.substring(0, sDomain.length - 1);
  146.     }
  147.  
  148.     try {
  149.         var pref = Components.classes["@mozilla.org/preferences-service;1"]
  150.                              .getService(Components.interfaces.nsIPrefBranch);
  151.  
  152.         var sDomainList = pref.getCharPref("browser.related.disabledForDomains");
  153.         if ((sDomainList) && (sDomainList != "")) {
  154.  
  155.             var aDomains = sDomainList.split(",");
  156.  
  157.             // split on commas
  158.             for (var x=0; x < aDomains.length; x++) {
  159.                 var sDomainCopy = sDomain;
  160.  
  161.                 var sTestDomain = aDomains[x];
  162.  
  163.                 if ('*' == sTestDomain[0]) { // wildcard match
  164.  
  165.                     // strip off the asterisk
  166.                     sTestDomain = sTestDomain.substring(1);    
  167.                     if (sDomainCopy.length > sTestDomain.length) {
  168.                         var sDomainIndex = sDomain.length - sTestDomain.length;
  169.                         sDomainCopy = sDomainCopy.substring(sDomainIndex);
  170.                     }
  171.                 }
  172.  
  173.                 if (0 == sDomainCopy.lastIndexOf(sTestDomain)) {
  174.  
  175.                     bSkipDomainFlag = true;
  176.                     break;
  177.                 }
  178.             }
  179.         }
  180.     } catch(ex) {
  181.     }
  182.     return(bSkipDomainFlag);
  183. }
  184.  
  185. function Init()
  186. {
  187.     // Initialize the Related Links panel
  188.  
  189.     // Install our navigation observer so we can track the main client window.
  190.  
  191.     oContentWindow = window._content;
  192.     oFrame = document.getElementById('daFrame');
  193.  
  194.     if (oContentWindow && oFrame) {
  195.         var oObserverService = Components.classes["@mozilla.org/observer-service;1"].getService();
  196.         oObserverService = oObserverService.QueryInterface(Components.interfaces.nsIObserverService);
  197.  
  198.         oNavObserver = new NavObserver(oFrame,oContentWindow);
  199.         oNavObserver.TrackContext(''+oContentWindow.location);
  200.  
  201.         if (oObserverService && oNavObserver) {
  202.             oObserverService.addObserver(oNavObserver, "StartDocumentLoad", false);
  203.             oObserverService.addObserver(oNavObserver, "EndDocumentLoad", false);
  204.             oObserverService.addObserver(oNavObserver, "FailDocumentLoad", false);
  205.         } else {
  206.             oNavObserver = null;
  207.             dump("FAILURE to get observer service\n");
  208.         }
  209.     }
  210. }
  211.  
  212. function Destruct()
  213. {
  214.     // remove our navigation observer.
  215.     var oObserverService = Components.classes["@mozilla.org/observer-service;1"].getService();
  216.     oObserverService = oObserverService.QueryInterface(Components.interfaces.nsIObserverService);
  217.     if (oObserverService && oNavObserver) {
  218.         oObserverService.removeObserver(oNavObserver, "StartDocumentLoad");
  219.         oObserverService.removeObserver(oNavObserver, "EndDocumentLoad");
  220.         oObserverService.removeObserver(oNavObserver, "FailDocumentLoad");
  221.         oNavObserver = null;
  222.     } else {
  223.         dump("FAILURE to get observer service\n");
  224.     }
  225. }
  226.  
  227. addEventListener("load", Init, false);
  228. addEventListener("unload", Destruct, false);
  229.