Google Maps v3 - prevent API from loading Roboto font

后端 未结 3 1776
忘掉有多难
忘掉有多难 2020-12-01 02:41

Google adds styles to the maps container that override my styles.
I know how to fix this. But the API (v3.8/9/exp) also loads the webfont \"Roboto\" which I don\'t reall

3条回答
  •  失恋的感觉
    2020-12-01 03:36

    I found above solution to prevent websites with Google Maps from loading Roboto.

    If you - like I do - use Wordpress, there might be other plugins referring to Google Fonts.

    However, I struggled on some of my websites with the above code, since parts of it (1) affected also other styles to load, (2) "killed" styles, which intentionally not only contained gm-style, but other styles as well and (3) not affected other Google Fonts to load, where one or another plugin added links to fonts.googleapis.com by DOM-manipulation as well.

    The below worked for me. It simply prevents other scripts from adding any tag having https://fonts.googleapis.com in it's href-attribute.

    (function($) {
    var isGoogleFont = function (element) {
        // google font download
        if (element.href
            && element.href.indexOf('https://fonts.googleapis.com') === 0) {
            return true;
        }       
        return false;
    }
    
    // we override these methods only for one particular head element
    // default methods for other elements are not affected
    var head = $('head')[0];
    
    var insertBefore = head.insertBefore;
    head.insertBefore = function (newElement, referenceElement) {
        if (!isGoogleFont(newElement)) {
            insertBefore.call(head, newElement, referenceElement);
        }
    };
    
    var appendChild = head.appendChild;
    head.appendChild = function (textNode) {
        if (!isGoogleFont($(textNode)[0])) {
            appendChild.call(head, textNode);
        }
    };
    })(jQuery);
    

提交回复
热议问题