-
Notifications
You must be signed in to change notification settings - Fork 0
/
substitute.js
107 lines (94 loc) · 3.04 KB
/
substitute.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
102
103
104
105
106
107
/*
* This file is responsible for performing the logic of replacing
* all occurrences of each mapped word with its counterpart.
*/
/*global blazingFastReplacements */
let blazingMap = [
'absurdly ',
'flaringly ',
'flamingly ',
'like, REALLY ',
'scorchingly ',
'super-',
'way ',
];
let fastMap = [
'agile',
'brisk',
'fast',
'high-speed',
'quick',
'rapid',
'snappy',
'speedy',
'sprightly',
'swift',
];
/*
* For efficiency, create a word --> search RegEx Map too.
*/
let bfRegex = new RegExp(/\bblazing(ly)?[-\s]+fast\b/, 'gi');
function selectResponse() {
let bi = Math.floor(Math.random() * blazingMap.length);
let blazing = blazingMap[bi];
let fi = Math.floor(Math.random() * fastMap.length);
let fast = fastMap[fi];
return `🔥${blazing}${fast}🔥`;
}
/**
* Substitutes text into text nodes.
* If the node contains more than just text (ex: it has child nodes),
* call replaceText() on each of its children.
*
* @param {Node} node - The target DOM Node.
* @return {void} - Note: the substitution is done inline.
*/
function replaceText (node) {
// Setting textContent on a node removes all of its children and replaces
// them with a single text node. Since we don't want to alter the DOM aside
// from substituting text, we only substitute on single text nodes.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
if (node.nodeType === Node.TEXT_NODE) {
// This node only contains text.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType.
// Skip textarea nodes due to the potential for accidental submission
// of substituted text where none was intended.
if (node.parentNode && node.parentNode.nodeName === 'TEXTAREA') {
return;
}
// Because DOM manipulation is slow, we don't want to keep setting
// textContent after every replacement. Instead, manipulate a copy of
// this string outside of the DOM and then perform the manipulation
// once, at the end.
let content = node.textContent;
content = content.replace(bfRegex, selectResponse);
node.textContent = content;
}
else {
// This node contains more than just text, call replaceText() on each
// of its children.
for (let i = 0; i < node.childNodes.length; i++) {
replaceText(node.childNodes[i]);
}
}
}
// Start the recursion from the body tag.
replaceText(document.body);
// Now monitor the DOM for additions and substitute text into new nodes.
// @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver.
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes && mutation.addedNodes.length > 0) {
// This DOM change was new nodes being added. Run our substitution
// algorithm on each newly added node.
for (let i = 0; i < mutation.addedNodes.length; i++) {
const newNode = mutation.addedNodes[i];
replaceText(newNode);
}
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});