// ==UserScript== // @name Codemao Markdown // @namespace codemao // @version 0.2 // @description 在编程猫社区使用Markdown语法发布帖子 // @author 简单得不简单 // @require https://greasyfork.org/scripts/425827-editormd-js/code/editormdjs.js // @require https://greasyfork.org/scripts/34138-marked-js/code/markedjs.js // @require https://greasyfork.org/scripts/425847-prettify-js/code/prettifyjs.js // @resource editormd https://jddbjd.gitee.io/editor.md/css/editormd.preview.css // @grant GM_getResourceText // @grant GM_addStyle // @include *://shequ.codemao.cn/community // @include *://shequ.codemao.cn/community/* // @downloadURL none // ==/UserScript== (function () { let editormdcss = GM_getResourceText('editormd') GM_addStyle(editormdcss) // 原理:提交时,将 Markdown 放在
 元素中;查看时,读取 
 元素的内容,并渲染

    // 粘贴 Markdown 文本
    var pasteText = function () {
        // 贴子发布表单
        var forumContainer = document.getElementsByClassName('r-community-c-forum_sender--container')[0];
        // 帖子编辑 iframe
        var iframeDoc = document.getElementById('react-tinymce-0_ifr').contentDocument;
        iframeDoc.body.innerHTML = '';

        // 粘贴 Markdown 文本 按钮
        var btnPasteText = document.createElement('button');
        btnPasteText.innerText = '粘贴Markdown代码';
        btnPasteText.style = 'display: block; text-align: center; width: 100%; padding: 5px 0; margin: 5px 0; background: #fec433; border-radius: 4px; color: #fff;';
        btnPasteText.onclick = function () {
            navigator.clipboard.readText().then(
                // 通过 
 元素保证内容不被编程猫改为HTML
                clipText => iframeDoc.body.innerHTML = '

本文使用由社区用户简单得不简单(https://shequ.codemao.cn/user/2776410)提供的油猴脚本:Codemao Markdown,由于你没有安装该脚本,无法查看帖子内容

' + clipText + '
'); // 对标题添加“[m]”,用于在帖子页面识别是否为 Markdown 文档 var forumTitle = document.getElementsByClassName('r-community-c-forum_sender--title_input')[0]; if (!forumTitle.value.endsWith('[m]')) { forumTitle.value = forumTitle.value + '[m]'; } } // 将按钮添加到表单底部 forumContainer.appendChild(btnPasteText); // 提示文本 var textExplain = document.createElement('a'); textExplain.style = 'display: block; text-align: center; width: 100%; padding: 5px 0; margin: 5px 0; background: #fec433; border-radius: 4px; color: #fff;'; textExplain.innerText = '点击进入编辑器'; textExplain.href = 'https://jddbjd.gitee.io/editor.md/'; textExplain.target = '__blank'; textExplain.title = '点击进入编辑器'; // 将文本添加到表单底部 forumContainer.appendChild(textExplain); } // 修改帖子页面的 HTML 为 Markdown 渲染后的 HTML var modify = function () { // 获取帖子标题,以确判断是否为 Markdown 帖子 var forumTitle = document.getElementsByClassName('r-community-r-detail--forum_title')[0]; if (forumTitle.innerText.endsWith('[m]')) { // 获取帖子内容 var forumContent = document.getElementsByClassName('r-community-r-detail--forum_content')[0]; // 提取
 中的文本
            var forumText = forumContent.getElementsByTagName('pre')[0].innerText;
            // 修改其 HTML 为 Markdown
            forumContent.innerHTML = '';
            forumContent.className = 'markdown-body editormd-html-preview';
            forumContent.id = 'markdown';

            $(function () {
                // 渲染 Markdown
                var editor = editormd.markdownToHTML("markdown", {
                    htmlDecode: true,
                    taskList: true,
                    tex: true,
                    flowChart: true,
                    sequenceDiagram: true,
                });
            });
        }
    }


    // 程序入口
    var forum = function () {
        var path = window.location.pathname.split('/');
        // 判断当前页是论坛首页还是帖子页
        if (path.length == 3) {
            // 帖子页
            modify();
        }
        else {
            // 论坛首页
            pasteText();
        }
    }


    // 设置延时,在文档加载完后渲染,否则不会渲染成功
    setTimeout(forum, 1000);
})();