我正在尝试在内容脚本端的扩展中使用谷歌字体 - 我从扩展目录下载并加载的 Noto 字体可以工作(它也在 web_accessible_resources 中声明)并且在 ShadowDOM 中工作正常,但是谷歌字体没有
我将此样式文本注入(inject)头部:
var styleNode = document.createElement("style");
styleNode.type = "text/css";
styleNode.textContent =
"@font-face { font-family: Noto Serif; src: url('" +
browser.extension.getURL("NotoSerifCJKjp-SemiBold.otf") +
"') format('opentype'); } @font-face { font-family: Poppins; font-style: normal; font-weight: 400; src: url(https://fonts.gstatic.com/s/poppins/v6/pxiEyp8kv8JHgFVrJJbecmNE.woff2) format('woff2'); }";
document.head.appendChild(styleNode);
我也尝试过
@import url("https://fonts.googleapis.com/css?family=Poppins");
采用 Shadow dom 风格,但这也不起作用
最佳答案
添加字体作为链接有效,不知道为什么:
var linkNode = document.createElement("link");
linkNode.type = "text/css";
linkNode.rel = "stylesheet";
linkNode.href = "//fonts.googleapis.com/css?family=Poppins";
document.head.appendChild(linkNode);
也可以这样做:
<link href="//fonts.googleapis.com/css?family=PT+Sans" rel="stylesheet" type="text/css">
关于html - 将 Google 字体与 Shadow DOM 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55382081/