Packaging a font with a Google Chrome extension

前端 未结 3 1072
后悔当初
后悔当初 2020-12-12 22:24

I want to use something other than the standard fonts with my Chrome extension. I was excited about the possibility of using the Google Web Fonts, but it seems that could i

3条回答
  •  轮回少年
    2020-12-12 22:54

    Choose your font. As example I'll take "Stint Ultra Expanded". There is example how to add it to your page:

    
    

    Take only the href and open it in browser. You'll see something like this:

    @font-face {
      font-family: 'Stint Ultra Expanded';
      font-style: normal;
      font-weight: 400;
      src: local('Stint Ultra Expanded'), local('StintUltraExpanded-Regular'), url(http://themes.googleusercontent.com/static/fonts/stintultraexpanded/v1/FeigX-wDDgHMCKuhekhedWmdhyVWfbygZe-w47Q2x_f3rGVtsTkPsbDajuO5ueQw.woff) format('woff');
    }
    

    Take first parameter of url value from src property (http://themes.googleusercontent.com/static/fonts/stintultraexpanded/v1/FeigX-wDDgHMCKuhekhedWmdhyVWfbygZe-w47Q2x_f3rGVtsTkPsbDajuO5ueQw.woff).

    Now download this file.

    Use parameter of local value of src property as filename (Stint Ultra Expanded)

    Easy way is to download it is using wget:

    wget -O "Stint Ultra Expanded.woff" http://themes.googleusercontent.com/static/fonts/stintultraexpanded/v1/FeigX-wDDgHMCKuhekhedWmdhyVWfbygZe-w47Q2x_f3rGVtsTkPsbDajuO5ueQw.woff
    

    Now create css file and add content that you have seen before. But you have to change value of src property. Remove all locals and adjust url. It should be smth like this:

    @font-face {
      font-family: 'Stint Ultra Expanded';
      font-style: normal;
      font-weight: 400;
      src: url('../fonts/Stint Ultra Expanded.woff') format('woff');
    }
    

    Now add your css file to extension and use the font:

    popup.html

    
    
    
        
        
    
    
    
        Hello Font
    
    
    
    

    If you would like to use this font in content_script you have to adjust url in your css:

    @font-face {
      font-family: 'Stint Ultra Expanded';
      font-style: normal;
      font-weight: 400;
      src: url('chrome-extension://__MSG_@@extension_id__/fonts/Stint Ultra Expanded.woff') format('woff');
    }
    

    You can add as many @font-face rules in your css as you like.

    Notice: local value in src property tells you which name should be used to store it. It is good idea to use this name.

    Second notice: If you are using it like google expected, your browser would check if this font is already available local. If this is not the case, then it would be downloaded and stored. So next time it would use font that was previously stored.

    As of Chrome 37 (maybe earlier), you need to mention the font as a web accessible resource in the extension's manifest:

    "web_accessible_resources": [
      "font/*.woff2"
    ]
    

    Otherwise you would see an error like:

    Denying load of chrome-extension://{ID}/font/My Web Font.woff2. 
    Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.
    

提交回复
热议问题