Skip to content

Instantly share code, notes, and snippets.

@qrg
Last active March 26, 2018 11:03
Show Gist options
  • Select an option

  • Save qrg/de603bf51458910916ad45492b9360d4 to your computer and use it in GitHub Desktop.

Select an option

Save qrg/de603bf51458910916ad45492b9360d4 to your computer and use it in GitHub Desktop.
Replace specified font-family on the body element.
const EXCLUSIONS = [
'Meiryo',
'メイリオ',
'"MS PGothic"',
'"MS ゴシック"',
'x-locale-body',
'system-ui',
'"Yu Gothic UI"',
'"YuGothic UI"'
].map(f => f.toLowerCase());
const ALT = '"游ゴシック Medium"';
const replaceFontFamily = () => {
const css = (element, property) => window.getComputedStyle(element, null).getPropertyValue(property);
const fonts = css(document.body, 'font-family').split(',').map(f => f.trim());
if (fonts.length === 0) return;
console.log('Replace Specifiied Font Family: original font family:', fonts);
const newFonts = [...new Set(fonts.map(f => {
if (EXCLUSIONS.includes(f.toLowerCase())) {
console.log('Replace Specifiied Font Family: ', 'found: ', f);
return ALT;
}
return f;
}))];
document.body.style.fontFamily = newFonts;
console.log('Replace Specifiied Font Family: current font-family: ', newFonts);
};
window.addEventListener('load', replaceFontFamily, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment