Zendesk user ticket markdown

Renders markdown in user sent tickets

目前為 2023-09-28 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Zendesk user ticket markdown
// @name:de      Zendesk Kundenticket Markdown
// @namespace    https://github.com/pke/zendesk-markdown
// @version      1.3
// @license      MIT
// @description  Renders markdown in user sent tickets
// @description:de Stellt markdown in Kundentickets dar
// @author       Philipp Kursawe <[email protected]>
// @match        https://*.zendesk.com/agent/tickets/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=zendesk.com
// @run-at       document-end
// @require      https://cdn.jsdelivr.net/npm/[email protected]/marked.min.js#sha256-kFpV+TsMjV61DFL9NPLjLbXsG8KAH88oX1pf7xjkTQY=
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/purify.min.js#sha256-QigBQMy2be3IqJD2ezKJUJ5gycSmyYlRHj2VGBuITpU=
// @supportURL   https://github.com/pke/zendesk-markdown/discussions
// @grant        GM_log
// ==/UserScript==

/* global marked, DOMPurify */

(function() {
    'use strict';

    function onChange(mutations, observer) {
        for (const mutation of mutations) {
            if (mutation.addedNodes.length) {
                for (const commentNode of document.getElementsByClassName("zd-comment")) {
                    // Already processed nodes are marked for not processing them again
                    if (!commentNode.markdowned) {
                        // GM_log("Added node:", commentNode.textContent);
                        // Grab the elements textContent which preserves indentions made
                        // by "  " constructs but strips all markup code.
                        const unmarked = commentNode.textContent;
                        const marked = marked.parse(unmarked);
                        const clean = DOMPurify.sanitize(marked);
                        if (typeof commentNode.setHTML === "function") {
                            commentNode.setHTML(clean);
                        } else {
                            commentNode.innerHTML = clean;
                        }
                        commentNode.markdowned = true;
                    }
                }
            }
        }
    }

    // Wait for .zd-comment nodes to be created and convert their textContent to markdown, then disconnect
    var observer = new MutationObserver(onChange);
    observer.observe(document.body, { subtree: true, childList: true });
})();