版本比较
洛谷用户介绍显示插件
正文
增加 20 行删除 129 行
123455678910111213141516171018192021221523242526272829202122232425262728293031303133343536373839404142434445464748495051525354555632333458353660616263373839656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814940如果你在保存站,请点击更新内容以查看最新版本。
## 功能
显示个人介绍。显示个人介绍,让个人介绍从新变得有用。
## 源码<https://github.com/liuyuhan1522/->
## 使用方法详见源码注释,以后会详细介绍。
## 版本### 0.1.1$2025.10.18$ $\texttt{2025.10.18}$
显示个人介绍 Markdown 源码。
### 0.1.2$2025.10.18$$\texttt{2025.10.18}$
更改了背景颜色、行间距。
### 0.2$\texttt{2025.10.19}$
:::info[源码]```// ==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==支持大部分 md 渲染。(虽然有些和 luogu 不太一样。)
(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' } };### 0.3$\texttt{2025.10.19}$
let retryCount = 0;在个人介绍前添加了“个人介绍”四个字,并把一些部分改成了与 luogu 相同的样式。
// 创建介绍div元素 function createIntroDiv(content) { const div = document.createElement('div'); div.className = 'luogu-user-intro';### 0.4$\texttt{2025.10.23}$
// 应用样式 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();})();
```:::可以不显示自己的个人介绍。