如何在内容脚本中的 Chrome 扩展上使用 @font-face
由于我无法在 CSS 文件上使用 chrome.extension.getURL()
,如何将 @font-face 与本地字体文件一起使用?
Since I can't use chrome.extension.getURL()
on a CSS file, how can I use @font-face with a local font file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是如何在 css 中获取本地路径:
更多信息此处。
Here is how to get local path in css:
More about it here.
这个解决方案最终对我有用:
它将样式节点注入到内容脚本的文档中。
对于 Font Awesome,您只需要 Chrome 的 .woff src。
将 @font-face 样式表规则添加到 chrome 扩展
我的代码:
在你的清单中:
This solution finally worked for me:
It injects a style node into the document of the content script.
And for Font Awesome, you only need the .woff src for Chrome.
Adding @font-face stylesheet rules to chrome extension
My code:
In your manifest:
老问题,但我认为这是最好的解决方案:
Firefox 扩展自定义字体
它同样适用于chrome 扩展,因为您不是指向字体文件,而是在 CSS 中包含该字体的 base64 编码版本。
Old question, but this I think is the best solution:
Firefox extension custom fonts
It applies equally for chrome extensions because rather than pointing to a font file, you're including the base64 encoded version of the font right in the CSS.
只需使用相对 URL。它更简单、更干净,而且是正确的做法™:
我知道这个问题很老了,但它是 Google 上的最高结果,而且公认的答案效率低下。
编辑:其他人似乎对此得到了不同的结果。我应该提到的是,在内容脚本中使用时它可能不起作用。我已经在弹出脚本中对此进行了测试,效果很好。
Just use a relative URL. It's simpler, cleaner, and is The Right Thing To Do™:
I know this question is old but it's a top result on Google and the accepted answer is inefficient.
EDIT: It seems that others are getting mixed results for this. I should mention that it probably doesn't work when used in Content Scripts. I've tested this in Popup Scripts and it works fine.