-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
137 lines (119 loc) · 3.69 KB
/
content.js
File metadata and controls
137 lines (119 loc) · 3.69 KB
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
var justInput = document.getElementsByTagName("input");
console.log(justInput);
const justAlert = () => {
console.log("active input");
};
for (i = 0; i < justInput.length; i++) {
// justInput[i].onchange= justAlert;
justInput[i].addEventListener("input", event => {
const newValue = event.target.value.toLowerCase();
console.log(event.target.value);
});
}
const arr = [" just ", " like ", " actually ", " sorry ", " but "];
for (let i = 0; i < arr.length; i++) {
if (arr.indexOf(justInput) !== -1) {
//highlight and provide suggestion
let replaced = "";
let boldText =
'<div style="background-color: yellow; display: inline; font-weight: bold;">' +
arr[i] +
"</div>";
let match = "suggestion";
replaced = justInput.replace(match, boldText);
} else if (arr.indexOf(justInput) == -1) {
console.log("error"); // ?
}
}
// if(justInput.activeElement){
// console.log('use just')
// }
function walk(rootNode) {
// Find all the text nodes in rootNode
var walker = document.createTreeWalker(
rootNode,
NodeFilter.SHOW_TEXT,
null,
false
),
node;
// Modify each text node's value
while ((node = walker.nextNode())) {
handleText(node);
}
}
function handleText(textNode) {
textNode.nodeValue = replaceText(textNode.nodeValue);
if (textNode.nodeValue === "just") {
console.log("found just");
// textNode.classList.add('blue');
}
}
// function selectText(textNode) {
// if(textNode.nodeType === )
// }
function replaceText(v) {
// Fix some misspellings
v = v.replace(/\b(J|j)ust(s)?\b/g, "$1ust$2");
v = v.replace(/\b(J|j)ust(s)?\b/g, "$1ust$2");
v = v.replace(/\b(J|j)ust(s)?\b/g, "$1ust$2");
v = v.replace(/\b(J|j)ust(s)?\b/g, "$1ust$2");
// Millennial Generation
v = v.replace(
/\b(?:Millennial Generation)|(?:Generation Millennial)\b/g,
"Plissken Faction"
);
return v;
}
// Returns true if a node should *not* be altered in any way
function isForbiddenNode(node) {
return (
node.isContentEditable || // DraftJS and many others
(node.parentNode && node.parentNode.isContentEditable) || // Special case for Gmail
(node.tagName &&
(node.tagName.toLowerCase() == "textarea" || // Some catch-alls
node.tagName.toLowerCase() == "input"))
);
}
// The callback used for the document body and title observers
function observerCallback(mutations) {
var i, node;
mutations.forEach(function(mutation) {
for (i = 0; i < mutation.addedNodes.length; i++) {
node = mutation.addedNodes[i];
if (isForbiddenNode(node)) {
// Should never operate on user-editable content
continue;
} else if (node.nodeType === 3) {
// Replace the text for text nodes
handleText(node);
} else {
// Otherwise, find text nodes within the given node and replace text
walk(node);
}
}
});
}
// Walk the doc (document) body, replace the title, and observe the body and title
function walkAndObserve(doc) {
var docTitle = doc.getElementsByTagName("title")[0],
observerConfig = {
characterData: true,
childList: true,
subtree: true
},
bodyObserver,
titleObserver;
// Do the initial text replacements in the document body and title
walk(doc.body);
doc.title = replaceText(doc.title);
// Observe the body so that we replace text in any added/modified nodes
bodyObserver = new MutationObserver(observerCallback);
bodyObserver.observe(doc.body, observerConfig);
// Observe the title so we can handle any modifications there
if (docTitle) {
titleObserver = new MutationObserver(observerCallback);
titleObserver.observe(docTitle, observerConfig);
}
}
walkAndObserve(document);