Pintia Code Highlight

自定义 Pintia 代码块样式,提升可读性

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Pintia Code Highlight
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  自定义 Pintia 代码块样式,提升可读性
// @author       Rayni
// @match        https://pintia.cn/*
// @grant        none
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    // 等待页面加载完成
    const observer = new MutationObserver(() => {
        const preElements = document.querySelectorAll('.rendered-markdown pre');
        preElements.forEach(pre => {
            // 添加自定义样式
            pre.style.display = 'flex';
            pre.style.backgroundColor = '#FFFFFF'; // 深色背景
            pre.style.color = '#000000';           // 浅灰色文本
            pre.style.border = '1px solid #444';
            pre.style.borderRadius = '8px';
            pre.style.padding = '1rem';
            pre.style.fontFamily = '"Fira Code", "Consolas", monospace';
            pre.style.fontSize = '14px';
            pre.style.wordBreak = 'break-all';
            pre.style.overflowX = 'auto';
            pre.style.margin = '0.5rem 0';
            pre.style.textOverflow = 'ellipsis';
            pre.style.whiteSpace = 'pre-wrap';
        });
    });

    // 观察 DOM 变化,以处理动态加载的内容
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 初始执行一次
    setTimeout(() => {
        observer.disconnect();
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }, 100);
})();