版本比较
在洛谷内无缝访问所有文章
正文
增加 83 行删除 24 行
268 行未改动全部展开269270271272273274275276277278279280281282283284285286287288289290291292293114 行未改动全部展开40840941041139539641241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846911 行未改动481482483484425485486487488489122 行未改动全部展开6126136146155566166176186192 行未改动6226236246255676266276286296304 行未改动63563663763863964064164264364464518 行未改动664665666667606668669670671672673674675676677678679680681144 行未改动全部展开826827828829765766767768769830831832833834835836143 行未改动全部展开980981982983984985986987988126 行未改动全部展开11151116111711181055111911201121112295 行未改动121812191220122111591222122312241225122612271228122911671230123112321233117111721173123412351236123712381239124012411242124311811244124512461247124812491250125112521190125312541255125677 行未改动1334133513361337127613381339134013411342134313441284134513461347134813491350135113521353129313541355129613561357135813591360136113031362136313641365277 行未改动全部展开16431644164516461647164816491650165193 行未改动268 行未改动全部展开 background: rgba(255,255,255,0.05); border-color: #30363d; } } /* 行号边距微调 */ .shiki .line { display: block; padding-left: 3.5em; /* 为行号留出固定空间,原来的 ::before 用了 2em + margin-right 1em,这里再加一点内边距 */ text-indent: -3.5em; /* 让行号向左突出,保持代码对齐 */ } /* 深色模式下普通 pre 的补充 */ pre:not(.shiki) { background-color: #f6f8fa !important; border-color: #d0d7de; } @media (prefers-color-scheme: dark) { pre:not(.shiki) { background-color: #0d1117 !important; border-color: #30363d; } } `;
// ---------- 工具函数 ---------- function getUserIdFromUrl() {114 行未改动全部展开 }
function addShikiCopyButtons(doc) { if (!doc || !doc.body) return; const shikiBlocks = doc.querySelectorAll('pre.shiki'); shikiBlocks.forEach(pre => { const allPres = doc.querySelectorAll('pre'); allPres.forEach(pre => { // 已处理过则跳过 if (pre.dataset.lgsCopyProcessed) return; pre.dataset.lgsCopyProcessed = 'true';
// 如果 pre 中还没有 .line 结构(即非 Shiki 渲染),则为其生成行号结构 if (!pre.querySelector('.line') && !pre.classList.contains('shiki')) { // 获取纯文本(保留换行) const text = pre.textContent || ''; const lines = text.split('\n'); // 清空 pre 内容 pre.innerHTML = ''; lines.forEach((line, i) => { const span = doc.createElement('span'); span.className = 'line'; span.textContent = line; pre.appendChild(span); // 除了最后一行,插入换行节点,模拟原始换行 if (i < lines.length - 1) { pre.appendChild(doc.createTextNode('\n')); } }); // 添加 .shiki 类,复用已写好的行号 CSS(.shiki .line::before) pre.classList.add('shiki'); }
// 避免重复包裹 if (pre.parentElement && pre.parentElement.classList.contains('shiki-wrapper')) return;
// 包裹 wrapper 并添加按钮 const wrapper = doc.createElement('div'); wrapper.className = 'shiki-wrapper'; pre.parentNode.insertBefore(wrapper, pre); wrapper.appendChild(pre);
// 如果 pre 没有 .shiki 类(极少情况),补充基础样式 if (!pre.classList.contains('shiki')) { pre.style.padding = '16px'; pre.style.borderRadius = '8px'; pre.style.border = '1px solid #d0d7de'; pre.style.backgroundColor = '#f6f8fa'; pre.style.overflowX = 'auto'; pre.style.fontFamily = '"SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace'; pre.style.fontSize = '13px'; pre.style.lineHeight = '1.5'; pre.style.margin = '1em 0'; }
const btn = doc.createElement('button'); btn.className = 'shiki-copy-btn'; btn.textContent = '复制'; btn.addEventListener('click', () => { // 复制时只取纯文本,自动忽略行号(伪元素) const code = pre.textContent || ''; navigator.clipboard.writeText(code).then(() => { btn.textContent = '已复制'; setTimeout(() => { btn.textContent = '复制'; }, 2000);11 行未改动 }); }); wrapper.appendChild(btn); }); console.log('[LGSinLG] 代码块复制按钮已添加'); console.log('[LGSinLG] 代码块复制按钮已添加(含行号适配)'); }
// ---------- 个人介绍卡片 ---------- const CARD_ID = 'lgsinlg-intro-card';122 行未改动全部展开 if (isAdminUser()) { removeUserCardIfExists(); return; }
const elements = getOrCreateCard(uid); if (!elements) { // 容器尚未就绪,延迟 300ms 后重试(最多尝试 10 次) if (!fetchAndUpdateCard._retryCount) fetchAndUpdateCard._retryCount = 0; if (fetchAndUpdateCard._retryCount < 10) { fetchAndUpdateCard._retryCount++; setTimeout(() => fetchAndUpdateCard(uid), 300);2 行未改动 fetchAndUpdateCard._retryCount = 0; } return; } fetchAndUpdateCard._retryCount = 0; // 成功时重置计数 fetchAndUpdateCard._retryCount = 0;
elements.contentDiv.innerHTML = '加载中...'; GM_xmlhttpRequest({ method: 'GET',4 行未改动 if (data.code === 200 && data.data) { const intro = data.data.renderedIntroduction || '<p>暂无个人介绍</p>'; elements.contentDiv.innerHTML = intro; constrainImages(elements.contentDiv); // 移除 heading-anchor 锚点 const headingAnchors = elements.contentDiv.querySelectorAll('a.heading-anchor'); headingAnchors.forEach(a => a.remove()); } else { elements.contentDiv.innerHTML = '<p>获取简介失败</p>'; } } catch (e) {18 行未改动 const data = JSON.parse(res.responseText); if (data.code === 200) { GM_xmlhttpRequest({ method: 'GET', url: `https://api.luogu.me/user/${uid}`, url: `https://api.luogu.me/user/query/${uid}`, onload: function(resp) { try { const p = JSON.parse(resp.responseText); if (p.code === 200 && p.data) { contentDiv.innerHTML = p.data.renderedIntroduction || '<p>暂无个人介绍</p>'; constrainImages(contentDiv); // 刷新后同样移除 heading-anchor const headingAnchors = contentDiv.querySelectorAll('a.heading-anchor'); headingAnchors.forEach(a => a.remove()); } else { contentDiv.innerHTML = '<p>获取简介失败</p>'; } } catch (e) {144 行未改动全部展开 if (!markedDiv || !timeCaption || !actionsDiv) return false; markedDiv.innerHTML = pasteData.renderedContent || '<p>暂无内容</p>'; constrainImages(markedDiv); forceExternalLinks(markedDiv); // 移除所有内部锚点链接(href 以 # 开头) const internalAnchors = markedDiv.querySelectorAll('a[href^="#"]'); internalAnchors.forEach(a => { a.replaceWith(document.createTextNode(a.textContent)); }); // 移除 heading-anchor 锚点 const headingAnchors = markedDiv.querySelectorAll('a.heading-anchor'); headingAnchors.forEach(a => a.remove()); injectShikiCSS(doc); const authorCaption = doc.querySelector('.author .lfe-caption.author-margin'); if (authorCaption) { const color = USER_CONFIG.colorMap[pasteData.author.color] || '#000';143 行未改动全部展开 if (titleEl) { titleEl.textContent = articleData.title; if (doc.title) doc.title = articleData.title; } contentDiv.innerHTML = articleData.renderedContent || '<p>暂无内容</p>'; constrainImages(contentDiv); forceExternalLinks(contentDiv); // 移除 heading-anchor 锚点(文章接管中保留原有处理) const headingAnchors = contentDiv.querySelectorAll('a.heading-anchor'); headingAnchors.forEach(a => a.remove()); injectShikiCSS(doc); const headings = contentDiv.querySelectorAll('h1, h2, h3, h4, h5, h6');126 行未改动全部展开 document.getElementById('lgsinlg-loading').style.display = 'none'; iframe.style.display = ''; window.__LGSinLG_active = true; console.log('[LGSinLG] 文章接管完成'); // 评论区接管 setupComments(doc, articleId); } } catch (e) { clearInterval(interval);95 行未改动 const commentContainer = doc.querySelector('.article-comment'); if (!commentContainer || commentContainer.dataset.lgsProcessed) return; commentContainer.dataset.lgsProcessed = 'true';
// ---------- 禁用排序下拉 ---------- // 禁用排序下拉 const sortWrapper = commentContainer.querySelector('.combo-wrapper'); if (sortWrapper) { sortWrapper.style.pointerEvents = 'none'; sortWrapper.style.opacity = '0.5'; sortWrapper.title = '评论区已接管,排序不可用'; }
// ---------- 精确禁用评论输入框(通过 textarea 向上查找父容器) ---------- // 禁用评论输入区域 const textarea = commentContainer.querySelector('textarea'); if (textarea) { const replyForm = textarea.closest('.l-card') || textarea.closest('form') || textarea.closest('.reply-box') || textarea.closest('.comment-editor'); || textarea.closest('form') || textarea.closest('.reply-box') || textarea.closest('.comment-editor'); if (replyForm) { replyForm.style.pointerEvents = 'none'; replyForm.style.opacity = '0.5'; replyForm.title = '评论区已由脚本接管,暂不支持发表'; } }
// 移除原生加载更多(初次尝试) // 尝试移除原生加载更多(可能此时还未渲染) const nativeLoadMoreLink = commentContainer.querySelector('p[style*="text-align: center"] a[href="javascript:void 0"]'); if (nativeLoadMoreLink && nativeLoadMoreLink.parentElement) { nativeLoadMoreLink.parentElement.remove(); }
const rowWrap = commentContainer.querySelector('.row-wrap'); if (!rowWrap) return;
// 初始化函数(需要 articleId) const doInit = (templateRow) => { rowWrap.innerHTML = '';
const countSpan = commentContainer.querySelector('.comment-filter-line span[style*="flex"]');77 行未改动 wrapper.appendChild(loadMoreBtn); commentContainer.appendChild(wrapper); }
// 初始加载 fetchReplies(articleId).then(replies => { appendReplies(replies); }).catch(err => { console.error('[LGSinLG] 初始评论加载失败', err); if (countSpan) countSpan.textContent = '评论加载失败,点击刷新重试'; });
// ----- 持续监听并移除后出现的原生“加载更多”按钮 ----- // 持续监听并移除后出现的原生“加载更多”按钮 const nativeCleaner = new MutationObserver(() => { const nativeLink = commentContainer.querySelector('p[style*="text-align: center"] a[href="javascript:void 0"]'); if (nativeLink && nativeLink.parentElement) { console.log('[LGSinLG] 移除后出现的原生加载更多按钮'); nativeLink.parentElement.remove(); } }); nativeCleaner.observe(commentContainer, { childList: true, subtree: true }); // ------------------------------------------------------- };
// 尝试立即获取模板 const templateRow = rowWrap.querySelector('.row'); if (templateRow) { doInit(templateRow); return; }
// 否则观察 rowWrap,等待懒加载完成(不设超时) const observer = new MutationObserver((mutations, obs) => { const row = rowWrap.querySelector('.row'); if (row) { obs.disconnect();277 行未改动全部展开 console.log('[LGSinLG] 自己的主页,跳过卡片'); removeUserCardIfExists(); return; } // 等待侧边栏容器就绪,否则稍后再试 if (!document.querySelector('.sidebar-container > .main')) { setTimeout(() => processRoute(), 300); return; }93 行未改动