Skip to content

Commit

Permalink
override font-family-modern font weight
Browse files Browse the repository at this point in the history
  • Loading branch information
marcustyphoon committed Jan 31, 2025
1 parent 66da7ca commit d301067
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 2 deletions.
47 changes: 47 additions & 0 deletions dev/update-font-weight-override.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import fs from 'node:fs/promises';
import cssParser from 'css';

try {
const fontWeight350Rules = [];

const sw = await fetch('https://www.tumblr.com/sw.js').then((result) => result.text());
const urls = sw.match(/https:\/\/assets\.tumblr\.com\/pop\/[a-z0-9-]+\.css/g);
for (const url of urls) {
console.log('downloading', url);

const [cssText] = await Promise.all([
fetch(url).then((response) => response.text()),
new Promise((resolve) => setTimeout(resolve, 500))
]);

const ast = cssParser.parse(cssText);
const rules = ast.stylesheet.rules
.flatMap((rule) => rule.rules ?? [rule])
.filter((rule) => rule.type === 'rule');

fontWeight350Rules.push(...rules.filter(
({ declarations }) =>
declarations.find(({ property, value }) => property === 'font-family' && value === 'var(--font-family-modern)') &&
declarations.find(({ property, value }) => property === 'font-weight' && value === '350')
));
}

const selectorSets = [...new Set(fontWeight350Rules.map(({ selectors }) => selectors.join(', ')))];

const override = `/*
Tumblr uses an unusual font weight of 350 on most --font-family-modern (Favorit Modern)
text, which may result in thin text when overriding the --font-family-modern variable.
This file is automatically generated by dev/update-font-weight-override.js and contains
the selectors of all native Tumblr rules which set font-family: var(--font-family-modern)
and font-weight: 350.
*/
${selectorSets.join(',\n')} {
font-weight: normal;
}\n`;

await fs.writeFile('src/fontWeightOverride.css', override, { encoding: 'utf8', flag: 'w+' });
console.log(`wrote ${selectorSets.length} selector sets`);
} catch (e) {
console.log(e);
}
48 changes: 48 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"build-webextension": "web-ext build",
"sign-webextension": "web-ext sign",
"webstore-upload": "chrome-webstore-upload upload --source=src/ --auto-publish",
"update-palette-system-data": "node dev/update-palette-system-data.js"
"update-palette-system-data": "node dev/update-palette-system-data.js",
"update-font-weight-override": "node dev/update-font-weight-override.js"
},
"devDependencies": {
"chrome-webstore-upload-cli": "^3.3.1",
"css": "^3.0.0",
"eslint": "^8.57.1",
"eslint-config-semistandard": "^17.0.0",
"eslint-config-standard": "^17.1.0",
Expand Down
91 changes: 91 additions & 0 deletions src/fontWeightOverride.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Tumblr uses an unusual font weight of 350 on most --font-family-modern (Favorit Modern)
text, which may result in thin text when overriding the --font-family-modern variable.
This file is automatically generated by dev/update-font-weight-override.js and contains
the selectors of all native Tumblr rules which set font-family: var(--font-family-modern)
and font-weight: 350.
*/
.vgL3J,
.TZgeO,
.Gt2Q9,
.n63mn,
.OmZD7, .RSZnt,
.gAJ73,
.AiALR .enHco .fgOG_,
.j8Eiw,
.h7IR_,
.ctBr6,
.CsHnt,
.fBQsy,
.h6QpP,
.zFlRa,
.R4hFV,
.gty8w,
.sG03a,
.EKpJR,
.y0RtB,
.R8_EV,
.deHVW .x66yu, .cJ1ik .x66yu, .PCzZt .x66yu, .deHVW .pLL0g, .cJ1ik .pLL0g, .PCzZt .pLL0g,
.L2HBn .x66yu, .n_fvx .x66yu, .L2HBn .pLL0g, .n_fvx .pLL0g,
.Pl0ah,
.NjLa2,
.KVX8S,
.ZRkRa,
.F0gXR,
.q9P6T,
.u8J1W .wVZAK .M523y,
.Hzn1C,
.YmIEY,
.wjhBM,
.GutOb .BV4gt,
.tSUo6,
.zGDfg,
.QKm6q .BnVIB .mV8AT,
.Ks9nn .ZR5vI,
.FzBTa .GpO9n,
.c1xDI,
.xPEP2,
.rB7hU,
.RPMuM,
.KPqE4 p.zDaqs,
.Sft1t,
.a0QuG,
.dJl65,
.YmlZc,
.KaTRm,
.qyc0j,
.YMD5o,
.pYQdH .uHcx_,
.Nq9JP,
.amytk,
.Z7ew7,
.srYxv,
.OfCo4,
.WiceP,
.f8LOG,
.uaMQw,
.Jo8I3,
._ilsA,
.kbuhn,
.Isbe9,
.SuDWJ,
.nhqrO.ldaI8,
.I3pYl,
.YVaw6 .uJ0hL,
.F3D5L,
.R4UJx,
.XADCw,
.PpzPj,
.L3C_C,
.iWoxw,
.iLTKv,
.YtLa6 .aPaHS,
.YtLa6 .G6FrY,
.mhABv,
.Z_cvW,
.kjdMf .Shb25,
.vgRpB,
.Vt7qF .wL4YA.jk3gM .UEULa {
font-weight: normal;
}
16 changes: 16 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ const paletteSystemData = fetch(browser.runtime.getURL('/paletteSystemData.json'
const setCssVariable = ([property, value]) => document.documentElement.style.setProperty(`--${property}`, value);
const removeCssVariable = ([property]) => document.documentElement.style.removeProperty(`--${property}`);

const documentInteractive = new Promise((resolve) =>
document.readyState === 'loading'
? document.addEventListener('readystatechange', resolve, { once: true })
: resolve()
);

let appliedPaletteEntries = [];

const applyCurrentPalette = async function () {
Expand Down Expand Up @@ -40,6 +46,12 @@ const applyCurrentPalette = async function () {
appliedPaletteEntries = currentPaletteEntries;
};

const fontWeightOverride = Object.assign(document.createElement('link'), {
rel: 'stylesheet',
id: 'palettes-for-tumblr-override',
href: browser.runtime.getURL(`/fontWeightOverride.css?t=${Date.now()}`)
});

const applyFontFamily = async function () {
const { fontFamily = '' } = await browser.storage.local.get('fontFamily');
const { customFontFamily = '' } = await browser.storage.local.get('customFontFamily');
Expand All @@ -52,6 +64,10 @@ const applyFontFamily = async function () {
'--font-family-modern',
fontFamily === 'custom' ? customFontFamily : fontFamily
);
await documentInteractive;
document.getElementById('palettes-for-tumblr-override')?.remove();
(fontFamily === 'custom' ? customFontFamily : fontFamily) &&
document.documentElement.append(fontWeightOverride);
};

const applyFontSize = async function () {
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"permissions": [ "storage" ],
"web_accessible_resources": [{
"resources": [ "*.json" ],
"resources": [ "*.json", "*.css" ],
"matches": [ "*://www.tumblr.com/*" ]
}],

Expand Down

0 comments on commit d301067

Please sign in to comment.