// ==UserScript== // @name Muse — Google Sans Flex + M PLUS Rounded 1c // @namespace kamatama // @version 2.0 // @description muse.ai のフォントを Google Sans Flex(欧文)+ M PLUS Rounded 1c Medium(日本語)に変更する。v2: サイト側の "Optimistic" フォント名の中身を @font-face で差し替える方式に変更(特異度戦争を回避) // @match https://muse.ai/* // @run-at document-start // @grant none // @license MIT // ==/UserScript== (function () { 'use strict'; // ---- 方式の説明 ---- // v1 は `* { font-family: ... !important }` でサイトの指定を上書きしていたが、 // `*` は特異度が最低なので、サイト側がクラスセレクタ+!important で // font-family: "Optimistic", system-ui, sans-serif を指定すると負ける。 // v2 は発想を変えて、サイトの指定には触らず "Optimistic" という // フォント名の中身(実体)を @font-face で差し替える。 // - 欧文レンジ: Google Sans Flex の実体を "Optimistic" の名で登録 // - 日本語レンジ: M PLUS Rounded 1c の実体を同じく "Optimistic" の名で登録 // (unicode-range で振り分けるので、1つの名前で2フォントを合成できる) // 同じ family 名の @font-face が複数ある場合、文書順で後ろのものが優先されるため、 // このスタイル要素を常に head の末尾に置くよう MutationObserver で維持する。 const CSS2_URL = 'https://fonts.googleapis.com/css2?family=Google+Sans+Flex:opsz,wght@6..144,1..1000&family=M+PLUS+Rounded+1c:wght@500&display=swap'; // フォールバック用: v1 の Google Fonts 読み込み+全要素ルールも併用する // (効く場所では効くし、害はない) const link = document.createElement('link'); link.rel = 'stylesheet'; link.href = CSS2_URL; (document.head || document.documentElement).appendChild(link); const fallback = document.createElement('style'); fallback.textContent = ` *:not(pre):not(code):not(kbd):not(samp) { font-family: 'Google Sans Flex', 'M PLUS Rounded 1c', sans-serif !important; } `; (document.head || document.documentElement).appendChild(fallback); // Google Fonts の css2 を取得し、family 名を 'Optimistic' に書き換えた // @font-face 群を作る。欧文(Flex)を後に置いて、重なるレンジでは Flex を優先。 async function buildAliasStyle() { try { const res = await fetch(CSS2_URL, { credentials: 'omit' }); if (!res.ok) return; const css = await res.text(); const blocks = css.match(/@font-face\s*{[^}]*}/g) || []; const flex = []; const rounded = []; for (const b of blocks) { if (b.includes("'Google Sans Flex'")) { flex.push(b.replace(/font-family:\s*'Google Sans Flex'/, "font-family: 'Optimistic'")); } else if (b.includes("'M PLUS Rounded 1c'")) { rounded.push(b.replace(/font-family:\s*'M PLUS Rounded 1c'/, "font-family: 'Optimistic'")); } } if (!flex.length && !rounded.length) return; const style = document.createElement('style'); style.id = 'kamatama-optimistic-alias'; style.textContent = [...rounded, ...flex].join('\n'); document.head.appendChild(style); keepLast(style); } catch (e) { /* 取得に失敗したらフォールバックのルールだけが残る */ } } // 自分の style 要素が head の末尾でなくなるたびに末尾へ戻す // (サイト側が後から自前の @font-face を差し込んできても負けないため) function keepLast(style) { const move = () => { if (style.parentNode === document.head && document.head.lastElementChild !== style) { document.head.appendChild(style); } }; new MutationObserver(move).observe(document.head, { childList: true }); move(); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', buildAliasStyle, { once: true }); } else { buildAliasStyle(); } })();