-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
88 lines (75 loc) · 2.29 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
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
let isClicked = false;
let gotMessage = (message, sender, sendResponse) => {
replaceImg();
isClicked = !isClicked;
if(message.txt === "button" && isClicked) {
colorText(document.body);
} else {
unColorText(document.body);
}
if(message.txt != "button") {
let paragraphs = document.getElementsByTagName('p');
iuText(paragraphs);
}
}
let filenames = [
"IU.jpg",
"IU1.jpg",
"IU2.jpg",
"IU3.jpg",
"IU4.jpg",
"IU5.jpg"
];
let colorText = (element) => {
if(element.hasChildNodes()) {
element.childNodes.forEach(colorText);
//text elements have no child nodes
} else if (element.nodeType === Text.TEXT_NODE) {
if(element.textContent.toLowerCase().includes('iu')) {
let newElement = document.createElement('span');
newElement.innerHTML = element.textContent.replace(/(iu)/gi,
'<span class="rainbow">$1</span>')
element.replaceWith(newElement)
}
}
}
let replaceImg = () => {
let imgs = document.getElementsByTagName('img');
for (imgElt of imgs) {
// let r = Math.floor(Math.random() * filenames.length);
// let file = 'IUimg/' + filenames[r];
// let url = chrome.extension.getURL(file);
// imgElt.src = url;
imgElt.src = 'https://iu.wonder.im/images/profile';
}
}
let unColorText = (element) => {
if(element.hasChildNodes()) {
element.childNodes.forEach(unColorText);
}
if(element.classList) {
for(let i = 0; i < element.classList.length; i++) {
if(element.classList.contains("rainbow")){
element.classList.remove("rainbow");
}
}
}
}
let iuText = (paragraphs) => {
for( p of paragraphs) {
// p.innerHTML = message.txt;
let paragraph = p.textContent.split(" ");
// console.log(paragraph)
// console.log(paragraph.length);
let newString = "";
for(let i = 0; i < paragraph.length; i++) {
newString = newString.concat("IU <3");
if(i != paragraph.length - 1) {
newString = newString.concat(" ");
}
}
console.log(newString)
p.innerHTML = newString;
}
}
chrome.runtime.onMessage.addListener(gotMessage);