Non-Printable Character Detection

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

目前为 2021-09-16 提交的版本,查看 最新版本

  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.1
  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 walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
  20. const replace = () => {
  21. let node;
  22. while (node = walker.nextNode()) {
  23. node.nodeValue = node.nodeValue.replaceAll(regex, to);
  24. }
  25. }
  26.  
  27. replace();
  28. new MutationObserver(mutations => {
  29. for (const mutation of mutations) {
  30. for (const node of mutation.addedNodes) {
  31. walker.currentNode = node;
  32. replace();
  33. }
  34. }
  35. }).observe(document.body, {subtree: true, childList: true});
  36. })();