霞鹜文楷

· · 休闲·娱乐

用着用着Amazing luogu,字体实在是夯到我了

所以我用AI问了一下是啥字体,告诉我是霞鹜文楷,然后用篡改猴做出了一个修改字体的插件

前置:篡改猴

::::info{open} 安装并固定(可有可无,方便操作)后依次点击图标,添加新脚本,把代码粘贴进去,点击文件,点击保存,然后进入洛谷刷新一下即可

:::info[如果你发现无效并图标有一个×] 可能是权限原因导致,修改权限试试 ::: ::::

:::success[Code]

// ==UserScript==
// @name         洛谷霞鹜文楷字体
// @namespace    https://www.luogu.com.cn/
// @version      1.0
// @description  将洛谷全站字体替换为 LXGW WenKai Screen(霞鹜文楷),来源 dash.amlg.top
// @author       马鲁逸尘2014->洛谷(luogu.com.cn)
// @match        *://*.luogu.com.cn/*
// @match        *://luogu.com.cn/*
// @icon         https://www.luogu.com.cn/favicon.ico
// @grant        GM_addStyle
// @grant        GM_addElement
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  // ===== 配置 =====
  // 字体 CSS 来源(dash.amlg.top 的字体样式表)
  // lwsw.min.css 包含全部 4 个变体的 @font-face 声明
  const FONT_CSS_URL = 'https://dash.amlg.top/public/lwsw.min.css';

  // 主字体族名称(与 dash.amlg.top 页面 body 使用的一致)
  const FONT_FAMILY = '"LXGW WenKai Screen", "LXGW WenKai GB Screen", system-ui, -apple-system, sans-serif';

  // ===== 1. 加载字体 CSS =====
  // 使用 @import 引入 dash.amlg.top 的字体样式表
  // 这样所有 @font-face 声明和 woff2 子集文件都会自动按需加载
  GM_addStyle(`
    @import url("${FONT_CSS_URL}");
  `);

  // ===== 2. 覆盖洛谷全站字体 =====
  GM_addStyle(`
    /* ---------- 全局字体覆盖 ---------- */
    :root {
      --font-family: ${FONT_FAMILY} !important;
      --luogu-font-family: ${FONT_FAMILY} !important;
    }

    html,
    body {
      font-family: ${FONT_FAMILY} !important;
    }

    /* ---------- 通用元素 ---------- */
    body,
    body *,
    #app,
    #app * {
      font-family: inherit !important;
    }

    /* 确保 input/textarea/button/select 也继承 */
    input,
    textarea,
    button,
    select {
      font-family: ${FONT_FAMILY} !important;
    }

    /* ---------- 洛谷特定 UI 组件 ---------- */
    /* 导航栏 */
    .navbar,
    .navbar *,
    .nav-item,
    .nav-link {
      font-family: ${FONT_FAMILY} !important;
    }

    /* 题目内容区 */
    .problem-content,
    .problem-content *,
    .markdown-body,
    .markdown-body *,
    .lfe-body,
    .lfe-body * {
      font-family: ${FONT_FAMILY} !important;
    }

    /* 代码块保留等宽字体(可选:如想代码也用文楷,删除此段) */
    pre,
    code,
    .hljs,
    .CodeMirror,
    .CodeMirror *,
    .ace_editor,
    .ace_editor *,
    .cm-editor,
    .cm-editor *,
    textarea.code-editor {
      font-family: "LXGW WenKai Screen", "Fira Code", "Source Code Pro",
        "Consolas", "Monaco", monospace !important;
    }

    /* 侧边栏、卡片、按钮 */
    .card,
    .card *,
    .sidebar,
    .sidebar *,
    .btn,
    .btn *,
    .am-btn,
    .am-btn * {
      font-family: ${FONT_FAMILY} !important;
    }

    /* 讨论区 */
    .forum-post,
    .forum-post *,
    .comment,
    .comment *,
    .reply-content,
    .reply-content * {
      font-family: ${FONT_FAMILY} !important;
    }

    /* 排行榜、表格 */
    table,
    table *,
    .table,
    .table * {
      font-family: ${FONT_FAMILY} !important;
    }

    /* 弹窗 / 模态框 */
    .modal,
    .modal *,
    .dialog,
    .dialog *,
    .popup,
    .popup * {
      font-family: ${FONT_FAMILY} !important;
    }

    /* 编辑器工具栏等 UI */
    .toolbar,
    .toolbar *,
    .editor-toolbar,
    .editor-toolbar * {
      font-family: ${FONT_FAMILY} !important;
    }
  `);

  // ===== 3. 处理动态插入的 DOM(SPA 路由切换) =====
  // 洛谷是 Vue SPA,路由切换时新 DOM 可能丢失继承
  // 用 MutationObserver 兜底确保新增元素也用目标字体
  const observer = new MutationObserver((mutations) => {
    for (const m of mutations) {
      for (const node of m.addedNodes) {
        if (node.nodeType === Node.ELEMENT_NODE && node.style) {
          const computed = getComputedStyle(node);
          if (
            computed.fontFamily &&
            !computed.fontFamily.includes('LXGW WenKai')
          ) {
            node.style.fontFamily = FONT_FAMILY;
          }
        }
      }
    }
  });

  // 在 DOM 准备好后启动观察
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', () => {
      observer.observe(document.body, { childList: true, subtree: true });
    });
  } else {
    observer.observe(document.body, { childList: true, subtree: true });
  }

  console.log('[洛谷霞鹜文楷] 字体脚本已加载 ✓');
})();

:::

当然,我突然感觉光是只有洛谷那也不太得劲儿

毕竟这么夯的字体怎么能只应用在洛谷上呢?

