跳到正文

科技·工程

洛谷用户介绍显示插件

#SUSPENDED#约 7 分钟
原文

这是 2025年10月18日 存档的第 3 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月27日
篇幅
约 7 分钟

版本

  1. v7当前
  2. v6
  3. v5
  4. v4
更早的 3 个版本
  1. v3
  2. v2
  3. v1

如果你在保存站,请点击更新内容以查看最新版本。

功能

显示个人介绍。

版本

0.1.1

2025.10.182025.10.18

显示个人介绍 Markdown 源码。

0.1.2

2025.10.182025.10.18

更改了背景颜色、行间距。

源码
代码
// ==UserScript==
// @name         洛谷个人介绍显示插件
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @description  显示洛谷个人介绍
// @author       liuyuhan1522
// @match        https://www.luogu.com.cn/user/*
// @grant        GM_xmlhttpRequest
// @connect      luogu.com.cn
// ==/UserScript==

(function() {
    'use strict';
    let nowurl = window.location.href;
    let nowurluid='';
    for(let i=0;i<nowurl.length;i++){
        if(nowurl[i]>='0'&&nowurl[i]<='9') nowurluid+=nowurl[i];
    }
    // 配置参数
    const CONFIG = {
        targetSelector: '#app > div.main-container.lside-nav > main > div > div.sidebar-container.reverse > div.main',
        apiUrl: 'https://www.luogu.com.cn/api/user/info/' + nowurluid,
        maxRetries: 5,
        retryInterval: 1000,
        divStyle: {
            padding: '15px',
            margin: '10px 0',
            background: '#ffffff',
            borderRadius: '5px',
            border: '1px solid #e1e4e8',
            fontSize: '14px',
            lineHeight: '2.0',
            color: '#24292e'
        }
    };

    let retryCount = 0;

    // 创建介绍div元素
    function createIntroDiv(content) {
        const div = document.createElement('div');
        div.className = 'luogu-user-intro';

        // 应用样式
        Object.assign(div.style, CONFIG.divStyle);

        // 处理内容格式
        const formattedContent = content
            .replace(/\n/g, '<br>')
            .replace(/!$(.*?)$$(.*?)$/g, '<div class="media-container"><span>$1</span></div>');

        div.innerHTML = formattedContent;
        return div;
    }

    // 获取用户数据
    function fetchUserData() {
        GM_xmlhttpRequest({
            method: 'GET',
            url: CONFIG.apiUrl,
            headers: {
                'Accept': 'application/json',
                'X-Requested-With': 'XMLHttpRequest'
            },
            onload: function(response) {
                try {
                    const data = JSON.parse(response.responseText);
                    if (data?.user?.introduction) {
                        injectIntroDiv(data.user.introduction);
                    } else {
                        console.warn('API响应中未找到用户介绍内容');
                    }
                } catch (e) {
                    console.error('解析用户数据失败:', e);
                    retryOrAbort();
                }
            },
            onerror: function(error) {
                console.error('获取用户数据失败:', error);
                retryOrAbort();
            }
        });
    }

    // 重试或放弃
    function retryOrAbort() {
        if (retryCount < CONFIG.maxRetries) {
            retryCount++;
            setTimeout(fetchUserData, CONFIG.retryInterval);
        }
    }

    // 注入介绍div
    function injectIntroDiv(content) {
        const targetElement = document.querySelector(CONFIG.targetSelector);

        if (!targetElement) {
            if (retryCount < CONFIG.maxRetries) {
                retryCount++;
                setTimeout(() => injectIntroDiv(content), CONFIG.retryInterval);
            }
            return;
        }

        // 检查是否已存在
        if (targetElement.querySelector('.luogu-user-intro')) {
            return;
        }

        targetElement.prepend(createIntroDiv(content));
        console.log('用户介绍内容已成功注入');
    }

    // 初始化
    function init() {
        if (document.readyState === 'complete') {
            fetchUserData();
        } else {
            document.addEventListener('DOMContentLoaded', fetchUserData);
            window.addEventListener('load', fetchUserData);
        }
    }

    init();
})();