[英]Webfont not working in Chrome extension
I've been following pretty much all tutorials there are to use a webfont in your chrome extension, but none of them have worked for me, it still continues to be the same ugly font. 我一直关注着几乎所有的教程,都在chrome扩展程序中使用了webfont,但是它们都对我没有用,它仍然是同样丑陋的字体。
I have downloaded this font as zip ( https://fonts.google.com/specimen/Raleway?selection.family=Raleway ) and saved Raleway-Regular.ttf under assets/fonts/
我已将该字体下载为zip( https://fonts.google.com/specimen/Raleway?selection.family=Raleway ),并将Raleway-Regular.ttf保存在
assets/fonts/
I have also added the file to web_accessible_resources. 我还将该文件添加到了web_accessible_resources中。
"web_accessible_resources": [
"assets/fonts/Raleway-Regular.ttf"
]
In the style.css I have the following code in the top 在style.css中,顶部有以下代码
@font-face {
font-family: 'UniqueNameFont';
font-style: normal;
font-weight: 700;
src: url('chrome-extension://<id_hidden>/assets/fonts/Raleway-Regular.ttf');
}
* {
font-family: 'UniqueNameFont';
box-sizing: border-box;
}
The path to the file is correct, I've checked. 我已经检查了文件路径是否正确。 Also, I don't get any errors in the console log.
另外,控制台日志中没有任何错误。
Now, my extension has a shadow dom, which keeps my extension inside there, I then dynamically add the style.css file and the html. 现在,我的扩展名有一个影子dom,将扩展名保留在其中,然后我动态添加style.css文件和html。 Like this..
像这样..
// Add stylesheet
var link = document.createElement("link")
link.type = "text/css";
link.rel = "stylesheet";
link.href = chrome.runtime.getURL('style.css');
shadow.appendChild(link);
// Add content to shadow
page = page.replace(/baseuri/g, extensionOrigin);
shadow.innerHTML += page;
All of the styling works, except for the font face. 除字体外,所有样式均有效。 I'm guessing that it might have something to do with dynamically adding the content, but I find it very weird, since the styling of the elements are fine.
我猜想它可能与动态添加内容有关,但是我发现它很奇怪,因为元素的样式很好。
I have also experimented with the @import that Google Fonts provided, and I could see in the Networks tab that it loaded the file, but for some weird reason it doesn't effect anything. 我还尝试了Google字体提供的@import,并且可以在“网络”标签中看到它加载了文件,但是出于某些奇怪的原因,它没有任何作用。
Seems it worked when I used Google Web Font Loader library, it's pretty straight forward. 当我使用Google Web Font Loader库时,它似乎可以正常工作。
var gwebfont = document.createElement("script");
gwebfont.src = "https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js";
var callback = function (){
var s = document.createElement('script');
s.type = 'text/javascript';
var code = "WebFont.load({google: { families: ['Open Sans', 'Open Sans:light', 'Open Sans:semi-bold', 'Open Sans:bold'] } });";
try {
s.appendChild(document.createTextNode(code));
shadow.appendChild(s);
} catch (e) {
s.text = code;
shadow.appendChild(s);
}
}
gwebfont.onload = callback;
shadow.appendChild(gwebfont);
And then you simply add the font family in your stylesheet 然后您只需在样式表中添加字体系列
* {
font-family: 'Open Sans', sans-serif;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.