-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpopup.js
70 lines (62 loc) · 2.21 KB
/
popup.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
// Shuffle the suggestions given
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// Update the icon image and badge based on settings
function updateIcon(settings, numWords, tabId) {
const hc = settings.HighContrast;
chrome.action.setIcon({
path: {
"16": `/images/icon-16${hc ? '-hc' : ''}.png`,
"32": `/images/icon-32${hc ? '-hc' : ''}.png`,
"48": `/images/icon-48${hc ? '-hc' : ''}.png`,
"128": `/images/icon-128${hc ? '-hc' : ''}.png`
},
tabId
});
chrome.action.setBadgeText({ text: `${numWords || 1}`, tabId });
chrome.action.setBadgeBackgroundColor({ color: hc ? '#f5793a' : '#538d4e' });
}
// Update the popup colors based on dark mode and contrast settings
function updateColors(settings) {
const bodyClasses = document.body.classList;
if (settings.DarkMode) {
bodyClasses.remove('lightmode');
bodyClasses.add('darkmode');
} else {
bodyClasses.remove('darkmode');
bodyClasses.add('lightmode');
}
if (settings.HighContrast) {
bodyClasses.remove('nocontrast');
bodyClasses.add('highcontrast');
} else {
bodyClasses.remove('highcontrast');
bodyClasses.add('nocontrast');
}
}
// Run when the popup is clicked and elements are loaded
document.addEventListener('DOMContentLoaded', async () => {
const numWords = document.getElementById('numWords');
const possibleHTML = document.getElementById('possible');
let [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
url: "https://www.nytimes.com/games/wordle/index.html*"
});
// Try to get results only if on NYT page
if (tab) {
// Send empty message to solver.js to get state and update
await chrome.tabs.sendMessage(tab.id, {}, ({ possible={}, settings={} }) => {
shuffleArray(possible);
numWords.innerHTML = `${possible.length} possible word${possible.length === 1 ? '' : 's'}`;
const suggestions = possible.map(word => `${word.toUpperCase()}`).join(', ');
possibleHTML.innerHTML = suggestions;
updateIcon(settings, possible.length, tab.id);
updateColors(settings);
});
}
})