-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
395 lines (356 loc) · 12.8 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
document.addEventListener('DOMContentLoaded', init, false);
let mode;
let notFound;
let wordGuess;
let infoBlock;
let diff = '';
let infoButton;
let globlSuggestedWord = '';
let answerList = [];
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
answerList = allText.split(/\n|\r/g);
}
}
}
rawFile.send(null);
}
readTextFile('possible_answer');
function writeTextFile() {
var http = new XMLHttpRequest();
var url = 'round_answer';
var params = '';
for(let word of answerList) {
params += word + '\n';
}
alert(params);
http.open('POST', url, true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
}
/*
Simulate all possible scenarios, brute force way
Knowing a guess (generated based on the temp_guessList (below)
and a list of letters' good info (mostly Y and G, Gray has already been eliminated)*/
class Letter {
constructor(color, letter, index) {
this.color = color;
this.letter = letter;
this.index = index;
}
}
/*
Helper class: give out 3 best suggestions
Helper(guess, letters_info).nextguess returns a list of 3 strings
guess: string
letters_info: dictionary of letter's info [ COLOR-LETTER-INDEX, ...] (e.g., Ya2 stands for Yellow - letter 'a' - index 2)
NOTE FOR 27/2:
- Change input for Helper according to json input from html extension
- wtf did I created in Helper __init__ ._.
sample input: {"hints":["Ya1"],"greys":["p","e","r"]} #test1.json
*/
class Helper {
constructor(letters_info) {
this.colorlist = [];
this.graylist = [];
this.suggestionlist = [];
for (var hint, k = 0, i = letters_info.hints, j = i.length; k < j; k += 1) {
hint = i[k];
this.colorlist.push(new Letter(hint[0], hint[1], hint[2]));
}
for (var greys, k = 0, i = letters_info.greys, j = i.length; k < j; k += 1) {
greys = i[k];
this.graylist.push(greys);
}
}
eliminate() {
for (let character in this.graylist)
{
let i = 0;
while(i < answerList.length)
{
let a_guess = answerList[i];
if (a_guess.includes(character))
{
answerList.splice(answerList.indexOf(a_guess),1);
i = i;
}
else
i++;
}
}
for (let color_letter in this.colorlist)
{
let index = Number.parseInt(color_letter.index);
for (let a_guess in answerList)
{
if (color_letter.color === "G") {
if (a_guess[index] === color_letter.letter)
{
if (!this.suggestionlist.includes(a_guess))
this.suggestionlist.push(a_guess);
}
}
if (color_letter.color === "Y" && a_guess[index] !== color_letter.letter)
this.suggestionlist.push(a_guess);
}
}
if (this.suggestionlist.length > 0) {
for (green in this.colorlist) {
if (green.color === "G")
{
let i = 0;
while(i < this.suggestionlist.length)
{
let green_index = Number.parseInt(green.index);
let suggest = this.suggestionlist[i];
if (suggest[green_index] !== green.letter) {
this.suggestionlist.splice(this.suggestionlist.indexOf(suggest),1);
i = i;
}
else
i++;
}
}
}
}
}
nextguess() {
this.eliminate();
const randomElement = this.suggestionlist[Math.floor(Math.random() * this.suggestionlist.length)];
if (this.suggestionlist.length > 0)
{
//console.log(randomElement, " - Success rate: ", "%.2f" % round_answer(100 / this.suggestionlist.length, 2), "%");
return randomElement;
}
else
{
//console.log("This is your first guess, try SALET!");
return "salet";
}
}
}
function useHelper(json_file) {
var data = JSON.parse(json_file);
return new Helper(data).nextguess();
}
async function scrapeLetters() {
let hintLetters = [], greyLetters = []
let rows = document.getElementsByTagName('game-app')[0].shadowRoot.querySelectorAll('game-row');
let i;
for(let row of rows) {
let tiles = row.shadowRoot.querySelectorAll('game-tile');
i = 0;
for(let tile of tiles) {
let char = tile.getAttribute("letter");
if(char == null)
continue;
let status = tile.getAttribute("evaluation");
if(status === "correct") {
char = 'G' + char + i;
if(!hintLetters.includes(char))
hintLetters.push(char);
} else if(status === 'present') {
char = 'Y' + char + i;
if(!hintLetters.includes(char))
hintLetters.push(char);
} else {
if(!greyLetters.includes(char))
greyLetters.push(char);
}
i++;
}
}
return "{\"hints\":" + JSON.stringify(hintLetters) + ',\"greys\":' + JSON.stringify(greyLetters) + "}";
}
async function fillLetters(word) {
let rows = document.getElementsByTagName('game-app')[0].shadowRoot.querySelectorAll('game-row');
for(let row of rows) {
if(row.getAttribute("letters") === '' || row.getAttribute("letters") === null) {
let tiles = row.shadowRoot.querySelectorAll('game-tile');
let i = 0;
for(let tile of tiles) {
tile.setAttribute("letter", word[i].toLowerCase());
i++;
}
return;
}
}
}
function autofill() {
let tabId;
doInCurrentTab(function (tab) {
tabId = tab.id;
if (globlSuggestedWord !== null) {
chrome.scripting.executeScript({
target: {tabId: tabId},
func: fillLetters,
args: [globlSuggestedWord]
}, () => {
});
}
});
}
function info() {
if(infoButton.innerText === 'Info') {
chrome.storage.sync.set({'state': 'info'});
infoButton.innerText = 'Back';
mode.style.display = 'none';
notFound.style.display = 'none';
wordGuess.style.display = 'none';
infoBlock.style.display = 'block';
} else {
infoButton.innerText = 'Info';
mode.style.display = 'block';
notFound.style.display = 'none';
wordGuess.style.display = 'none';
infoBlock.style.display = 'none';
}
}
function changeWord(word) {
let letterBoxes = document.getElementsByClassName("word");
let i = 0;
for(let box of letterBoxes) {
if(i < word.length) {
box.innerHTML = word[i].toUpperCase();
} else {
box.innerHTML = '';
}
i++;
}
}
function doInCurrentTab(tabCallback) {
chrome.tabs.query(
{ currentWindow: true, active: true },
function (tabArray) { tabCallback(tabArray[0]); }
);
}
// Function that handles suggesting each word
function suggestWord() {
let input = '';
let tabId;
doInCurrentTab(function (tab) {
tabId = tab.id;
chrome.scripting.executeScript({target: {tabId: tabId},func: scrapeLetters}, (results) => {
if(!chrome.runtime.lastError) {
for(let result of results) {
input = result.result;
let output = useHelper(input);
changeWord(output);
chrome.storage.sync.set({'word': output});
}
}
});
});
}
function normal() {
chrome.storage.sync.set({'state': 'guess'});
mode.style.display = 'none';
notFound.style.display = 'none';
wordGuess.style.display = 'block';
}
function init() {
mode = document.getElementById("modeContainer");
notFound = document.getElementById("notFound");
wordGuess = document.getElementById("wordGuess");
let mainTitle = document.getElementById("name");
infoBlock = document.getElementById("creditContainer");
let startButton = document.getElementById("startButton");
infoButton = document.getElementById("info");
let goButton = document.getElementById("goToWordle");
let nextButton = document.getElementById("next");
//autoButton = document.getElementById("autofill");
doInCurrentTab(function (tab) {
chrome.storage.sync.get(['state','word','tabInfo'], function (data) {
//Update popupElement1 and popupElement2 with loaded data
let state = data.state;
let word = data.word;
let tabI = data.tabInfo;
if(tabI === tab.id.toString()) {
if (state === 'main' || state === null || state === '') {
mode.style.display = 'none';
notFound.style.display = 'none';
wordGuess.style.display = 'none';
infoBlock.style.display = 'none';
changeWord('');
chrome.storage.sync.set({'state': 'main'});
} else if (state === 'mode') {
mainTitle.classList.remove("anim");
infoButton.style.display = 'block';
mode.style.display = 'block';
notFound.style.display = 'none';
infoBlock.style.display = 'none';
wordGuess.style.display = 'none';
} else if(state === 'guess') {
mainTitle.classList.remove("anim");
infoButton.style.display = 'block';
mode.style.display = 'none';
notFound.style.display = 'none';
infoBlock.style.display = 'none';
wordGuess.style.display = 'block';
if(word !== null) {
changeWord(word);
}
} else if(state === 'notFound') {
mainTitle.classList.remove("anim");
infoButton.style.display = 'block';
mode.style.display = 'none';
notFound.style.display = 'block';
wordGuess.style.display = 'none';
infoBlock.style.display = 'none';
} else if(state === 'info') {
mainTitle.classList.remove("anim");
infoButton.style.display = 'block';
infoButton.innerText = 'Back';
mode.style.display = 'none';
notFound.style.display = 'none';
wordGuess.style.display = 'none';
infoBlock.style.display = 'block';
}
} else {
chrome.storage.sync.set({'state': 'main'});
changeWord('');
}
chrome.storage.sync.set({'tabInfo': tab.id.toString()});
});
});
startButton.onclick = normal;
goButton.onclick = function () {chrome.tabs.update({url: "https://www.nytimes.com/games/wordle/index.html"});}
nextButton.onclick = suggestWord;
infoButton.onclick = info;
//autoButton.onclick = autofill;
let title = document.getElementById("name");
title.addEventListener("animationend", function() {
infoButton.style.display = 'block';
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
let url = tabs[0].url;
if (url === "https://www.nytimes.com/games/wordle/index.html") {
chrome.storage.sync.set({'state': 'mode'});
mode.style.display = 'block';
notFound.style.display = 'none';
infoBlock.style.display = 'none';
wordGuess.style.display = 'none';
} else {
chrome.storage.sync.set({'state': 'notFound'});
mode.style.display = 'none';
notFound.style.display = 'block';
infoBlock.style.display = 'none';
wordGuess.style.display = 'none';
}
});
});
}