Old Reddit Better Codeblocks

re-render markdown with marked.js and highlight codeblocks with highlight.js

当前为 2023-11-22 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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();