【发布时间】:2013-11-15 01:22:04
【问题描述】:
应该如何加载谷歌字体,我真的必须下载和打包我在应用程序中使用的每一种字体吗?我试图避免打包字体,因为它们太多以至于我的应用程序会很大(它是一个网络编辑器)
<link href='http://fonts.googleapis.com/css?family=Nunito' rel='stylesheet' type='text/css'>
> Refused to load the stylesheet 'http://fonts.googleapis.com/css?family=Nunito' because it violates the following Content Security Policy directive: "style-src 'self' data: chrome-extension-resource: 'unsafe-inline'".
我以为我可以将它作为 blob 加载,但我不确定它是否可以完成。数据已下载但没有任何变化,我试过这个:
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://themes.googleusercontent.com/static/fonts/nunito/v4/0rdItLTcOd8TSMl72RUU5w.woff", true);
xhr.responseType = "blob";
xhr.onreadystatechange = function() {
console.log("STATE", xhr.readyState);
if (xhr.readyState == 4) {
var myfontblob = window.webkitURL.createObjectURL(xhr.response);
$("<style>").text("@font-face {\
font-family: 'Nunito';\
font-style: normal;\
font-weight: 400;\
src: '"+myfontblob+"' format('woff');\
}").prependTo("head");
}
};
xhr.send();
【问题讨论】:
-
好吧,如果它是一个 blob,您必须通过
xhr.response访问它。 -
@JoshLee 我试图避免打包字体,因为它们太多了,以至于我的应用程序会很大(它是一个网络编辑器)
-
不是扩展问题的副本——因为 CSP 的工作方式不同。
-
@JoshLee 是一个有点不同的话题,我看到 shuji 要求 Google Chrome 中的打包应用程序而不是扩展程序。在 Google Chrome 中打包应用和扩展是两个不同的概念。
标签: javascript blob webfonts google-chrome-app