-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_script.js
101 lines (94 loc) · 2.77 KB
/
content_script.js
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
const detect3awalem = function (innerHTML){
let detected3awalem = [];
let STATE_INITIAL = 0;
let STATE_COMMIT = 1;
let currentState = STATE_INITIAL;
let lines = innerHTML.split("\n");
let tokens = [];
for (let line of lines){
tokens = tokens.concat(line.split(" "));
}
let confirmationCounter = 0;
let buffer = [];
for (let token of tokens){
let sanitizedToken = token.trim();
if (sanitizedToken.startsWith("الس")) {
sanitizedToken = sanitizedToken.substring(2, sanitizedToken.length);
}
if (currentState == STATE_INITIAL) {
buffer = [];
if (!sanitizedToken.startsWith("س")) {
currentState = STATE_INITIAL;
if (confirmationCounter < 2) {
confirmationCounter = 0;
}
continue;
} else {
currentState = STATE_COMMIT;
buffer = buffer.concat(token.split("\n")[0]);
continue;
}
}
if (currentState == STATE_COMMIT){
buffer = buffer.concat(token.split("\n")[0]);
confirmationCounter++;
detected3awalem.push(buffer);
currentState = STATE_INITIAL;
continue
}
}
if (confirmationCounter >=2){
return detected3awalem;
} else {
return [];
}
}
const humanize3awalem = function (tokenPair) {
let sinWord = tokenPair[0];
let afterWord = tokenPair[1];
let sanitizedSinWord = sinWord;
//let sanitizedSinWord = sinWord.trim();
if (sanitizedSinWord.startsWith("الس")) {
sanitizedSinWord = sanitizedSinWord.substring(2, sanitizedSinWord.length);
}
let finalSanitizedWord = afterWord[0] + sanitizedSinWord.substring(1, sanitizedSinWord.length);
let finalWord = sinWord.replace(sanitizedSinWord, finalSanitizedWord);
return finalWord
}
const verifyDataKey = function (item) {
if (item.getAttribute("data-text") == "true"){
return true;
}
for (let i=0; i<item.children.length; i++){
if (item.children[i].getAttribute("data-text") == "true"){
return true;
}
if (verifyDataKey(item.children[i])) {
return true;
}
}
return false;
}
const checkOccurences = function (replace=true){
const text = document.querySelectorAll('h1, h2, h3, h4, h5, p, li, td, caption, span, a');
for (let i=0; i < text.length; i++){
if (!verifyDataKey(text[i])) {
if (replace) {
let tokenPairs = detect3awalem(text[i].innerText);
if (tokenPairs.length > 0) {
console.log(text[i]);
console.log(tokenPairs);
}
for (let pair of tokenPairs) {
let original = pair[0] + " " + pair[1];
let fixed = humanize3awalem(pair);
text[i].innerHTML = text[i].innerHTML.replace(original, fixed);
}
} else {
console.log(text[i]);
}
}
}
setTimeout(checkOccurences, 1000);
}
checkOccurences();