-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetHints.js
88 lines (67 loc) · 2.13 KB
/
getHints.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
function extractFoundWords() {
return document.querySelector('ul.sb-wordlist-items-pag').innerText.split('\n').map(x => x.toLowerCase());
}
function injectScript(file, node) {
const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', file);
const outerElement = document.getElementsByTagName(node)[0];
outerElement.appendChild(script);
}
function extractAnswersFromList(listNode) {
var extractedAnswers = [];
listNode.querySelectorAll('li').forEach(element => {
extractedAnswers.push(element.textContent.toLowerCase());
});
return extractedAnswers;
}
hiddenAnswersElement = document.querySelector('ul.hidden-answers-list');
if (hiddenAnswersElement == null) {
injectScript(browser.runtime.getURL('/injectAnswers.js'), 'body');
}
daAnswers = extractAnswersFromList(hiddenAnswersElement);
foundWords = extractFoundWords();
filteredAnswers = daAnswers.filter(x => !foundWords.includes(x));
// filteredAnswers.join('\n');
// ===================================
// Create the hints
// ===================================
function transformAnswersToHints(answers) {
function addHint(hints, answer) {
const firstLetter = answer[0];
if(!hints.hasOwnProperty(firstLetter)) {
hints[firstLetter] = {
count: 0,
lengths: {},
firstTwo: {}
}
}
hint = hints[firstLetter];
hint.count += 1;
return hint;
}
function addLength(hint, answer) {
const length = answer.length
if(!hint.lengths.hasOwnProperty(length)) {
hint.lengths[length] = 1
} else {
hint.lengths[length] += 1
}
}
function addFirstTwoLetters(hint, answer) {
const firstTwoLetters = answer.slice(0, 2)
if(!hint.firstTwo.hasOwnProperty(firstTwoLetters)) {
hint.firstTwo[firstTwoLetters] = 1
} else {
hint.firstTwo[firstTwoLetters] += 1
}
}
hints = {}
for (answer of answers){
const hint = addHint(hints, answer);
addLength(hint, answer);
addFirstTwoLetters(hint, answer);
}
return hints;
}
JSON.stringify(transformAnswersToHints(filteredAnswers), null, 2);