[MenuWizard] Load/Restore icons the web-sites in Bookmarks

Announce and Discuss the Latest Theme and Extension Releases.
Post Reply
pag77
Posts: 1642
Joined: December 26th, 2013, 10:46 pm

[MenuWizard] Load/Restore icons the web-sites in Bookmarks

Post by pag77 »

It is a solution for the add-on "Menu Wizard"
https://addons.mozilla.org/addon/s3menu-wizard/
http://forums.mozillazine.org/viewtopic ... &t=2828771

From time to time in the Bookmarks disappear icons for websites, for example, after the synchronization of bookmarks across Firefox-syncing
This solution will restore icons - each time you open bookmarks will be checked for the presence of an icon of a particular site and then try to recover it.

Install:
  1. Install Menu Wizard, if it is not already in your browser
  2. Open the settings Menu Wizard: "Tools > Menu Wizard", "Add-ons > Menu Wizard > Settings" or type in the address bar about:config-menu
  3. Open the menu "Menu Bar"
  4. Open the properties in the "Bookmarks"
    Image
  5. In the "Command > onPopupShowing", insert this code:

    Code: Select all

    this.check_icons = function(event) {
    	var menu_popup = event.target;
    	var menu_items = menu_popup.childNodes;
    	for (var i=0; i<menu_items.length; i++) {
    		var menu = menu_items[i];
    		if ((menu.localName == 'menuitem') && (menu._placesNode)) {
    			var place = menu._placesNode;
    			if ((place.icon == '') && (/^https?\:\/\//.test(place.uri))) {
    				this.get_icon_html(place.uri);
    			}
    		}
    	}
    }
    this.get_icon = function(page_url) {
    	var favicon_url = /^https?\:\/\/[^\/]+/i.exec(page_url);
    	if (! favicon_url) { return; }
    
    	favicon_url = favicon_url[0] + '/favicon.ico';
    
    	var req = new XMLHttpRequest();
    	var self = this;
    	req.onreadystatechange = function () {
    		if (req.readyState == 4) {
    			if (req.status == 200) {
    				self.set_icon(page_url, favicon_url);
    			}
    		}
    	};
    
    	req.open("GET", favicon_url, true);
    	req.send(null);
    }
    this.get_icon_html = function(page_url) {
    	var req = new XMLHttpRequest();
    	var self = this;
    	req.onreadystatechange = function () {
    		if (req.readyState == 4) {
    			if (req.status == 200) {
    				var favicon_url = self.icon_parse_html(page_url, req.responseText);
    				if (favicon_url) {
    					self.set_icon(page_url, favicon_url);
    				} else {
    					self.get_icon(page_url);
    				}
    			} else {
    				self.get_icon(page_url);
    			}
    		}
    	};
    
    	req.open("GET", page_url, true);
    	req.send(null);
    }
    this.icon_parse_html = function(page_url, responseText) {
    	responseText = responseText.replace(/<\!\-\-.*?\-\->/g, '');
    	var RegEx_head = /<head.*>[\s\S]+<\/head>/i;
    	var RegEx_base = /<base\s*href\s*=\s*"(.*)"\s*>/i;
    	var RegEx_link = /<link[\s\S]+?>/gi;
    	var RegEx_rel_sicon = /rel[\s]*?=[\s]*?["\'][\s]*?(?:shortcut)[\s]*?icon[\s]*?["\']/i;
    	var RegEx_rel_icon = /rel[\s]*?=[\s]*?["\'][\s]*?icon[\s]*?["\']/i;
    	var RegEx_href = /href[\s]*?=[\s]*?["\'](.+?)[\s]*?["\']/i;
    
    	var head = RegEx_head.exec(responseText);
    	var base = RegEx_base.exec(head);
    	var link = '';
    	var href = '';
    
    	var nsIIOService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
    	var PageURI = nsIIOService.newURI(page_url, null, null);
    
    	var resolvedPageURI = PageURI;
    	var FavIconURL = "";
    
    	if (base !== null) {
    		resolvedPageURI = nsIIOService.newURI(PageURI.resolve(base[1]), null, null);
    	}
    	while ((link = RegEx_link.exec(head)) !== null) {
    		if (RegEx_rel_sicon.test(link) === true) {
    			href = RegEx_href.exec(link);
    			if (href !== null) {
    				FavIconURL = href[1];
    				break;
    			}
    		}
    		RegEx_link.lastIndex;
    	}
    
    	RegEx_link.lastIndex = 0;
    	if (FavIconURL === "") {
    		while ((link = RegEx_link.exec(head)) !== null) {
    			if (RegEx_rel_icon.test(link) === true) {
    				href = RegEx_href.exec(link);
    				if (href !== null) {
    					FavIconURL = href[1];
    					break;
    				}
    			}
    			RegEx_link.lastIndex;
    		}
    	}
    	RegEx_link.lastIndex = 0;
    	if (FavIconURL) {
    		FavIconURL = resolvedPageURI.resolve(FavIconURL);
    	}
    	return FavIconURL;
    }
    this.set_icon = function(page_url, favicon_url) {
    	var faviconService = Components.classes["@mozilla.org/browser/favicon-service;1"].getService(Components.interfaces.nsIFaviconService);
    
    	var nsIIOService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
    	faviconService.setAndFetchFaviconForPage(nsIIOService.newURI(page_url, null, null), nsIIOService.newURI(favicon_url, null, null), false, faviconService.FAVICON_LOAD_NON_PRIVATE);
    }
    this.setAttribute("onpopupshown", "this.check_icons(event)");
    
  6. Click "Apply"
PS:
For testing the code, you can use an addon to remove icons: Delete Bookmark Icons
A small piece of code was taken from the addon: FavIcon Reloader
Post Reply