版本比较
解决犇犇回复时丢失 Markdown 的问题
正文
增加 118 行删除 108 行
1234456678891011121314151617181920212223242526272829303132333435131436373839404142434445464748495051525354555657585960616263646566672068697023242526272829303132333435363738394041424344717273747576777879808182838485868788899091929394959697989910010110210310410510610710810946474849505152531101111121131141151161171181191201215512257585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105123124108125126127128129130113114131132116117118119120121122123124125126127133134135136137138139140141142143144145135136137138139146147148149150141151152153154155156157148158159众所周知,在回复犇犇的时候会丢失已有的 Markdown 格式,于是写了一个脚本修复 Markdown 格式。
具体实现:在回复犇犇的时候自动在回复框中识别回复的内容,然后用正确的 Markdown 格式覆盖错误的文本。具体实现:在回复犇犇的时候自动在回复框中识别回复的内容,然后用正确的 Markdown 格式覆盖错误的文本,流程如下:
支持修复除 `#` 以外常见的 Markdown 格式,比如~~删除线~~和**加粗**。
::::info[代码(~~DeepSeek 还是太好用了你们知道吗~~)]修复了除 `#`、`>`、折叠框、表格等以外常见的 Markdown 格式,比如~~删除线~~、**加粗**以及链接、图床等。
出现问题欢迎反馈。
**现在你可以在 [github](https://github.com/a-small-OIer/benben-better/) 上发现这个项目了**。
::::info[$upd$]- $1.2.8$ 版($2026.5.9$):首个发布版,~~DeepSeeK 还是太好用了你们知道吗~~。
- $2.1.1$ 版($2026.5.9$):于当日下午发现恶性 bug,感谢同学@[Sakument_tree](https://www.luogu.com.cn/user/786914) 的帮助,修复了 bug,同时优化了正则逻辑,修复了 `@` 符后的链接渲染问题。
- $3.1.1$ 版($2026.5.11$):感谢@[xcb__fhuHGFgf56454](https://www.luogu.com.cn/user/1971739) @[Sakument_tree](https://www.luogu.com.cn/user/786914) 反馈的 bug,优化了正则表达式,使其更加适配洛谷前端,修复了部分问题:
- 在回复中包含链接时,不再使用 `[]()` 语法; - 优化了回复格式,使其不再丢失原有回复格式中的空格; - 优化了“回复”按钮的匹配逻辑;- $3.1.2$ 版($2026.5.14$)
感谢@[limaotong](https://www.luogu.com.cn/user/1417265) 反馈的 bug,现在可以正常使用 `[![]()]()` 语法了;同时感谢@[_zyx2012 ](https://www.luogu.com.cn/user/1934210) 的提醒,补充了 MIT 开源协议声明。
现在你可以在 [github](https://github.com/a-small-OIer/benben-better/) 上下载脚本了。::::::::info[代码($3.1.2$ 版)]```javascript// ==UserScript==// @name benben-better// @namespace http://tampermonkey.net/// @version 1.9// @description 成功解决犇犇回复时丢失 LaTeX 和 Markdown 的bug// @version 3.1.2// @description 解决犇犇回复时丢失 LaTeX 和 Markdown 格式的问题// @author a_small_OIer// @match www.luogu.com.cn// @grant none// @license MIT// ==/UserScript==
/* * MIT License * * Copyright (c) 2026 a_small_OIer * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */
(function() {(function(){ 'use strict'; const REPLY_TEXT_MATCH = /回复|reply/i; function extractReplacements(container) { const list = []; const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, { acceptNode: function(node) { if (node.classList && node.classList.contains('katex')) return NodeFilter.FILTER_ACCEPT; if (node.tagName === 'SCRIPT' && node.type && /^math\/tex/.test(node.type)) return NodeFilter.FILTER_ACCEPT; if (['STRONG','B','EM','I','DEL','S','CODE','A','IMG'].includes(node.tagName)) return NodeFilter.FILTER_ACCEPT; return NodeFilter.FILTER_SKIP; } });
while (walker.nextNode()) { const node = walker.currentNode; const tag = node.tagName.toLowerCase(); try { //图床 if (tag === 'img') { const alt = (node.getAttribute('alt') || '').trim(); const src = node.getAttribute('src') || ''; if (src) { const replacement = ``; list.push(alt ? { text: alt, replacement } : { text: '', replacement, type: 'img' }); function generateMarkdown(node) { let result = ''; const stack = [node]; while (stack.length) { const n = stack.shift(); if (n.nodeType === Node.TEXT_NODE) { result += n.textContent; } else if (n.nodeType === Node.ELEMENT_NODE) { const tag = n.tagName.toLowerCase(); if (n.classList && n.classList.contains('katex')) { const ann = n.querySelector('annotation[encoding="application/x-tex"]'); const raw = ann ? ann.textContent.trim() : ''; result += raw ? '$' + raw + '$' : n.textContent; } else if (tag === 'script' && n.type && /^math\/tex/.test(n.type)) { const raw = n.textContent.trim(); result += (n.type.includes('block') || n.type.includes('display')) ? '$$\n' + raw + '\n$$' : '$' + raw + '$'; } else if (tag === 'img') { const alt = n.getAttribute('alt') || ''; const src = n.getAttribute('src') || ''; if (src) result += ''; } else if (tag === 'a'){ const text = n.textContent; const href = n.getAttribute('href') || ''; const isMention = text.trim().startsWith('@') || /\/user\//.test(href); const isBareURL = /^(https?:\/\/|www\.)/i.test(text.trim()) && !/\s/.test(text.trim()); if (isMention || isBareURL) { result += text; }else{ let inner = ''; for (let i = 0; i < n.childNodes.length; i++) { inner += generateMarkdown(n.childNodes[i], false); } if (inner === '') { inner = text; } result += '[' + inner + '](' + href + ')'; } continue; } //链接 if (tag === 'a') { const text = node.textContent.trim(); const href = node.getAttribute('href') || ''; if (text && href) { list.push({ text, replacement: `[${text}](${href})` }); } else if (tag === 'strong' || tag === 'b') { result += '**' + n.textContent + '**'; } else if (tag === 'em' || tag === 'i') { result += '*' + n.textContent + '*'; } else if (tag === 'del' || tag === 's') { result += '~~' + n.textContent + '~~'; } else if (tag === 'code') { result += '`' + n.textContent + '`'; } else { for (let i = n.childNodes.length - 1; i >= 0; i--) { stack.unshift(n.childNodes[i]); } continue; } //KaTeX if (node.classList && node.classList.contains('katex')) { const ann = node.querySelector('annotation[encoding="application/x-tex"]'); const raw = ann ? ann.textContent.trim() : null; const rendered = node.textContent.trim(); if (raw && rendered) { list.push({ text: rendered, replacement: `$${raw}$` }); } continue; } //MathJax <script> if (tag === 'script' && /^math\/tex/.test(node.type)) { const raw = node.textContent.trim(); if (raw) { const replacement = node.type.includes('display') ? `$$\n${raw}\n$$` : `$${raw}$`; list.push({ text: raw, replacement }); } continue; } const rendered = node.textContent.trim(); if (!rendered) continue; const cmap = { strong: `**${rendered}**`, b: `**${rendered}**`, em: `*${rendered}*`, i: `*${rendered}*`, del: `~~${rendered}~~`, s: `~~${rendered}~~`, code: `\`${rendered}\`` }; if (cmap[tag]) { list.push({ text: rendered, replacement: cmap[tag] }); } } catch (e) {} } return list; } function applyReplacements(text, list) { const normal = list.filter(item => item.text && item.type !== 'img'); const imageless = list.filter(item => item.type === 'img' || !item.text); const sorted = normal.sort((a, b) => b.text.length - a.text.length); let result = text; for (const { text: search, replacement } of sorted) { const escaped = search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); result = result.replace(new RegExp(escaped, 'g'), replacement); } for (const item of imageless) { if (item.replacement && !result.includes(item.replacement)) { result += '\n' + item.replacement; } } return result; return result.trim(); } document.addEventListener('click', function(event) { const target = event.target; if (!target.closest('a[name="feed-reply"]')) return; if (!REPLY_TEXT_MATCH.test(target.innerText.trim())) return;
const commentItem = target.closest('.feed-item, .am-comment, li[class*="comment"], .comment-item, [class*="feed-item"]'); let commentItem = target.closest('.am-comment-main'); if (!commentItem) return;
let contentEl = null; const selectors = ['.am-comment-bd', '.markdown-body', '.feed-content', '.comment-content', '[class*="content"]']; for (const sel of selectors) { contentEl = commentItem.querySelector(sel); if (contentEl) break; } if (!contentEl) contentEl = commentItem;
const replacements = extractReplacements(contentEl); if (replacements.length === 0) return;
commentItem = commentItem.querySelector('.am-comment-bd'); if (!commentItem) return; commentItem = commentItem.querySelector('.feed-comment'); if (!commentItem) return; const mdContent = generateMarkdown(commentItem); if (!mdContent) return; function fillSource() { const textarea = document.querySelector('.am-form-group.am-form textarea, textarea'); if (!textarea) { setTimeout(fillSource, 100); return; } const oldVal = textarea.value; const newVal = applyReplacements(oldVal, replacements); if (newVal !== oldVal) { textarea.value = newVal; textarea.focus(); } const prefixMatch = oldVal.match(/^(.+?[:] ?)/); const prefix = prefixMatch ? prefixMatch[1] : ''; textarea.value = prefix + mdContent; textarea.focus(); }
setTimeout(fillSource, 200); });})();
```::::::::info[一句闲话]这份代码同时支持修复 [$\LaTeX$](https://www.luogu.com.cn/article/x9dpta33) 文本。这个脚本同时支持修复 [$\LaTeX$](https://www.luogu.com.cn/article/x9dpta33) 文本。::::