Kanka Preserve HTML Entities in Summernote

Checks PRE and CODE tags in Summernote and ensures that their contents use HTML entities for proper display.

目前为 2023-06-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Kanka Preserve HTML Entities in Summernote
  3. // @namespace http://tampermonkey.net/
  4. // @version 2
  5. // @description Checks PRE and CODE tags in Summernote and ensures that their contents use HTML entities for proper display.
  6. // @author Salvatos
  7. // @license MIT
  8. // @match https://kanka.io/*
  9. // @match https://marketplace.kanka.io/*
  10. // @icon https://www.google.com/s2/favicons?domain=kanka.io
  11. // @run-at document-end
  12. // ==/UserScript==
  13.  
  14. // Wait for Summernote to initialize
  15. $('#entry').on('summernote.init', function() {
  16. // Grab node tree from visual editor
  17. var masterNode = document.querySelector('#entry + .note-editor .note-editable');
  18.  
  19. // Find CODEs and PREs
  20. masterNode.querySelectorAll(":is(pre, code)").forEach((domObject) => {
  21. domObject.replaceChildren(domObject.innerHTML);
  22. // Exclude ' < ' and ' > ' for CSS and comparison operators
  23. domObject.textContent = domObject.textContent.replace(/ &lt; /g, " < ").replace(/ &gt; /g, " > ");
  24. });
  25.  
  26. // Apply changes to master copy in case of immediate save or switch to code view
  27. document.getElementById('entry').value = masterNode.innerHTML;
  28. // BUG: for some reason, this causes changes made in code view to be discarded if you don’t switch back to visual first, so let’s bring back our good old code view save fix...
  29. $('#entry').on('summernote.change.codeview', function(we, contents, $editable) {
  30. $(this).val(contents);
  31. });
  32. });