-
Notifications
You must be signed in to change notification settings - Fork 878
Fix Cloudflare verification loop by deferring KaTeX fonts #1094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jiawku
wants to merge
5
commits into
ChatGPTBox-dev:master
Choose a base branch
from
jiawku:codex/cf-katex-lazy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
463b84a
Defer KaTeX font loading until math is rendered
jiawku 67e9a8e
Fix KaTeX font loading on restricted pages
jiawku 23d13ee
Omit KaTeX assets from builds without math
jiawku 9c7145f
Load KaTeX faces independently on demand
jiawku ef3e75d
Rescan KaTeX fonts only when formulas change
jiawku File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* Loaded only when ChatGPTBox renders math. */ | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_AMS; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_AMS-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_AMS-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_AMS-Regular.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Caligraphic; | ||
| font-style: normal; | ||
| font-weight: 700; | ||
| src: url('katex-fonts/KaTeX_Caligraphic-Bold.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Caligraphic-Bold.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Caligraphic-Bold.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Caligraphic; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Caligraphic-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Caligraphic-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Caligraphic-Regular.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Fraktur; | ||
| font-style: normal; | ||
| font-weight: 700; | ||
| src: url('katex-fonts/KaTeX_Fraktur-Bold.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Fraktur-Bold.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Fraktur-Bold.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Fraktur; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Fraktur-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Fraktur-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Fraktur-Regular.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Main; | ||
| font-style: normal; | ||
| font-weight: 700; | ||
| src: url('katex-fonts/KaTeX_Main-Bold.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Main-Bold.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Main-Bold.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Main; | ||
| font-style: italic; | ||
| font-weight: 700; | ||
| src: url('katex-fonts/KaTeX_Main-BoldItalic.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Main-BoldItalic.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Main-BoldItalic.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Main; | ||
| font-style: italic; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Main-Italic.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Main-Italic.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Main-Italic.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Main; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Main-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Main-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Main-Regular.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Math; | ||
| font-style: italic; | ||
| font-weight: 700; | ||
| src: url('katex-fonts/KaTeX_Math-BoldItalic.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Math-BoldItalic.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Math-BoldItalic.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Math; | ||
| font-style: italic; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Math-Italic.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Math-Italic.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Math-Italic.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_SansSerif; | ||
| font-style: normal; | ||
| font-weight: 700; | ||
| src: url('katex-fonts/KaTeX_SansSerif-Bold.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_SansSerif-Bold.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_SansSerif-Bold.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_SansSerif; | ||
| font-style: italic; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_SansSerif-Italic.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_SansSerif-Italic.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_SansSerif-Italic.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_SansSerif; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_SansSerif-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_SansSerif-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_SansSerif-Regular.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Script; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Script-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Script-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Script-Regular.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Size1; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Size1-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Size1-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Size1-Regular.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Size2; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Size2-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Size2-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Size2-Regular.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Size3; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Size3-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Size3-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Size3-Regular.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Size4; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Size4-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Size4-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Size4-Regular.ttf') format('truetype'); | ||
| } | ||
| @font-face { | ||
| font-display: swap; | ||
| font-family: KaTeX_Typewriter; | ||
| font-style: normal; | ||
| font-weight: 400; | ||
| src: url('katex-fonts/KaTeX_Typewriter-Regular.woff2') format('woff2'), | ||
| url('katex-fonts/KaTeX_Typewriter-Regular.woff') format('woff'), | ||
| url('katex-fonts/KaTeX_Typewriter-Regular.ttf') format('truetype'); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import Browser from 'webextension-polyfill' | ||
|
|
||
| const fontFiles = [ | ||
| 'KaTeX_AMS-Regular', | ||
| 'KaTeX_Caligraphic-Bold', | ||
| 'KaTeX_Caligraphic-Regular', | ||
| 'KaTeX_Fraktur-Bold', | ||
| 'KaTeX_Fraktur-Regular', | ||
| 'KaTeX_Main-Bold', | ||
| 'KaTeX_Main-BoldItalic', | ||
| 'KaTeX_Main-Italic', | ||
| 'KaTeX_Main-Regular', | ||
| 'KaTeX_Math-BoldItalic', | ||
| 'KaTeX_Math-Italic', | ||
| 'KaTeX_SansSerif-Bold', | ||
| 'KaTeX_SansSerif-Italic', | ||
| 'KaTeX_SansSerif-Regular', | ||
| 'KaTeX_Script-Regular', | ||
| 'KaTeX_Size1-Regular', | ||
| 'KaTeX_Size2-Regular', | ||
| 'KaTeX_Size3-Regular', | ||
| 'KaTeX_Size4-Regular', | ||
| 'KaTeX_Typewriter-Regular', | ||
| ].map((filename) => { | ||
| const [family, variant] = filename.split('-') | ||
| return { | ||
| filename, | ||
| family, | ||
| style: variant.includes('Italic') ? 'italic' : 'normal', | ||
| weight: variant.includes('Bold') ? '700' : '400', | ||
| } | ||
| }) | ||
| const fontLoads = new WeakMap() | ||
|
|
||
| /** Watch actual formula changes instead of rebuilt React child identities. */ | ||
| export function observeKatexFonts(root) { | ||
| let content | ||
| const update = () => { | ||
| const nextContent = root.outerHTML | ||
| if (nextContent === content) return | ||
| content = nextContent | ||
| loadKatexFonts(root) | ||
| } | ||
| const observer = new root.ownerDocument.defaultView.MutationObserver(update) | ||
| observer.observe(root, { | ||
| childList: true, | ||
| characterData: true, | ||
| attributes: true, | ||
| attributeFilter: ['class', 'style'], | ||
| subtree: true, | ||
| }) | ||
| update() | ||
| return () => observer.disconnect() | ||
| } | ||
|
|
||
| function usedFonts(root) { | ||
| const document = root.ownerDocument | ||
| const html = root.querySelector('.katex-html') | ||
| const fonts = new Set() | ||
| if (!html) return fonts | ||
|
|
||
| // Inspect rendered glyphs, not the hidden MathML accessibility copy. | ||
| const walker = document.createTreeWalker(html, 4 /* NodeFilter.SHOW_TEXT */) | ||
| while (walker.nextNode()) { | ||
| const text = walker.currentNode | ||
| if (!text.data.trim()) continue | ||
| const style = document.defaultView.getComputedStyle(text.parentElement) | ||
| const families = style.fontFamily.split(',').map((family) => family.trim().replace(/['"]/g, '')) | ||
| const family = families.find((name) => fontFiles.some((font) => font.family === name)) | ||
| const faces = fontFiles.filter((font) => font.family === family) | ||
| if (!faces.length) continue | ||
|
|
||
| const requestedStyle = /italic|oblique/.test(style.fontStyle) ? 'italic' : 'normal' | ||
| const styledFaces = faces.filter((font) => font.style === requestedStyle) | ||
| const candidates = styledFaces.length ? styledFaces : faces | ||
| const weight = Number(style.fontWeight) > 500 || style.fontWeight === 'bold' ? '700' : '400' | ||
| fonts.add(candidates.find((font) => font.weight === weight) || candidates[0]) | ||
| } | ||
| return fonts | ||
| } | ||
|
|
||
| async function loadFont(font, document) { | ||
| // Retry a transient failure once, even if the mounted formula does not change. | ||
| for (let attempt = 0; attempt < 2; attempt++) { | ||
| try { | ||
| const response = await fetch(Browser.runtime.getURL(`katex-fonts/${font.filename}.woff2`)) | ||
| if (!response.ok) throw new Error(`Failed to fetch ${font.filename}: ${response.status}`) | ||
| // Binary sources avoid the host page's font-src restriction. | ||
| const face = new FontFace(font.family, await response.arrayBuffer(), { | ||
| style: font.style, | ||
| weight: font.weight, | ||
| }) | ||
| await face.load() | ||
| document.fonts.add(face) | ||
| return | ||
| } catch (error) { | ||
| if (attempt === 1) throw error | ||
| await new Promise((resolve) => setTimeout(resolve, 250)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Load only faces used by a rendered KaTeX subtree; cache each face independently. */ | ||
| export function loadKatexFonts(root) { | ||
| if (!root) return Promise.resolve() | ||
| const document = root.ownerDocument | ||
| if (document.location.origin === new URL(Browser.runtime.getURL('/')).origin) { | ||
| if (!document.getElementById('chatgptbox-katex-fonts')) { | ||
| const stylesheet = document.createElement('link') | ||
| stylesheet.id = 'chatgptbox-katex-fonts' | ||
| stylesheet.rel = 'stylesheet' | ||
| stylesheet.href = Browser.runtime.getURL('katex-fonts.css') | ||
| document.head.appendChild(stylesheet) | ||
| } | ||
| return Promise.resolve() | ||
| } | ||
|
|
||
| let cache = fontLoads.get(document) | ||
| if (!cache) { | ||
| cache = new Map() | ||
| fontLoads.set(document, cache) | ||
| } | ||
| return Promise.all( | ||
| [...usedFonts(root)].map((font) => { | ||
| if (!cache.has(font.filename)) { | ||
| const loading = loadFont(font, document).catch((error) => { | ||
| cache.delete(font.filename) | ||
| console.warn('[markdown] Failed to load KaTeX font', error) | ||
| }) | ||
| cache.set(font.filename, loading) | ||
| } | ||
| return cache.get(font.filename) | ||
| }), | ||
| ) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.