-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
55 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
// Christina Aiello, 2019
// Thank you Tom Maxwell for the starter code!
// https://9to5google.com/2015/06/14/how-to-make-a-chrome-extensions/
chrome.storage.sync.get(['userEnteredName'], function(result) {
var nameSavedInChromeStorage = result["userEnteredName"];
if (nameSavedInChromeStorage != null) {
replaceNamesWithUserInput(nameSavedInChromeStorage.toString());
}
});
function replaceNamesWithUserInput(userEnteredName) {
console.log("Starting Name-Replacement Extension's replace method")
var elements = document.getElementsByTagName('*');
var cachedFoundNames = new Array();
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;
var wordsInNode = text.split(" ");
wordsInNode.forEach(function(value)
{
text = node.nodeValue;
var firstLetter = value.replace(/[^\w\s]/gi, '').charAt(0).toLowerCase();
if (cachedFoundNames.includes(value))
{
var replacedText = replaceText(text, value, userEnteredName);
node.nodeValue = replacedText;
}
else if (firstLetter in nameData && nameData[firstLetter].includes(value))
{
var replacedText = replaceText(text, value, userEnteredName);
node.nodeValue = replacedText;
cachedFoundNames.push(value);
}
});
}
}
}
console.log("Finished Name-Replacement Extension's replace method")
}
function replaceText(text, value, userEnteredName) {
console.log("Value, the name to replace, is: " + value);
console.log("UserEnteredName is: " + userEnteredName);
var updatedText = text.replace(value, userEnteredName);
return updatedText;
}