跳到正文

科技·工程

解决犇犇回复时丢失 Markdown 的问题

a_small_OIer约 9 分钟
原文

这是 2026年5月9日 存档的第 3 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年5月9日
收录
2026年5月9日
更新
2026年11月9日
篇幅
约 9 分钟

版本

  1. v7当前
  2. v6
  3. v5
  4. v4
更早的 3 个版本
  1. v3
  2. v2
  3. v1

众所周知,在回复犇犇的时候会丢失已有的 Markdown 格式, 于是写了一个脚本修复 Markdown 格式。

具体实现:在回复犇犇的时候自动在回复框中识别回复的内容,然后用正确的 Markdown 格式覆盖错误的文本。

修复了除 #、 >、折叠框、表格等以外常见的 Markdown 格式,比如删除线和加粗等。

updupd
  • 1.2.81.2.8 版(2026.5.92026.5.9):首个发布版,DeepSeeK 还是太好用了你们知道吗。

  • 2.1.12.1.1 版(2026.5.92026.5.9):于当日下午发现恶行 bug,感谢同学@Sakument_tree 的帮助,修复了 bug,同时优化了正则逻辑,修复了 @ 符后的链接渲染问题。

代码(2.1.12.1.1 版)
JavaScript
// ==UserScript==
// @name         benben-better
// @namespace    http://tampermonkey.net/
// @version      2.1.1
// @description  解决犇犇回复时丢失 LaTeX 和 Markdown 格式的问题
// @author       a_small_OIer
// @match        www.luogu.com.cn
// @grant        none
// ==/UserScript==
(function(){
    'use strict';
    const REPLY_TEXT_MATCH = /回复|reply/i;
    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 += '![' + alt + '](' + src + ')';
                } else if (tag === 'a'){
                    const text = n.textContent;
                    const href = n.getAttribute('href') || '';
                    if (text.trim().startsWith('@') || /\/user\//.test(href)) {
                        result += text;
                    }else{
                        result += '[' + 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]);
                    }
                }
            }
        }
        return result.trim();
    }
    document.addEventListener('click', function(event) {
        const target = event.target;
        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"]');
        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 mdContent = generateMarkdown(contentEl);
        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 prefixMatch = oldVal.match(/^(.+?[::])/);
            const prefix = prefixMatch ? prefixMatch[1] : '';
            textarea.value = prefix + mdContent;
            textarea.focus();
        }
        setTimeout(fillSource, 200);
    });
})();
一句闲话

这份代码同时支持修复 LaTeX\LaTeX 文本。