A bookmarklet to extract favicons

15 Dec 2012

At Désinvolt, we often try to found the favicon for a website. This task is easy for me, but for other people, less “geek” than me… it’s hard!

So, I write a little bookmarklet to help them.

var founded = false;
 
var checkFavicon = function(url) {
    var image = new Image()
    image.onload = function() {
        if (!founded) {
            founded = true;
            alert(url);
        }
    };
    image.src = url;
}
 
var searchFavicon = function() {
    founded = false;
    var links = document.getElementsByTagName('link');
    for (i = 0; i < links.length; i++) {
        var link = links[i];
        var rel = link.getAttribute('rel');
        if (rel.match(/icon/)) {
            var favicon = link.getAttribute('href');
            if (!favicon.match(/\/\//)) {
                if (favicon.match(/^\//)) {
                    favicon = location.protocol + '//' +location.host + favicon;
                } else {
                    favicon = location.protocol + '//' +location.host + '/' + favicon;
                }
            }
            checkFavicon(favicon);
        }
    }
    checkFavicon(location.protocol + '//' + location.host + '/favicon.ico');
}
 
searchFavicon();

Drag this link into your favorites, then go to a web page and clic on the new favorite to get the URL of the favicon!

» Search favicon