|
| 1 | +export default { |
| 2 | + icon: "https://lh3.googleusercontent.com/6i_qAB7dGRV44NxLp_JzemPr0ZoHp1e2JT0QxHbAteVJwsLQ-1HLaxmcUJ19F-wKITawVukn8ZbLri2FZamcxgBB=w128-h128-e365-rj-sc0x00ffffff", |
| 3 | + name: { |
| 4 | + en: "Detect Zero-Width Characters", |
| 5 | + vi: "Phát hiện ký tự ẩn (Zero-Width)", |
| 6 | + }, |
| 7 | + description: { |
| 8 | + en: "Detects zero-width characters, highlights the characters and containing DOM element.\n\nClick for more detail.", |
| 9 | + vi: "Phát hiện ký tự ẩn (zero-width) trong văn bản cho trình duyệt, e-mail client, trình soạn thảo văn bản,...\n\nBấm để xem thêm chi tiết.", |
| 10 | + }, |
| 11 | + |
| 12 | + // Code extracted from https://chrome.google.com/webstore/detail/detect-zero-width-charact/icibkhaehdofmcbfjfpppogioidkilib |
| 13 | + onDocumentIdle: () => { |
| 14 | + (function () { |
| 15 | + var unicodeCode; |
| 16 | + const zeroWidthCharacterCodes = [8203, 8204, 8205, 8288]; |
| 17 | + |
| 18 | + let elementsWithZWCC = []; |
| 19 | + const highlightCharacters = function (element) { |
| 20 | + const zeroWidthCharacters = String.fromCodePoint( |
| 21 | + ...zeroWidthCharacterCodes |
| 22 | + ); |
| 23 | + const regExp = new RegExp(`([${zeroWidthCharacters}])`, "g"); |
| 24 | + element.innerHTML = element.innerHTML.replace( |
| 25 | + regExp, |
| 26 | + '$1<span class="zero-width-character"></span>' |
| 27 | + ); |
| 28 | + }; |
| 29 | + // From: https://jsfiddle.net/tim333/np874wae/13/ |
| 30 | + const checkElement = function (element) { |
| 31 | + const text = textWithoutChildren(element); |
| 32 | + [...text].forEach(function (character) { |
| 33 | + unicodeCode = character.codePointAt(0); |
| 34 | + if ( |
| 35 | + zeroWidthCharacterCodes.includes(unicodeCode) && |
| 36 | + !elementsWithZWCC.includes(element) |
| 37 | + ) { |
| 38 | + elementsWithZWCC.push(element); |
| 39 | + } |
| 40 | + }); |
| 41 | + }; |
| 42 | + // From: https://stackoverflow.com/a/9340862/535363 |
| 43 | + const textWithoutChildren = function (element) { |
| 44 | + let child = element.firstChild, |
| 45 | + texts = []; |
| 46 | + while (child) { |
| 47 | + if (child.nodeType == 3) { |
| 48 | + texts.push(child.data); |
| 49 | + } |
| 50 | + child = child.nextSibling; |
| 51 | + } |
| 52 | + return texts.join(""); |
| 53 | + }; |
| 54 | + const checkPage = function () { |
| 55 | + const allElements = document.getElementsByTagName("*"); |
| 56 | + [...allElements].forEach(checkElement); |
| 57 | + elementsWithZWCC.forEach(function (element) { |
| 58 | + element.classList.add("zero-width-characters"); |
| 59 | + highlightCharacters(element); |
| 60 | + }); |
| 61 | + }; |
| 62 | + |
| 63 | + var readyStateCheckInterval = setInterval(function () { |
| 64 | + if (document.readyState === "complete") { |
| 65 | + clearInterval(readyStateCheckInterval); |
| 66 | + // Check Page |
| 67 | + checkPage(); |
| 68 | + // Check page again when any input field is changed |
| 69 | + const inputs = document.querySelectorAll("input"); |
| 70 | + [...inputs].forEach(function (input) { |
| 71 | + input.addEventListener("change", checkPage); |
| 72 | + }); |
| 73 | + } |
| 74 | + }, 10); |
| 75 | + })(); |
| 76 | + |
| 77 | + UsefulScriptGlobalWebpageContext.DOM.injectCssCode(` |
| 78 | + .zero-width-characters { |
| 79 | + background-color: rgba(255, 0, 0, 0.2) !important; |
| 80 | + position: relative; |
| 81 | + } |
| 82 | + .zero-width-characters:after { |
| 83 | + background: #ffb2b2; |
| 84 | + bottom: -14px; |
| 85 | + color: #000; |
| 86 | + content: 'Warning: Contains zero-width characters.'; |
| 87 | + float: right; |
| 88 | + font-family: monospace; |
| 89 | + font-size: 10px; |
| 90 | + font-weight: bold; |
| 91 | + line-height: 1; |
| 92 | + padding: 2px 4px; |
| 93 | + position: absolute; |
| 94 | + right: 0; |
| 95 | + } |
| 96 | + .zero-width-character:after { |
| 97 | + color: red; |
| 98 | + content: '\u25CF'; |
| 99 | + }`); |
| 100 | + }, |
| 101 | + |
| 102 | + onClick: () => { |
| 103 | + prompt( |
| 104 | + "Đọc bài viết bên dưới để hiểu rõ hơn chức năng này", |
| 105 | + "https://viblo.asia/p/ky-tu-zero-width-sat-thu-vo-hinh-nam-giua-doan-van-ban-thuan-vo-hai-L4x5xM7qKBM" |
| 106 | + ); |
| 107 | + }, |
| 108 | +}; |
0 commit comments