Old Reddit Better Codeblocks

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

目前為 2023-11-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();