-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
override
font-family-modern
font weight live
- Loading branch information
1 parent
66da7ca
commit 7ee54a1
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,49 @@ | ||
await new Promise((resolve) => | ||
document.readyState === 'loading' | ||
? document.addEventListener('readystatechange', resolve, { once: true }) | ||
: resolve() | ||
); | ||
|
||
export const fontWeightOverride = Object.assign(document.createElement('style'), { | ||
id: 'palettes-for-tumblr-override' | ||
}); | ||
|
||
const getSelectorsFromUrl = async (url) => { | ||
const cssText = await fetch(url).then(response => response.text()); | ||
const sheet = await new CSSStyleSheet().replace(cssText); | ||
|
||
const allCssStyleRules = [...sheet.cssRules] | ||
.filter((rule) => rule instanceof CSSStyleRule && rule.style) | ||
.map(({ selectorText, style }) => ({ | ||
selectorText, | ||
rules: Object.fromEntries([...style].map((key) => [key, style.getPropertyValue(key)])) | ||
})); | ||
|
||
return allCssStyleRules | ||
.filter( | ||
({ rules }) => | ||
rules['font-family'] === 'var(--font-family-modern)' && | ||
rules['font-weight'] === '350' | ||
) | ||
.map(({ selectorText }) => selectorText); | ||
}; | ||
|
||
const cache = {}; | ||
|
||
const processLinkElements = async () => { | ||
[...document.head.querySelectorAll('link[rel="stylesheet"][href^="https://assets.tumblr.com/pop/"]')] | ||
.map(element => element.href) | ||
.forEach(url => { | ||
cache[url] ??= getSelectorsFromUrl(url); | ||
}); | ||
|
||
const selectors = await Promise.all(Object.values(cache)).then(results => results.flat()); | ||
fontWeightOverride.textContent = ` | ||
${selectors.join(', ')} { | ||
font-weight: normal; | ||
} | ||
`; | ||
}; | ||
|
||
processLinkElements(); | ||
new MutationObserver(processLinkElements).observe(document.head, { childList: true }); |