Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding @font-face stylesheet rules to chrome extension

What is the recommended way to add a @font-face stylesheet rule through a chrome-extension? The problem is that the url of the font embed is located from within the extension, so I must do it in javascript in order to use chrome.extension.getURL.

I have tried document.styleSheets[0].addRule through a content-script, but that did not work. To clarify, I also have the font listed under web_accessible_resources.

like image 271
badunk Avatar asked Aug 18 '12 01:08

badunk


2 Answers

You can also specify any additional stuff your extension uses in manifest.json in web_accesible_resources section. Docs are here.

Add this into your manifest.json file:

 "web_accessible_resources": [
    "fonts/*.*",
    "templates/*.html"
 ]

And also correct urls in your css:

@font-face {
  font-family: 'MyFont';
  src: url('chrome-extension://__MSG_@@extension_id__/fonts/font.eot') /* ... */;
}
like image 61
Max Podriezov Avatar answered Oct 24 '22 02:10

Max Podriezov


Inject a <style> node, in your content-script. Something like so:

var styleNode           = document.createElement ("style");
styleNode.type          = "text/css";
styleNode.textContent   = "@font-face { font-family: YOUR_FONT; src: url('"
                        + chrome.extension.getURL ("YOUR_FONT.otf")
                        + "'); }"
                        ;
document.head.appendChild (styleNode);
like image 21
Brock Adams Avatar answered Oct 24 '22 01:10

Brock Adams