home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9866 / 9866.xpi / modules / samfind_modfavicon.jsm < prev    next >
Encoding:
Text File  |  2009-09-08  |  4.8 KB  |  172 lines

  1. //-------------------------------------------------------------------------------------------
  2. // Favicon module.
  3. //-------------------------------------------------------------------------------------------
  4.  
  5. const Cc = Components.classes;
  6. const Ci = Components.interfaces;
  7.  
  8. var EXPORTED_SYMBOLS = ["samfind_modfavicon"];
  9.  
  10. var samfind_modfavicon =
  11. {
  12.     _io_service : null,
  13.     _favicon_service : null,
  14.  
  15.     _init : function()
  16.     {
  17.         samfind_modfavicon._io_service = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  18.         samfind_modfavicon._favicon_service = Cc["@mozilla.org/browser/favicon-service;1"].getService(Ci.nsIFaviconService);
  19.       },
  20.  
  21.     getWebsiteFavicon : function(faviconAttr, iconAttr, idAttr, homeUrlAttr)
  22.     {
  23.         var favicon = null;
  24.         if (faviconAttr && faviconAttr.length)
  25.         {
  26.             favicon = faviconAttr;
  27.         }
  28.         else
  29.         {
  30.             if (iconAttr && iconAttr == "1")
  31.             {
  32.                 favicon = "http://samfind.com/images/website-icons/" + idAttr + ".gif";
  33.             }
  34.             else
  35.             {
  36.                 favicon = samfind_modfavicon.getFaviconFromURL(homeUrlAttr);
  37.                 if (samfind_modfavicon.checkFavicon(favicon, idAttr) == false)
  38.                 {
  39.                     var second_attempt = null;
  40.                     if (homeUrlAttr.indexOf("https://") == 0)
  41.                     {
  42.                         if (homeUrlAttr.indexOf("https://www.") == 0)
  43.                         {
  44.                             second_attempt = homeUrlAttr.replace("https://www.", "https://");
  45.                         }
  46.                         else
  47.                         {
  48.                             second_attempt = homeUrlAttr.replace("https://", "https://www.");
  49.                         }
  50.                     }
  51.                     else if (homeUrlAttr.indexOf("http://") == 0)
  52.                     {
  53.                         if (homeUrlAttr.indexOf("http://www.") == 0)
  54.                         {
  55.                             second_attempt = homeUrlAttr.replace("http://www.", "http://");
  56.                         }
  57.                         else
  58.                         {
  59.                             second_attempt = homeUrlAttr.replace("http://", "http://www.");
  60.                         }
  61.                     }
  62.                     if (second_attempt)
  63.                     {
  64.                         favicon = samfind_modfavicon.getFaviconFromURL(second_attempt);
  65.                         samfind_modfavicon.checkFavicon(favicon, idAttr);
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.         return favicon;
  71.     },
  72.  
  73.     getFaviconFromURL : function(url)
  74.     {
  75.         try
  76.         {
  77.             return samfind_modfavicon.getFaviconFromURLObject(samfind_modfavicon._io_service.newURI(url, null, null));
  78.         }
  79.         catch (e)
  80.         {}
  81.         return null;
  82.     },
  83.  
  84.     getFaviconFromURLObject : function(urlObject)
  85.     {
  86.         try
  87.         {
  88.             var favicon = samfind_modfavicon._favicon_service.getFaviconImageForPage(urlObject);
  89.             if (favicon && favicon.spec && favicon.spec.length)
  90.             {
  91.                 return favicon.spec;    
  92.             }
  93.         }
  94.         catch (e)
  95.         {}
  96.         return null;
  97.     },
  98.  
  99.     checkFavicon : function(favicon, websiteId)
  100.     {
  101.         if (favicon
  102.             && favicon.indexOf("/made-up-favicon/") == -1
  103.             && favicon.indexOf("chrome://mozapps/skin/places/defaultFavicon.png") == -1)
  104.         {
  105.             samfind_modfavicon.sendFaviconToServer(websiteId, favicon.substring("moz-anno:favicon:".length));
  106.             return true;
  107.         }
  108.         return false;
  109.     },
  110.  
  111.     updateFavicon : function(uri, websiteId, websiteHomeURL)
  112.     {
  113.         var favicon = samfind_modfavicon.getFaviconFromURLObject(uri);
  114.         if (!favicon)
  115.         {
  116.             return;
  117.         }
  118.         if (favicon.indexOf("/made-up-favicon/") != -1
  119.             || favicon.indexOf("chrome://mozapps/skin/places/defaultFavicon.png") != -1)
  120.         {
  121.             try
  122.             {
  123.                 var location_uri = samfind_modfavicon._io_service.newURI(content.location.href, null, null);
  124.                 favicon = samfind_modfavicon._favicon_service.getFaviconForPage(location_uri);
  125.                 location_uri = null;
  126.                 favicon = favicon.spec;
  127.             }
  128.             catch (e)
  129.             {
  130.                 return;    
  131.             }
  132.         }
  133.         //
  134.         if (favicon.indexOf("/made-up-favicon/") == -1
  135.             && favicon.indexOf("chrome://mozapps/skin/places/defaultFavicon.png") == -1)
  136.         {
  137.             if (favicon.indexOf("moz-anno:favicon:") == 0)
  138.             {
  139.                 favicon = favicon.substring("moz-anno:favicon:".length);
  140.             }
  141.             samfind_modfavicon.sendFaviconToServer(websiteId, favicon);
  142.             var observer_service = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
  143.             observer_service.notifyObservers(null, "samfind-update-favicons", websiteHomeURL + "ºº" + favicon);
  144.             observer_service = null;
  145.         }
  146.     },
  147.  
  148.     sendFaviconToServer : function(websiteId, favicon)
  149.     {
  150.         var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  151.         timer.initWithCallback({ notify : function()
  152.                                           {
  153.                                                   try
  154.                                                   {
  155.                                                       var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
  156.                                                       xhr.open("POST", "http://samfind.com/images/website-icons/favicon.php", true);
  157.                                                       xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  158.                                                       xhr.setRequestHeader("User-Agent", "samfind Toolbar");
  159.                                                       xhr.send("wid=" + websiteId + "&url=" + favicon);
  160.                                                       xhr = null;
  161.                                                   }
  162.                                                   catch (e)
  163.                                                   {}
  164.                                           }
  165.                                }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
  166.     }
  167. };
  168.  
  169. /**
  170.  * Constructor.
  171.  */
  172. (function() { this._init(); }).apply(samfind_modfavicon);