-
Notifications
You must be signed in to change notification settings - Fork 6
/
content.js
62 lines (50 loc) · 2.1 KB
/
content.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
var elements = document.getElementsByTagName('*');
var sourceWordsToTargetWords = [
[['bro', 'bruh', 'bruv', 'brah', 'broseph', 'mate'], 'boyfriend'],
[['bros', 'bruhs', 'bruvs', 'brahs'], 'boyfriends'],
[['brosephine'], 'girlfriend'],
[['babe'], 'idk your name (so I\'m going to call you something that supposedly sounds like I\'m complimenting you because I have a favour to ask)'],
];
function makeRegex(sourceWords) {
return new RegExp('\\b' + sourceWords.join('\\b|\\b') + '\\b', 'g');
};
function identity(string) {
return string;
};
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
function toUpperCase(string) {
return string.toUpperCase();
};
function makeRegexToTargetWords(sourceWordsToTargetWords, modifyWords) {
return sourceWordsToTargetWords.map(function(sourceAndTarget) {
var [source,target] = sourceAndTarget;
source = source.map(modifyWords);
target = modifyWords(target);
return [makeRegex(source), target];
});
};
var sourceRegexToTargetWords = makeRegexToTargetWords(sourceWordsToTargetWords, identity).concat(makeRegexToTargetWords(sourceWordsToTargetWords, capitalizeFirstLetter)).concat(makeRegexToTargetWords(sourceWordsToTargetWords, toUpperCase));
function replaceTextWithRegexes(text, sourceRegexToTargetWords) {
for (var k = 0; k < sourceRegexToTargetWords.length; k++) {
var [regex, targetWord] = sourceRegexToTargetWords[k];
var replacedText = text.replace(regex, targetWord);
text = replacedText
}
return text;
};
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
for (var j = 0; j < element.childNodes.length; j++) {
var node = element.childNodes[j];
if (node.nodeType === 3) {
var text = node.nodeValue;
replacedText = replaceTextWithRegexes(text, sourceRegexToTargetWords);
if (replacedText !== text) {
console.log('replaced');
element.replaceChild(document.createTextNode(replacedText), node);
}
}
}
}