当站点使用内容策略安全时,如何在Firefox插件中使用字体

How to use font face in Firefox ad-on when the site uses Content Policy Security?

本文关键字:Firefox 字体 插件 站点 策略 安全      更新时间:2023-09-26

我开发了一个允许用户修改网站风格的Firefox插件。他可以选择使用扩展的字体。

Content Policy Security指定它不应该干扰用户安装的浏览器附加组件或扩展的操作:https://en.wikipedia.org/wiki/Content_Security_Policy Browser_add-ons_and_extensions_exemption

当网站使用内容安全策略的"font-src"指令不允许跨域字体时,它不起作用。我尝试通过以下方法在插件的内容脚本中注入css:

var l = document.createElement('LINK');
l.rel = "stylesheet";
l.type = "text/css";
l.href = 'fontface.css';
this.head.appendChild(l);

或者用这种方法:

 s = '@font-face {
font-family: 'customfont';
src: url(data:application/x-font-ttf;charset=utf-8;base64, ... ) format('truetype');
}';
newStyle = document.createElement("style");
newStyle.setAttribute("type", "text/css");
newStyle.appendChild(document.createTextNode(s));
document.getElementsByTagName('head')[0].appendChild(newStyle);

在这两个方法中,我在firebug中有错误:"downloadable font: download not allowed ({css code})"

是否有可能使用自定义字体面对Firefox插件时,网站有CSP指令font-src ?怎么做呢?

尝试在包含文件的路径上的chrome.manifest文件中设置contentaccessible=true,除非它是base64,否则我不确定:https://gist.github.com/Noitidart/9406437#file-chrome-manifest-L1