Google Maps v3 - prevent API from loading Roboto font

后端 未结 3 1774
忘掉有多难
忘掉有多难 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:28

    UPDATE 10/2017

    Google changed the approach of how they inject the styles on the page. Currently they inserting an empty style element and then changing the contents of this style element with Robot font. Here is a new solution:

    // Preventing the Google Maps libary from downloading an extra font
    (function() {
        var isRobotoStyle = function (element) {
    
            // roboto font download
            if (element.href
                && element.href.indexOf('https://fonts.googleapis.com/css?family=Roboto') === 0) {
                return true;
            }
            // roboto style elements
            if (element.tagName.toLowerCase() === 'style'
                && element.styleSheet
                && element.styleSheet.cssText
                && element.styleSheet.cssText.replace('\r\n', '').indexOf('.gm-style') === 0) {
                element.styleSheet.cssText = '';
                return true;
            }
            // roboto style elements for other browsers
            if (element.tagName.toLowerCase() === 'style'
                && element.innerHTML
                && element.innerHTML.replace('\r\n', '').indexOf('.gm-style') === 0) {
                element.innerHTML = '';
                return true;
            }
            // when google tries to add empty style
            if (element.tagName.toLowerCase() === 'style'
                && !element.styleSheet && !element.innerHTML) {
                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 (!isRobotoStyle(newElement)) {
                insertBefore.call(head, newElement, referenceElement);
            }
        };
    
        var appendChild = head.appendChild;
        head.appendChild = function (textNode) {
            if (!isRobotoStyle($(textNode)[0])) {
                appendChild.call(head, textNode);
            }
        };
    })();
    

    ORIGINAL ANSWER

    Thanks to coma for the solution! I also decided to intercept styles which override the font-family, font-size and font-weight. The complete solution for modern browsers and IE8+:

    // Preventing the Google Maps libary from downloading an extra font
    var head = $('head')[0];
    var insertBefore = head.insertBefore;
    head.insertBefore = function (newElement, referenceElement) {
        // intercept font download
        if (newElement.href
            && newElement.href.indexOf('https://fonts.googleapis.com/css?family=Roboto') === 0) {
            return;
        }
        // intercept style elements for IEs
        if (newElement.tagName.toLowerCase() === 'style'
            && newElement.styleSheet
            && newElement.styleSheet.cssText
            && newElement.styleSheet.cssText.replace('\r\n', '').indexOf('.gm-style') === 0) {
            return;
        }
        // intercept style elements for other browsers
        if (newElement.tagName.toLowerCase() === 'style'
            && newElement.innerHTML
            && newElement.innerHTML.replace('\r\n', '').indexOf('.gm-style') === 0) {
            return;
        }
        insertBefore.call(head, newElement, referenceElement);
    };
    

提交回复
热议问题