Firefox extension custom fonts

前端 未结 2 1225
旧巷少年郎
旧巷少年郎 2020-11-28 14:39

I am using the Firefox Add-on SDK to create an extension and am performing a PageMod. This code is in main.js.

...
exports.main = function() {
          


        
2条回答
  •  情深已故
    2020-11-28 15:26

    If you look into the console messages, this is what you should see:

    downloadable font: download not allowed (font-family: "FontAwesome" style:normal weight:normal stretch:normal src index:0): bad URI or cross-site access not allowed

    Unfortunately for you, web fonts are subject to the same-origin policy which can only be relaxed via CORS. This means that there is no way to load the font file from an extension URL as there is no way to use CORS there.

    This leaves you with two options:

    1. You host the font on a web server with proper CORS headers or use some existing font hosting.
    2. You encode the font as a data: URI. There is a number of data: URI generators available, e.g. this one.

    IMHO the second solution is the preferable one as your extension won't be dependent on any web servers, especially not third-party web servers. Also, it won't introduce any delays caused by font downloading. I tried it and it worked fine:

    @font-face {
      font-family: 'FontAwesome';
      src: url('data:application/octet-stream;base64,d09GRgAB...') format('woff');
      font-weight: normal;
      font-style: normal;
    } 
    

    Side-note: You don't need the full backwards compatibility dance in a Firefox extension, it's sufficient to have the font in the WOFF format.

提交回复
热议问题