问HN:为什么我无法在HN上应用自定义字体?

3作者: -__-7 天前原帖
我正在构建一个具有无障碍功能的移动浏览器(基于webkit),其中包括一个选项,可以用OpenDyslexic字体覆盖页面字体。我尝试了以下所有变体: ```javascript const style = document.createElement('style'); style.textContent = ` @font-face { font-family: 'OpenDyslexic'; src: url('data:font/opentype;base64,...') format('opentype'); } * { font-family: 'OpenDyslexic', sans-serif !important; } `; document.head.appendChild(style); ``` 这段代码在其他地方都能正常工作。我从这个页面点击的每一个链接,找到的每一个90年代的网站都能正常显示,唯独在这里不行! 在HN(Hacker News)上,font-family的CSS确实应用了(我在检查器中可以看到,元素显示font-family: OpenDyslexic !important),但实际渲染的字体却回退到了sans-serif。@font-face确实在DOM中,base64也是有效的,这段代码在其他网站上都能正常工作。 我尝试了:使用blob URL而不是data URL,使用MutationObserver重新应用样式,针对特定元素(如font、td、table),设置setTimeout延迟等等,尝试了很多方法,但在HN上都没有效果。 HN的标记或头部是否有什么特别之处,导致@font-face静默失败?有没有人遇到过这个问题,或者知道可能发生的原因?内容安全策略和CORS似乎没有问题,因为base64在DOM中。我可能遗漏了什么?
查看原文
I&#x27;m building a mobile browser (webkit base) with accessibility features, including an option to override page fonts with OpenDyslexic. I have tried every permutation of:<p>const style = document.createElement(&#x27;style&#x27;); style.textContent = ` @font-face { font-family: &#x27;OpenDyslexic&#x27;; src: url(&#x27;data:font&#x2F;opentype;base64,...&#x27;) format(&#x27;opentype&#x27;); } * { font-family: &#x27;OpenDyslexic&#x27;, sans-serif !important; } `; document.head.appendChild(style);<p>it works everywhere. every link I click from this page, every website from the 90s I can find. everywhere except here!<p>On HN, the font-family CSS applies (I can see it in the inspector, elements show font-family: OpenDyslexic !important), but the actual rendered font falls back to sans-serif. The @font-face is in the DOM. The base64 is valid, the code works on other sites.<p>Tried: blob URLs instead of data URLs, MutationObserver to reapply, targeting specific elements (font, td, table), setTimeout delays. so much more. Nothing works on HN specifically.<p>Is there something about HN&#x27;s markup or headers that would cause @font-face to fail silently? Has anyone dealt with this issue or know why it could be happening? content security policy &amp; cors don’t seem at fault bc the base64 is in the dom. What might I be missing?