问题
I'm trying to use a google font with my extension on the content script side - the Noto font I have downloaded and loading from the extension directory works (it is also declared in web_accessible_resources) and works fine in the ShadowDOM but the google font doesn't
I'm injecting this style text into the head:
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);
I also tried putting
@import url("https://fonts.googleapis.com/css?family=Poppins");
in the shadow dom style but that didn't work either
回答1:
Adding the fonts as a link worked, no idea why:
var linkNode = document.createElement("link");
linkNode.type = "text/css";
linkNode.rel = "stylesheet";
linkNode.href = "//fonts.googleapis.com/css?family=Poppins";
document.head.appendChild(linkNode);
also can be done like this:
<link href="//fonts.googleapis.com/css?family=PT+Sans" rel="stylesheet" type="text/css">
as per Google Fonts Font Doesn't load
来源:https://stackoverflow.com/questions/55382081/using-google-fonts-with-shadow-dom