众所周知,在回复犇犇的时候会丢失已有的 Markdown 格式, 于是写了一个脚本修复 Markdown 格式。
具体实现:在回复犇犇的时候自动在回复框中识别回复的内容,然后用正确的 Markdown 格式覆盖错误的文本,流程如下:
修复了除 #、>、折叠框、表格等以外常见的 Markdown 格式,比如删除线、加粗以及链接、图床等。
出现问题欢迎反馈。
-
版():首个发布版,
DeepSeeK 还是太好用了你们知道吗。 -
版():于当日下午发现恶性 bug,感谢同学@Sakument_tree 的帮助,修复了 bug,同时优化了正则逻辑,修复了
@符后的链接渲染问题。 -
版():感谢@xcb__fhuHGFgf56454 @Sakument_tree 反馈的 bug,优化了正则表达式,使其更加适配洛谷前端,修复了部分问题:
- 在回复中包含链接时,不再使用
[]()语法; - 优化了回复格式,使其不再丢失原有回复格式中的空格;
- 优化了“回复”按钮的匹配逻辑;
- 现在图床
![]()格式可以正常包含链接了。
- 在回复中包含链接时,不再使用
代码( 版)
// ==UserScript==
// @name benben-better
// @namespace http://tampermonkey.net/
// @version 3.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 += '';
} 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 {
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 (!target.closest('a[name="feed-reply"]')) return;
if (!REPLY_TEXT_MATCH.test(target.innerText.trim())) return;
let commentItem = target.closest('.am-comment-main');
if (!commentItem) 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 prefixMatch = oldVal.match(/^(.+?[:] ?)/);
const prefix = prefixMatch ? prefixMatch[1] : '';
textarea.value = prefix + mdContent;
textarea.focus();
}
setTimeout(fillSource, 200);
});
})();