home *** CD-ROM | disk | FTP | other *** search
- //-------------------------------------------------------------------------------------------
- // Favicon module.
- //-------------------------------------------------------------------------------------------
-
- const Cc = Components.classes;
- const Ci = Components.interfaces;
-
- var EXPORTED_SYMBOLS = ["samfind_modfavicon"];
-
- var samfind_modfavicon =
- {
- _io_service : null,
- _favicon_service : null,
-
- _init : function()
- {
- samfind_modfavicon._io_service = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
- samfind_modfavicon._favicon_service = Cc["@mozilla.org/browser/favicon-service;1"].getService(Ci.nsIFaviconService);
- },
-
- getWebsiteFavicon : function(faviconAttr, iconAttr, idAttr, homeUrlAttr)
- {
- var favicon = null;
- if (faviconAttr && faviconAttr.length)
- {
- favicon = faviconAttr;
- }
- else
- {
- if (iconAttr && iconAttr == "1")
- {
- favicon = "http://samfind.com/images/website-icons/" + idAttr + ".gif";
- }
- else
- {
- favicon = samfind_modfavicon.getFaviconFromURL(homeUrlAttr);
- if (samfind_modfavicon.checkFavicon(favicon, idAttr) == false)
- {
- var second_attempt = null;
- if (homeUrlAttr.indexOf("https://") == 0)
- {
- if (homeUrlAttr.indexOf("https://www.") == 0)
- {
- second_attempt = homeUrlAttr.replace("https://www.", "https://");
- }
- else
- {
- second_attempt = homeUrlAttr.replace("https://", "https://www.");
- }
- }
- else if (homeUrlAttr.indexOf("http://") == 0)
- {
- if (homeUrlAttr.indexOf("http://www.") == 0)
- {
- second_attempt = homeUrlAttr.replace("http://www.", "http://");
- }
- else
- {
- second_attempt = homeUrlAttr.replace("http://", "http://www.");
- }
- }
- if (second_attempt)
- {
- favicon = samfind_modfavicon.getFaviconFromURL(second_attempt);
- samfind_modfavicon.checkFavicon(favicon, idAttr);
- }
- }
- }
- }
- return favicon;
- },
-
- getFaviconFromURL : function(url)
- {
- try
- {
- return samfind_modfavicon.getFaviconFromURLObject(samfind_modfavicon._io_service.newURI(url, null, null));
- }
- catch (e)
- {}
- return null;
- },
-
- getFaviconFromURLObject : function(urlObject)
- {
- try
- {
- var favicon = samfind_modfavicon._favicon_service.getFaviconImageForPage(urlObject);
- if (favicon && favicon.spec && favicon.spec.length)
- {
- return favicon.spec;
- }
- }
- catch (e)
- {}
- return null;
- },
-
- checkFavicon : function(favicon, websiteId)
- {
- if (favicon
- && favicon.indexOf("/made-up-favicon/") == -1
- && favicon.indexOf("chrome://mozapps/skin/places/defaultFavicon.png") == -1)
- {
- samfind_modfavicon.sendFaviconToServer(websiteId, favicon.substring("moz-anno:favicon:".length));
- return true;
- }
- return false;
- },
-
- updateFavicon : function(uri, websiteId, websiteHomeURL)
- {
- var favicon = samfind_modfavicon.getFaviconFromURLObject(uri);
- if (!favicon)
- {
- return;
- }
- if (favicon.indexOf("/made-up-favicon/") != -1
- || favicon.indexOf("chrome://mozapps/skin/places/defaultFavicon.png") != -1)
- {
- try
- {
- var location_uri = samfind_modfavicon._io_service.newURI(content.location.href, null, null);
- favicon = samfind_modfavicon._favicon_service.getFaviconForPage(location_uri);
- location_uri = null;
- favicon = favicon.spec;
- }
- catch (e)
- {
- return;
- }
- }
- //
- if (favicon.indexOf("/made-up-favicon/") == -1
- && favicon.indexOf("chrome://mozapps/skin/places/defaultFavicon.png") == -1)
- {
- if (favicon.indexOf("moz-anno:favicon:") == 0)
- {
- favicon = favicon.substring("moz-anno:favicon:".length);
- }
- samfind_modfavicon.sendFaviconToServer(websiteId, favicon);
- var observer_service = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
- observer_service.notifyObservers(null, "samfind-update-favicons", websiteHomeURL + "ºº" + favicon);
- observer_service = null;
- }
- },
-
- sendFaviconToServer : function(websiteId, favicon)
- {
- var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
- timer.initWithCallback({ notify : function()
- {
- try
- {
- var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
- xhr.open("POST", "http://samfind.com/images/website-icons/favicon.php", true);
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- xhr.setRequestHeader("User-Agent", "samfind Toolbar");
- xhr.send("wid=" + websiteId + "&url=" + favicon);
- xhr = null;
- }
- catch (e)
- {}
- }
- }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
- }
- };
-
- /**
- * Constructor.
- */
- (function() { this._init(); }).apply(samfind_modfavicon);