跳到正文

科技·工程

个人主页显示脚本

yajiyi114514约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年2月2日
收录
2026年2月5日
更新
2026年11月10日
篇幅
约 12 分钟

版本

  1. v1当前

在很久很久以前, Greasyfork 上存在着大量类似于“洛谷个人简介显示”的油猴脚本。

然而,最近洛谷大改了个人主页的内容,现在就连 系统维护,该内容暂不可见 的牌子都消失了,于是那些现成的脚本纷纷失效。

为了解决这个问题,我在 GenAI 工具制作了一个简单的脚本,通过分析网页源代码实现个人简介的还原。

使用时,在篡改猴上新建脚本,将代码复制后按 Ctrl+S 保存即可。


代码如下:

JavaScript
// ==UserScript==
// @name         Luogu Introduction View 17 Pro Max
// @namespace    https://tampermonkey.net/
// @version      1.0
// @match        https://www.luogu.com.cn/user/*
// @grant        GM_addStyle
// @grant        GM_getResourceText
// @grant        GM_setClipboard
// @require      https://unpkg.com/marked@4.3.0/marked.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js
// @resource     HLJS_CSS https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/vs.min.css
// ==/UserScript==

(function () {
    'use strict';

    const hljsCss = GM_getResourceText("HLJS_CSS");
    GM_addStyle(hljsCss);

    marked.setOptions({
        highlight: function(code, lang) {
            if (lang && hljs.getLanguage(lang)) {
                return hljs.highlight(code, { language: lang }).value;
            }
            return hljs.highlightAuto(code).value;
        },
        langPrefix: 'hljs language-'
    });

    function getIntro() {
        const ctx = document.getElementById('lentille-context');
        if (!ctx) return null;
        try {
            return JSON.parse(ctx.textContent)?.data?.user?.introduction || null;
        } catch { return null; }
    }

    function insertIntro() {
        if (document.getElementById('tm-intro-card')) return;

        const intro = getIntro();
        if (!intro) return;

        const sidebar = document.querySelector('.sidebar-container.reverse');
        const main = sidebar?.querySelector('div.main');
        if (!main) return;

        const card = document.createElement('div');
        card.id = 'tm-intro-card';
        card.className = 'l-card';
        card.style.marginBottom = '20px';

        let htmlContent = marked.parse(intro);
        card.innerHTML = `
            <div class="tm-intro-box">
                <h3 class="lfe-h3">个人简介</h3>
                <div class="tm-intro markdown-body">
                    ${htmlContent}
                </div>
            </div>
        `;

        card.querySelectorAll('pre').forEach(pre => {
            const code = pre.querySelector('code');
            let lang = 'text';
            if (code) {
                const langClass = Array.from(code.classList).find(c => c.startsWith('language-'));
                if (langClass) lang = langClass.replace('language-', '');
            }

            const toolbar = document.createElement('div');
            toolbar.className = 'tm-code-toolbar';

            const langTag = document.createElement('span');
            langTag.className = 'tm-code-lang';
            langTag.innerText = lang.toUpperCase();

            const copyBtn = document.createElement('span');
            copyBtn.className = 'tm-code-copy';
            copyBtn.innerText = '复制';
            copyBtn.onclick = () => {
                GM_setClipboard(code.innerText, 'text');
                copyBtn.innerText = '已复制!';
                copyBtn.classList.add('success');
                setTimeout(() => {
                    copyBtn.innerText = '复制';
                    copyBtn.classList.remove('success');
                }, 2000);
            };

            toolbar.appendChild(langTag);
            toolbar.appendChild(copyBtn);
            pre.insertBefore(toolbar, pre.firstChild);
        });

        main.append(card);
        injectStyle();
    }

    function injectStyle() {
        if (document.getElementById('tm-intro-style')) return;
        const style = document.createElement('style');
        style.id = 'tm-intro-style';
        style.textContent = `
            .tm-intro-box {
                margin-top: 8px;
                padding: 14px 16px;
                border: 1px solid rgba(0,0,0,0.08);
                border-radius: 10px;
                background: rgba(0,0,0,0.01);
            }
            .tm-intro { font-size: 14px; line-height: 1.75; color: #333; }

            .tm-intro pre {
                position: relative;
                margin: 1.2em 0;
                padding: 35px 16px 14px;
                background: #f8fafc;
                border: 1px solid #e5e7eb;
                border-radius: 8px;
                overflow: hidden;
            }

            .tm-code-toolbar {
                position: absolute;
                top: 0; left: 0; right: 0;
                height: 28px;
                background: #edf2f7;
                display: flex;
                justify-content: space-between;
                align-items: center;
                padding: 0 12px;
                font-family: sans-serif;
                font-size: 11px;
                font-weight: bold;
                color: #718096;
                border-bottom: 1px solid #e5e7eb;
            }

            .tm-code-copy {
                cursor: pointer;
                transition: color 0.2s;
                color: #3498db;
            }
            .tm-code-copy:hover { color: #2980b9; }
            .tm-code-copy.success { color: #2ecc71; }

            .tm-intro pre code.hljs { background: transparent; padding: 0; }

            /* 行内代码 */
            .tm-intro :not(pre) > code {
                padding: 2px 5px;
                background: #fff5f5;
                color: #c0392b;
                border-radius: 4px;
                font-family: monospace;
            }
        `;
        document.head.appendChild(style);
    }

    const observer = new MutationObserver(insertIntro);
    observer.observe(document.getElementById('app'), { childList: true, subtree: true });
    setTimeout(insertIntro, 2000);
})();

效果如下:

栏目框会比正常的白框颜色稍深。亲测可与主流 Stylus 样式如 Smart-Luogu 兼容。


本文在编写过程中使用了 GenAI 工具,主要用于:

  • 编写代码

本人参与了 设计、测试

原文
抓取于
  1. yajiyi114514

    @litjohn 原理很简单,个人主页的数据是发到了网页前端的,但是没有渲染出来,这个脚本只是帮它渲染出来的

  2. litjohn

    这是什么原理啊

  3. zhouhaoGG

    @Anthony2011sfh 真的可以使用,'ReferenceError: marked is not defined'是因为要到网站里才有这个标识符

  4. Anthony2011sfh

    为什么我这里会有ReferenceError: marked is not defined

  5. Anthony2011sfh

    这个真的能使用吗……