Zendesk user ticket markdown

Renders markdown in user sent tickets

  1. // ==UserScript==
  2. // @name Zendesk user ticket markdown
  3. // @name:de Zendesk Kundenticket Markdown
  4. // @namespace https://github.com/pke/zendesk-markdown
  5. // @version 1.4
  6. // @license MIT
  7. // @description Renders markdown in user sent tickets
  8. // @description:de Stellt markdown in Kundentickets dar
  9. // @author Philipp Kursawe <pke@pke.fyi>
  10. // @match https://*.zendesk.com/agent/tickets/*
  11. // @icon https://www.google.com/s2/favicons?sz=64&domain=zendesk.com
  12. // @run-at document-end
  13. // @require https://cdn.jsdelivr.net/npm/marked@9.0.3/marked.min.js#sha256-kFpV+TsMjV61DFL9NPLjLbXsG8KAH88oX1pf7xjkTQY=
  14. // @require https://cdn.jsdelivr.net/npm/dompurify@3.0.5/dist/purify.min.js#sha256-QigBQMy2be3IqJD2ezKJUJ5gycSmyYlRHj2VGBuITpU=
  15. // @supportURL https://github.com/pke/zendesk-markdown/discussions
  16. // @grant GM_log
  17. // ==/UserScript==
  18.  
  19. /* global marked, DOMPurify */
  20.  
  21. (function() {
  22. 'use strict';
  23.  
  24. // GM_log("Loaded");
  25.  
  26. function onChange(mutations, observer) {
  27. for (const mutation of mutations) {
  28. if (mutation.addedNodes.length) {
  29. for (const commentNode of document.getElementsByClassName("zd-comment")) {
  30. // GM_log("zd-comment found");
  31. // Already processed nodes are marked for not processing them again
  32. if (!commentNode.markdowned) {
  33. // GM_log("Added node:", commentNode.textContent);
  34. // Grab the elements textContent which preserves indentions made
  35. // by " " constructs but strips all markup code.
  36. const unmarkedContent = commentNode.textContent;
  37. try {
  38. const markedContent = marked.parse(unmarkedContent);
  39. // GM_log("Marked:", markedContent);
  40. const cleanContent = DOMPurify.sanitize(markedContent);
  41. // GM_log("Clean:", cleanContent);
  42. if (typeof commentNode.setHTML === "function") {
  43. // GM_log("setHTML: ", cleanContent);
  44. commentNode.setHTML(cleanContent);
  45. } else {
  46. // GM_log("innerHTML: ", cleanContent);
  47. commentNode.innerHTML = cleanContent;
  48. }
  49. } catch (error) {
  50. // GM_log("error: ", error.message);
  51. }
  52. commentNode.markdowned = true;
  53. }
  54. }
  55. }
  56. }
  57. }
  58.  
  59. // Wait for .zd-comment nodes to be created and convert their textContent to markdown, then disconnect
  60. var observer = new MutationObserver(onChange);
  61. observer.observe(document.body, { subtree: true, childList: true });
  62. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址