Sunday, November 27, 2011

从页面里提取favicon的javascript

可以支持apple touch icon

    function voyager_urllist2json(list) {
     len = list.length;
        json = '[';
        if( len > 0 ) {
         url = list[0].href;
            json = json + '{"href":"' + url + '"}';
            for( i = 1; i < len; i++ ) {
                url = list[i].href;
                json = json + ',{"href":"' + url + '"}';
            }
        }
        json = json + ']';
        return json;
    }

 document.VoyagerBrowser_getFavicon = function() {
     list = new Array();
     // find apple touch icon first
        appleTouchIconLink = document.querySelector('link[rel="apple-touch-icon"]');
        if( null != appleTouchIconLink ) {
         list.push( appleTouchIconLink );
        }
        // locate short cut icon
        shortcutIcon = document.querySelector('link[rel="shortcut icon"]');
        if( null == shortcutIcon ) {
         shortcutIcon = document.querySelector('link[rel="SHORTCUT ICON"]');
        }
        if( null != shortcutIcon ) {
         list.push( shortcutIcon );
        }
        
  icon = document.querySelector('link[rel="icon"]');
        if( null == icon ) {
         icon = document.querySelector('link[rel="ICON"]');
        }
        if( null != icon ) {
         list.push( icon );
        }
        
        return voyager_urllist2json(list);
    }

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home