版本比较
洛谷用户介绍显示插件
正文
增加 23 行删除 122 行
12343565778891010111213141516171819202122112425262728293031323334353637383940414243444546471213144915515253541617181956572021596061622223246465662526686970717273747576777879808182838485868788899091929394952728299798991001011021033031105106107323334109110111112113114115353611711811912037383912212312412512612712812913013113213313413513613713813940如果你在保存站,请点击更新内容以查看最新版本。
## 功能
显示个人介绍。显示个人介绍,让个人介绍从新变得有用。
---
## 版本### 0.1## 源码<https://github.com/liuyuhan1522/->
显示个人介绍 Markdown 源码。:::info[源码]```// ==UserScript==// @name 洛谷个人介绍显示插件// @namespace http://tampermonkey.net/// @version 0.1// @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: '#f8f9fa', borderRadius: '5px', border: '1px solid #e1e4e8', fontSize: '14px', lineHeight: '1.5', color: '#24292e' } };## 使用方法详见源码注释,以后会详细介绍。
let retryCount = 0;
// 创建介绍div元素 function createIntroDiv(content) { const div = document.createElement('div'); div.className = 'luogu-user-intro';## 版本### 0.1.1$\texttt{2025.10.18}$
// 应用样式 Object.assign(div.style, CONFIG.divStyle);显示个人介绍 Markdown 源码。
// 处理内容格式 const formattedContent = content .replace(/\n/g, '<br>') .replace(/!$(.*?)$$(.*?)$/g, '<div class="media-container"><span>$1</span></div>');### 0.1.2$\texttt{2025.10.18}$
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(); } }); }### 0.2$\texttt{2025.10.19}$
// 重试或放弃 function retryOrAbort() { if (retryCount < CONFIG.maxRetries) { retryCount++; setTimeout(fetchUserData, CONFIG.retryInterval); } }支持大部分 md 渲染。(虽然有些和 luogu 不太一样。)
// 注入介绍div function injectIntroDiv(content) { const targetElement = document.querySelector(CONFIG.targetSelector);### 0.3$\texttt{2025.10.19}$
if (!targetElement) { if (retryCount < CONFIG.maxRetries) { retryCount++; setTimeout(() => injectIntroDiv(content), CONFIG.retryInterval); } return; }在个人介绍前添加了“个人介绍”四个字,并把一些部分改成了与 luogu 相同的样式。
// 检查是否已存在 if (targetElement.querySelector('.luogu-user-intro')) { return; }### 0.4$\texttt{2025.10.23}$
targetElement.prepend(createIntroDiv(content)); console.log('用户介绍内容已成功注入'); }
// 初始化 function init() { if (document.readyState === 'complete') { fetchUserData(); } else { document.addEventListener('DOMContentLoaded', fetchUserData); window.addEventListener('load', fetchUserData); } }
init();})();```:::可以不显示自己的个人介绍。