-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
265 lines (254 loc) · 7.42 KB
/
main.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// store initial HTML
const initialHTML = document.body.innerHTML;
console.log('INITIATED');
// ALTERNATIVE ALGO
// try this out on Drudger and NYtimes, most headlines are anchors
// elementList is a nodeList of anchor tags that we can iterate through
const elementList = document.querySelectorAll('a');
const imageList = document.querySelectorAll('img');
console.log(imageList);
// make slider on page, position through CSS in top-right
// const slider = document.createElement('button');
// slider.innerText = 'Replace Words';
// slider['id'] = 'toggleButton';
// slider.style.position = 'fixed';
// slider.style.top = '10px';
// slider.style.right = '10px';
// console.log(slider);
// TODO: toggle between states
const toggleButton = document.createElement('button');
// made another button to put it back
// TECHNICAL CHALLENGE: getting the button to do on/off functionality (tried it out by changing innerText and adding/removing className but it didn't work; i think it was because the button was dynamically modified with each click and lost its link to the click functionality)
// ANOTHER TECHNICAL CHALLENGE: changing the anchor tags back to their previous innerHTML state. It's also difficult to capture the previous innerHTMLs of the initial node list and then iterate over that again to restore the previous states. For now, each button works once and it's restored by resetting the entire body innerHTML back to initial state
const anotherToggleButton = document.createElement('button');
toggleButton['id'] = 'toggleButton';
toggleButton.innerText = 'CODDLE ME!';
anotherToggleButton['id'] = 'anotherToggleButton';
anotherToggleButton.innerText =
"That's enough. Take me back to the real world.";
// const toggleButton = document.createElement('label');
// toggleButton['class'] = 'slider';
// toggleButton.innerText = 'Reverse Words';
// toggleButton.innerHTML = `<input type="checkbox" id="toggle" />`;
document.body.appendChild(toggleButton);
document.body.appendChild(anotherToggleButton);
document.getElementById('toggleButton').onclick = function () {
// alternate just grabs anchor tags for theDrudgeReport; can use replaceWords to do document.body.innerHTML
replaceWordsAlternate();
// console.log('hello');
// if (!toggleButton.classList.contains('reversed')) {
// toggleButton.classList.add('reversed');
// toggleButton.innerText = 'Put Words Back';
// replaceWordsAlternate();
// } else {
// toggleButton.classList.remove('reversed');
// toggleButton.innerText = 'Reverse Words';
// putWordsBack();
// }
};
document.getElementById('anotherToggleButton').onclick = function () {
putWordsBack();
};
// console.log(toggleButton);
// document.getElementById('toggleButton').addEventListener('click', () => {
// console.log('hello');
// console.log(toggleButton.classList.contains('reversed'));
// if (!toggleButton.classList.contains('reversed')) {
// toggleButton.classList.add('reversed');
// toggleButton.innerText = 'Put Words Back';
// replaceWords();
// } else {
// toggleButton.classList.remove('reversed');
// toggleButton.innerText = 'Reverse Words';
// putWordsBack();
// }
// });
const replaceWords = () => {
console.log('REPLACED');
let triggerWords = [
'omicron',
'inflation',
'covid',
'death',
'war',
'stocks',
'trillion',
'deficit',
'debt',
'guns',
'alarms',
'politics',
'court',
'biden',
'republican',
'democrat',
'senate',
'business',
'elections',
'fed',
'hike',
'news',
'conservatives',
];
let niceWords = [
'rainbows',
'butterflies',
'sunshine',
'dessert',
'sugar',
'babies',
'beautiful',
'fluff',
'giggle',
'Marshawn Lynch',
];
let replacementHTML = document.body.innerHTML;
for (let i = 0; i < triggerWords.length; i++) {
let randIndex = Math.floor(Math.random() * niceWords.length);
let triggerRegEx = new RegExp(triggerWords[i], 'gi');
// console.log(triggerRegEx);
replacementHTML = replacementHTML.replace(
triggerRegEx,
`<b style="color:pink;font-size:36px;">${niceWords[
randIndex
].toUpperCase()}</b>`
);
}
// console.log(replacementHTML);
document.body.innerHTML = replacementHTML;
};
// alternate replace function that works with the node list of anchor tags
// this is the one I'm calling
const replaceWordsAlternate = () => {
console.log('REPLACED');
let triggerWords = [
'omicron',
'inflation',
'covid',
'death',
'war',
'stocks',
'trillion',
'deficit',
'debt',
'guns',
'alarms',
'politics',
'court',
'biden',
'republican',
'democrat',
'senate',
'business',
'elections',
'fed',
'hike',
'news',
'conservatives',
'kill',
'damage',
'coronavirus',
'pandemic',
'harm',
'military',
'shot',
'fatal',
'mask',
'gun',
'abuse',
'hit',
'kidnapping',
'fire',
'trump',
'desantis',
'suicide',
];
let niceWords = [
'rainbows',
'butterflies',
'sunshine',
'dessert',
'sugar',
'babies',
'beautiful',
'fluff',
'giggle',
'Marshawn Lynch',
'spice',
'everything nice',
'Gerber',
'lullaby',
];
let colors = [
'red',
'orange',
'yellow',
'green',
'blue',
'indigo',
'violet',
'pink',
];
elementList.forEach((element) => {
let replacementHTML = element.innerHTML;
for (let i = 0; i < triggerWords.length; i++) {
let randIndex = Math.floor(Math.random() * niceWords.length);
let anotherRandIndex = Math.floor(Math.random() * colors.length);
let triggerRegEx = new RegExp(triggerWords[i], 'gi');
// console.log(triggerRegEx);
replacementHTML = replacementHTML.replace(
triggerRegEx,
`<span style="color:${
colors[anotherRandIndex]
};font-size:28px;">${niceWords[randIndex].toUpperCase()}</span>`
);
}
element.innerHTML = replacementHTML;
});
// CHANGE ALL IMAGES TO JIGGLYPUFF.. not working lol
// imageList.forEach((image) => {
// image.src =
// 'https://gamepress.gg/pokemonmasters/sites/pokemonmasters/files/styles/300h/public/2020-07/pm0039_00_purin_256.ktx_.png?itok=Xgx6SSkk';
// });
// FETCH A RANDOM FAMOUS QUOTE AND DISPLAY IT
fetch('https://quotes15.p.rapidapi.com/quotes/random/', {
method: 'GET',
headers: {
'x-rapidapi-host': 'quotes15.p.rapidapi.com',
'x-rapidapi-key': '2a9eee72a4msh645b8436f2dd348p129521jsn2232b805f1a3',
},
})
.then((data) => data.json())
.then((data) => {
console.log(data.content);
const randomQuote = document.createElement('div');
randomQuote['id'] = 'randomQuoteContainer';
randomQuote.innerHTML = `<p id="randomQuote"><b>Here's a randomly generated quote to cheer you up:</b><br><br>${data.content}<br><br><i>- Powered by QuotePark.com</i></p>`;
document.body.appendChild(randomQuote);
});
// CSS ANIMATION
const cssBubbles = document.createElement('div');
cssBubbles['id'] = 'background-wrap';
cssBubbles.innerHTML = `<div class="bubble x1"></div>
<div class="bubble x2"></div>
<div class="bubble x3"></div>
<div class="bubble x4"></div>
<div class="bubble x5"></div>
<div class="bubble x6"></div>
<div class="bubble x7"></div>
<div class="bubble x8"></div>
<div class="bubble x9"></div>
<div class="bubble x10"></div>`;
document.body.appendChild(cssBubbles);
document.body.style.background = '#00b4ff';
};
const putWordsBack = () => {
document.body.innerHTML = initialHTML;
// because of the CSS animation
document.body.style.background = 'white';
// workaround: refresh the page to reset the buttons
location.reload();
};
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
replaceWordsAlternate();
});