Firefox extension custom fonts

前端 未结 2 1208
旧巷少年郎
旧巷少年郎 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:11

    I think you need to use absolute paths in those url fields. So figure out what seld.data.url('fonts') (by like console.log'ing it) (the path should be something like resource://weraweraer-at-jetpack/data/fonts) and then update your url's to use that path.

    I'm pretty sure this should work because resource url's don't have security restrictions. See this page here: https://developer.mozilla.org/en-US/docs/Chrome_Registration#resource

    It says:

    Note: There are no security restrictions preventing web content from including content at resource: URIs, so take care what you make visible there.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题