Non-Printable Character Detection

Replace non-printable characters, e.g., zero-width spaces, with a visible symbol.

  1. // ==UserScript==
  2. // @name Non-Printable Character Detection
  3. // @namespace https://greasyfork.org/users/193469
  4. // @description Replace non-printable characters, e.g., zero-width spaces, with a visible symbol.
  5. // @version 1.2
  6. // @author Rui LIU (@liurui39660)
  7. // @match *://*/*
  8. // @icon https://icons.duckduckgo.com/ip2/example.com.ico
  9. // @license MIT
  10. // @run-at document-end
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. const regex = /\p{Cf}/gu; // https://stackoverflow.com/a/12054775/8056404
  17. const to = '\u2756'; // ❖
  18.  
  19. const replace = root => new Promise(() => {
  20. const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
  21. let node;
  22. while (node = walker.nextNode())
  23. node.nodeValue = node.nodeValue.replaceAll(regex, to);
  24. })
  25.  
  26. new MutationObserver(mutations => mutations.forEach(mutation => mutation.addedNodes.forEach(node => replace(node)))).observe(document.body, {subtree: true, childList: true});
  27.  
  28. replace(document.body);
  29. })();