-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
336 lines (302 loc) · 11 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import {
Storage,
CustomFont,
sync,
keys,
reasonUpdateAllText,
reasonToggleOff,
CustomSetting,
injectCustomFonts,
runtime,
reasonInjectCustomFonts,
} from "./shared";
/**
* This Hebrew regex allows and accepts any non Hebrew symbols next to Hebrew symbols,
* this means that it accepts anything as long as it has some Hebrew symbol in it
*/
const hebrewRegex = new RegExp(
"([\u0591-\u05F4\uFB1D-\uFB4F]+([\u0591-\u05F4\uFB1D-\uFB4F\\d]+)*)",
"g"
);
// const hebrewNumbersRegex = new RegExp("([\u0660-\u0669\u06F0-\u06F9]+)", "g");
const htmlEditables = [
"textarea",
"input",
"text",
"email",
"number",
"search",
"tel",
"url",
"password",
];
/** The observer used in {@linkcode startObserver} to dynamically update any newly added Nodes */
let observer: MutationObserver;
/**
* Returns true if the passed in Node has been updated by Behirut and false otherwise
*/
function hasNodeBeenUpdated(node: Node): boolean {
return node.parentElement && node.parentElement.getAttribute("behirut") === "true";
}
/**
* Returns true if this document has already been updated by Behirut before,
* this is done in {@linkcode notifyDocument()}
*/
function hasDocumentBeenUpdated(): boolean {
return document.getElementById("behirutMetaElement") !== null;
}
/**
* Returns whether the given node has any Hebrew script or not, this is any script that matches hebrewRegEx.
* True if it does and false otherwise
*/
function hasHebrewScript(node: Node): boolean {
return !!node.nodeValue && !!node.nodeValue.match(hebrewRegex);
}
/**
* Checks whether the passed in node is editable or not.
* An editable node is one that returns true to isContentEditable or has a tag name as
* any one of the following:
* "textarea", "input", "text", "email", "number", "search", "tel", "url", "password"
*
* @param node the node to check
* @return true if the node is editable and false otherwise
*/
function isNodeEditable(node: Node): boolean {
const element: HTMLElement = node as HTMLElement;
const nodeName: string = element.nodeName.toLowerCase();
return (
element.isContentEditable ||
(element.nodeType === Node.ELEMENT_NODE && htmlEditables.contains(nodeName))
);
}
/**
* Remaps the passed in numberCharacter from Eastern Hebrew numeral form to Western Hebrew numeral form (digit)
* @param numberCharacter the string containing the single number character to remap
* @return the string containing the single number character after remapping
*/
// function remapNumber(numberCharacter: string): string {
// const char = numberCharacter.charAt(0);
// if (char === "٠" || char === "۰") return "0";
// if (char === "١" || char === "۱") return "1";
// if (char === "٢" || char === "۲") return "2";
// if (char === "٣" || char === "۳") return "3";
// if (char === "٤" || char === "۴") return "4";
// if (char === "٥" || char === "۵") return "5";
// if (char === "٦" || char === "۶") return "6";
// if (char === "٧" || char === "۷") return "7";
// if (char === "٨" || char === "۸") return "8";
// if (char === "٩" || char === "۹") return "9";
// }
/**
* Gets all nodes within the passed in node that have any Hebrew text
* @param rootNode the node to use as the root of the traversal
* @return an array of nodes that contain all the nodes with Hebrew text that are children of the passed in
* root node
*/
function getHebrewTextNodesIn(rootNode: Node): Node[] {
let treeWalker: TreeWalker = document.createTreeWalker(rootNode, NodeFilter.SHOW_TEXT);
let hebrewTextNodes: Node[] = [];
let node: Node = treeWalker.nextNode();
while (!!node) {
if (hasHebrewScript(node)) hebrewTextNodes.push(node);
node = treeWalker.nextNode();
}
return hebrewTextNodes;
}
/**
* Updates the passed in node's html to have the properties of a modified Hebrew text node, this will
* replace any text that matches hebrewRegEx to be a span with the font size and line height specified by
* the user's options, the span will have a class='ar', this can be used to check if the text has been
* updated by this function or not
* @param node the node to update
* @param textSize the size to update the text to
* @param lineHeight the height to update the line to
* @param font the name of the font to update the text to
*/
async function updateNode(node: Node, textSize: number, lineHeight: number, font: string) {
let newSize: number = textSize / 100;
let newHeight: number = lineHeight / 100;
if (!!node.nodeValue) {
if (hasNodeBeenUpdated(node)) updateByChanging(node, newSize, newHeight, font);
else if (!hasNodeBeenUpdated(node)) updateByAdding(node, newSize, newHeight, font);
}
}
async function updateByAdding(node: Node, textSize: number, lineHeight: number, font: string) {
const parent: Node = node.parentNode;
// return if parent or node are null
if (!parent || !node) return;
// don't do anything if this node or its parent are editable
if (isNodeEditable(parent) || isNodeEditable(node)) return;
let newHTML: string;
if (font === "Original") {
newHTML =
"<span behirut='true' style='" +
"font-size:" +
textSize +
"em;" +
"line-height:" +
lineHeight +
"em;" +
"'>$&</span>";
} else {
newHTML =
"<span behirut='true' style='" +
"font-size:" +
textSize +
"em;" +
"line-height:" +
lineHeight +
"em;" +
"font-family:" +
'"' +
font +
'";' +
"'>$&</span>";
}
let text: string = node.nodeValue.replace(hebrewRegex, newHTML);
let nextSibling: ChildNode = node.nextSibling;
// the div is temporary and doesn't show up in the html
let newElement: HTMLDivElement = document.createElement("div");
newElement.innerHTML = text;
while (newElement.firstChild) {
// we only insert the passed in html, the div is not inserted
parent.insertBefore(newElement.firstChild, nextSibling);
}
parent.removeChild(node);
}
async function updateByChanging(node: Node, textSize: number, lineHeight: number, font: string) {
node.parentElement.style.fontSize = textSize + "em";
node.parentElement.style.lineHeight = lineHeight + "em";
if (font === "Original") node.parentElement.style.fontFamily = "";
else node.parentElement.style.fontFamily = font;
}
/**
* Updates all Hebrew script nodes in this document's body by calling updateNode() on each node in this document
* with Hebrew script
* @param textSize the size to update the text to
* @param lineHeight the height to update the line to
* @param font the name of the font to update the text to
*/
async function updateAll(textSize: number, lineHeight: number, font: string) {
getHebrewTextNodesIn(document.body).forEach((it: Node) =>
updateNode(it, textSize, lineHeight, font)
);
}
/**
* Starts the observer that will observe for any additions to the document and update them if they have any
* Hebrew text and they have not been updated yet
* @param textSize the size to update the text to
* @param lineHeight the height to update the line to
* @param font the name of the font to update the text to
*/
async function startObserver(textSize: number, lineHeight: number, font: string) {
// If observer was existing then disconnect it and delete it
if (!!observer) {
observer.disconnect();
observer = null;
}
// Only do anything if observer is null
if (!observer) {
const config: MutationObserverInit = {
attributes: false, // we don't care about attribute changes
attributeOldValue: false, // we don't care about attribute changes
characterData: true, // we get notified of any changes to character data
characterDataOldValue: true, // we keep the old value
childList: true, // we get notified about changes to a node's children
subtree: true, // we get notified about any changes to any of a node's descendants
};
const callback: MutationCallback = (mutationsList: MutationRecord[]) => {
mutationsList.forEach((record: MutationRecord) => {
// If something has been added
if (record.addedNodes.length > 0) {
// For each added node
record.addedNodes.forEach((addedNode: Node) => {
// For each node with Hebrew script in addedNode
getHebrewTextNodesIn(addedNode).forEach((hebrewNode: Node) => {
updateNode(hebrewNode, textSize, lineHeight, font);
});
});
}
// If the value has changed
if (
record.target.nodeValue !== record.oldValue &&
record.target.parentNode instanceof Node
) {
// If the node now has Hebrew text when it didn't, this is rare and only occurs on YouTube
getHebrewTextNodesIn(record.target.parentNode).forEach((hebrewNode: Node) => {
updateNode(hebrewNode, textSize, lineHeight, font);
});
}
});
};
observer = new MutationObserver(callback);
observer.observe(document.body, config);
}
}
async function notifyDocument() {
if (!hasDocumentBeenUpdated()) {
let meta = document.createElement("meta");
meta.id = "behirutMetaElement";
meta.setAttribute("behirut", "true");
document.head.appendChild(meta);
}
}
async function toggleOff() {
if (!!observer) {
observer.disconnect();
observer = null;
}
getHebrewTextNodesIn(document.body).forEach((node: Node) => {
node.parentElement.style.fontSize = null;
node.parentElement.style.lineHeight = null;
node.parentElement.style.fontFamily = null;
});
}
async function addMessageListener() {
runtime.onMessage.addListener((message: any) => {
if (message.reason == null) return;
if (message.reason === reasonUpdateAllText) {
main();
}
if (message.reason === reasonInjectCustomFonts) {
injectCustomFonts(message.customFonts);
}
if (message.reason === reasonToggleOff) {
toggleOff();
}
});
}
async function main() {
const storage: Storage = await sync.get(keys);
let textSize: number = storage.textSize;
let lineHeight: number = storage.lineHeight;
let font: string = storage.font;
const isOn: boolean = storage.onOff;
const whitelisted: string[] = storage.whitelisted;
const customSettings: CustomSetting[] = storage.customSettings;
const customFonts: CustomFont[] = storage.customFonts;
const thisURL: string = new URL(document.URL).hostname;
const isWhitelisted: boolean = !!whitelisted.find((it) => it === thisURL);
const customSite: CustomSetting = customSettings.find(
(custom: CustomSetting) => custom.url === thisURL
);
// Only do anything if the switch is on and this site is not whitelisted
if (isOn && !isWhitelisted) {
injectCustomFonts(customFonts);
// If it's a custom site then change the textSize, lineHeight and font
if (customSite) {
textSize = customSite.textSize;
lineHeight = customSite.lineHeight;
font = customSite.font;
}
updateAll(textSize, lineHeight, font);
startObserver(textSize, lineHeight, font);
notifyDocument();
}
// We've been updated and now we've become whitelisted
if (hasDocumentBeenUpdated() && isWhitelisted) {
toggleOff();
}
}
main();
addMessageListener();