Non-Printable Character Detection

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

目前為 2021-09-17 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Non-Printable Character Detection
// @namespace    https://greasyfork.org/users/193469
// @description  Replace non-printable characters, e.g., zero-width spaces, with a visible symbol.
// @version      1.2
// @author       Rui LIU (@liurui39660)
// @match        *://*/*
// @icon         https://icons.duckduckgo.com/ip2/example.com.ico
// @license      MIT
// @run-at       document-end
// ==/UserScript==

(function () {
	'use strict';

	const regex = /\p{Cf}/gu; // https://stackoverflow.com/a/12054775/8056404
	const to = '\u2756'; // ❖

	const replace = root => new Promise(() => {
		const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
		let node;
		while (node = walker.nextNode())
			node.nodeValue = node.nodeValue.replaceAll(regex, to);
	})

	new MutationObserver(mutations => mutations.forEach(mutation => mutation.addedNodes.forEach(node => replace(node)))).observe(document.body, {subtree: true, childList: true});

	replace(document.body);
})();