Changing website favicon dynamically

前端 未结 15 1605
温柔的废话
温柔的废话 2020-11-22 02:17

I have a web application that\'s branded according to the user that\'s currently logged in. I\'d like to change the favicon of the page to be the logo of the private label,

15条回答
  •  清歌不尽
    2020-11-22 02:38

    I use this feature all the time when developing sites ... so I can see at-a-glance which tab has local, dev or prod running in it.

    Now that Chrome supports SVG favicons it makes it a whole lot easier.

    Tampermonkey Script

    Have a gander at https://gist.github.com/elliz/bb7661d8ed1535c93d03afcd0609360f for a tampermonkey script that points to a demo site I chucked up at https://elliz.github.io/svg-favicon/

    Basic code

    Adapted this from another answer ... could be improved but good enough for my needs.

    (function() {
        'use strict';
    
        // play with https://codepen.io/elliz/full/ygvgay for getting it right
        // viewBox is required but does not need to be 16x16
        const svg = `
        
          
          
        
        `;
    
        var favicon_link_html = document.createElement('link');
        favicon_link_html.rel = 'icon';
        favicon_link_html.href = svgToDataUri(svg);
        favicon_link_html.type = 'image/svg+xml';
    
        try {
            let favicons = document.querySelectorAll('link[rel~="icon"]');
            favicons.forEach(function(favicon) {
                favicon.parentNode.removeChild(favicon);
            });
    
            const head = document.getElementsByTagName('head')[0];
            head.insertBefore( favicon_link_html, head.firstChild );
        }
        catch(e) { }
    
        // functions -------------------------------
        function escapeRegExp(str) {
            return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
        }
    
        function replaceAll(str, find, replace) {
            return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
        }
    
        function svgToDataUri(svg) {
            // these may not all be needed - used to be for uri-encoded svg in old browsers
            var encoded = svg.replace(/\s+/g, " ")
            encoded = replaceAll(encoded, "%", "%25");
            encoded = replaceAll(encoded, "> <", "><"); // normalise spaces elements
            encoded = replaceAll(encoded, "; }", ";}"); // normalise spaces css
            encoded = replaceAll(encoded, "<", "%3c");
            encoded = replaceAll(encoded, ">", "%3e");
            encoded = replaceAll(encoded, "\"", "'"); // normalise quotes ... possible issues with quotes in 
            encoded = replaceAll(encoded, "#", "%23"); // needed for ie and firefox
            encoded = replaceAll(encoded, "{", "%7b");
            encoded = replaceAll(encoded, "}", "%7d");
            encoded = replaceAll(encoded, "|", "%7c");
            encoded = replaceAll(encoded, "^", "%5e");
            encoded = replaceAll(encoded, "`", "%60");
            encoded = replaceAll(encoded, "@", "%40");
            var dataUri = 'data:image/svg+xml;charset=UTF-8,' + encoded.trim();
            return dataUri;
        }
    
    })();
    

    Just pop your own SVG (maybe cleaned with Jake Archibald's SVGOMG if you're using a tool) into the const at the top. Make sure it is square (using the viewBox attribute) and you're good to go.

提交回复
热议问题