所以又整了个全局的一个应用

:::success[全局应用字体]

// ==UserScript==
// @name         全局霞鹜文楷字体 (LXGW WenKai Screen)
// @namespace    lxgw-wenkai-global
// @version      1.0
// @description  将所有网站的字体替换为 LXGW WenKai Screen(霞鹜文楷),来源 dash.amlg.top
// @author       马鲁逸尘2014->洛谷(luogu.com.cn)
// @match        *://*/*
// @icon         https://dash.amlg.top/icon.png
// @grant        GM_addStyle
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  // ===== 配置区 =====
  const FONT_CSS_URL = 'https://dash.amlg.top/public/lwsw.min.css';
  const FONT_FAMILY = '"LXGW WenKai Screen", "LXGW WenKai GB Screen", system-ui, -apple-system, sans-serif';
  const CODE_FONT_FAMILY = '"LXGW WenKai Screen", "Fira Code", "Source Code Pro", "Cascadia Code", "JetBrains Mono", Consolas, Monaco, "Courier New", monospace';

  // ===== 1. 注入字体 @font-face =====
  GM_addStyle(`@import url("${FONT_CSS_URL}");`);

  // ===== 2. 全局字体覆盖 =====
  GM_addStyle(`
    /*
     * 全局霞鹜文楷覆盖
     * 策略:用最高优先级覆盖所有可能设置 font-family 的地方
     */

    /* --- 根变量 --- */
    :root {
      --font-family: ${FONT_FAMILY} !important;
      --bs-font-sans-serif: ${FONT_FAMILY} !important;
      --bs-body-font-family: ${FONT_FAMILY} !important;
      --mdb-font-sans-serif: ${FONT_FAMILY} !important;
      --tw-font-sans: ${FONT_FAMILY} !important;
      --font-sans: ${FONT_FAMILY} !important;
    }

    /* --- 全局选择器 --- */
    *,
    *::before,
    *::after {
      font-family: ${FONT_FAMILY} !important;
    }

    /* --- 表单元素(浏览器默认会覆盖继承) --- */
    input,
    textarea,
    select,
    button {
      font-family: ${FONT_FAMILY} !important;
    }

    /* --- 代码块:保留等宽字体,霞鹜文楷优先 --- */
    pre,
    code,
    kbd,
    samp,
    var,
    tt,
    .hljs,
    .CodeMirror,
    .CodeMirror *,
    .ace_editor,
    .ace_editor *,
    .cm-editor,
    .cm-editor *,
    .cm-line,
    .monaco-editor,
    .monaco-editor *,
    .prism-code,
    [class*="highlight"],
    [class*="code-block"],
    [class*="CodeBlock"],
    [class*="codeblock"],
    [data-lang],
    textarea[class*="code"] {
      font-family: ${CODE_FONT_FAMILY} !important;
    }

    /* --- SVG 文字 --- */
    svg text,
    svg tspan {
      font-family: ${FONT_FAMILY} !important;
    }

    /* --- iframe 内无法覆盖(同源策略限制),
         但对同源 iframe 尽力覆盖 --- */
  `);

  // ===== 3. Shadow DOM 兼容 =====
  // 部分网站(如 YouTube、GitHub 的某些组件)使用 Web Components / Shadow DOM
  // 无法通过常规 CSS 覆盖,需要劫持 attachShadow 并注入样式
  const originalAttachShadow = Element.prototype.attachShadow;
  Element.prototype.attachShadow = function (options) {
    const shadow = originalAttachShadow.call(this, options);
    const style = document.createElement('style');
    style.textContent = `
      @import url("${FONT_CSS_URL}");
      *, *::before, *::after {
        font-family: ${FONT_FAMILY} !important;
      }
      input, textarea, select, button {
        font-family: ${FONT_FAMILY} !important;
      }
      pre, code, kbd, samp {
        font-family: ${CODE_FONT_FAMILY} !important;
      }
    `;
    shadow.prepend(style);
    return shadow;
  };

  // ===== 4. MutationObserver 兜底 =====
  // 处理 SPA 路由切换、动态插入的内联样式覆盖 font-family 的情况
  const fixInlineFont = (node) => {
    if (node.nodeType !== Node.ELEMENT_NODE) return;
    // 移除行内 font-family 样式(让 CSS 覆盖生效)
    if (node.style && node.style.fontFamily) {
      node.style.fontFamily = '';
    }
    // 检查 style 属性字符串
    const styleAttr = node.getAttribute && node.getAttribute('style');
    if (styleAttr && /font-family/i.test(styleAttr)) {
      node.setAttribute(
        'style',
        styleAttr.replace(/font-family\s*:[^;]*;?/gi, '')
      );
    }
  };

  const observer = new MutationObserver((mutations) => {
    for (const m of mutations) {
      // 处理新增节点
      for (const node of m.addedNodes) {
        if (node.nodeType === Node.ELEMENT_NODE) {
          fixInlineFont(node);
          if (node.querySelectorAll) {
            node.querySelectorAll('*').forEach(fixInlineFont);
          }
        }
      }
      // 处理 style 属性变更(JS 动态改字体)
      if (m.type === 'attributes' && m.attributeName === 'style') {
        fixInlineFont(m.target);
      }
    }
  });

  const startObserve = () => {
    observer.observe(document.documentElement, {
      childList: true,
      subtree: true,
      attributes: true,
      attributeFilter: ['style'],
    });
  };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', startObserve);
  } else {
    startObserve();
  }

  console.log('[全局霞鹜文楷] 字体已应用到当前页面 ✓');
})();

:::

顺便提一嘴:AI真是太好用了

预览: