re-render markdown with marked.js and highlight codeblocks with highlight.js
当前为
// ==UserScript== // @name Old Reddit Better Codeblocks // @namespace http://tampermonkey.net/ // @version 0.2 // @license MIT // @description re-render markdown with marked.js and highlight codeblocks with highlight.js // @author cultab // @match http*://*.reddit.com/* // @exclude http*://new.reddit.com/* // @icon https://www.google.com/s2/favicons?sz=64&domain=reddit.com // @grant GM_addStyle // @grant GM_getResourceText // @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@2 // @require https://cdn.jsdelivr.net/npm/[email protected]/lib/marked.umd.min.js // @require https://cdn.jsdelivr.net/npm/[email protected]/dist/purify.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js // @resource hljs https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css // ==/UserScript== function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } const stoyle = GM_getResourceText("hljs"); let ran = false; const disconnect = VM.observe(document.body, () => { if (ran) { return; } ran = true; let open = true; const comments = document.getElementsByClassName('usertext-body'); for (let i = 0; i < comments.length; i++) { let comment = comments[i]; let g = comment.parentNode.parentNode; if (!g.classList.contains("entry")) { g = g.parentNode; } console.log(g); let btn = g.querySelector(".viewSource"); console.log(btn); if (btn == null) { continue; } btn.children[0].click(); // yes twice btn.children[0].click(); sleep(500).then(() => { let source = g.querySelector("textarea").innerHTML; //console.log(source); comment.children[0].innerHTML = DOMPurify.sanitize(marked.parse(source)); console.log("done?", comment); }) //marked.parse( } sleep(500).then(() => { GM_addStyle(stoyle); hljs.highlightAll(); }) }); /*if (i !== 1) { continue; } let md = comments[i].children[0]; console.log(md); for (let j = 0; j < md.children.length; j++) { let line = md.children[j]; if (line.children[0]) { unwrap(line.children[0]); changeType(line, "p"); open = false; } let before = line.innerHTML; let after = ""; console.log("try"); if (open) { after = before.replace("```", "<code>"); } else { after = before.replace("```", "</code>"); } if (before !== after) { console.log("yes!"); open = !open; } //console.log(line); } */ // You can also disconnect the observer explicitly when it's not used any more //disconnect();