How to use @font-face on a Chrome Extension in a Content Script

匿名 (未验证) 提交于 2019-12-03 02:50:02

问题:

Since I can't use chrome.extension.getURL() on a CSS file, how can I use @font-face with a local font file?

回答1:

Here is how to get local path in css:

body {   background-image:url('chrome-extension://__MSG_@@extension_id__/background.png'); } 

More about it here.



回答2:

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:

var fa = document.createElement('style');     fa.type = 'text/css';     fa.textContent = '@font-face { font-family: FontAwesome; src: url("'         + chrome.extension.getURL('lib/fa/fonts/fontawesome-webfont.woff?v=4.0.3')         + '"); }'; document.head.appendChild(fa); 

In your manifest:

"css":[     "lib/fa/css/font-awesome.min.css",     ...     ]  "web_accessible_resources":[     "lib/fa/fonts/*",     ...     ] 


回答3:

@font-face {     font-family: 'FontAwesome';     src: url('../font/fontawesome-webfont.eot'); } 

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.



回答4:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!