Skip to content

Commit d467fe2

Browse files
committed
detect zero-width characters
1 parent 8a2c680 commit d467fe2

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

popup/tabs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ const tabs = [
229229
s.studyphim_unlimited,
230230
s.studocu_bypassPreview,
231231
createTitle("--- Unlock function ---", "--- Mở khoá chức năng ---"),
232+
s.detect_zeroWidthCharacters,
232233
s.simpleAllowCopy,
233234
s.reEnableContextMenu,
234235
s.showHiddenFields,

scripts/content-scripts/scripts/ufs_global_webpage_context.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ const UsefulScriptGlobalWebpageContext = {
6464
// return disconnect function
6565
return () => observer.disconnect();
6666
},
67+
68+
injectCssCode(code) {
69+
var css = document.createElement("style");
70+
if ("textContent" in css) css.textContent = code;
71+
else css.innerText = code;
72+
document.head.appendChild(css);
73+
},
6774
},
6875
Facebook: {
6976
decodeArrId(arrId) {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
};

scripts/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ import insta_injectDownloadBtn from "./insta_injectDownloadBtn.js";
159159
import studocu_bypassPreview from "./studocu_bypassPreview.js";
160160
import fb_revealDeletedMessages from "./fb_revealDeletedMessages.js";
161161
import fb_whoIsTyping from "./fb_whoIsTyping.js";
162+
import detect_zeroWidthCharacters from "./detect_zeroWidthCharacters.js";
162163

163164
// inject badges
164165
const allScripts = {
@@ -339,6 +340,7 @@ const allScripts = {
339340
studocu_bypassPreview: addBadge(studocu_bypassPreview, BADGES.new),
340341
fb_revealDeletedMessages: addBadge(fb_revealDeletedMessages, BADGES.hot),
341342
fb_whoIsTyping: addBadge(fb_whoIsTyping, BADGES.new),
343+
detect_zeroWidthCharacters: addBadge(detect_zeroWidthCharacters, BADGES.new),
342344
};
343345

344346
// alert(Object.keys(allScripts).length);

0 commit comments

Comments
 (0)