Pintia Code Highlight

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();