-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
276 lines (238 loc) · 9.05 KB
/
Copy pathpopup.js
File metadata and controls
276 lines (238 loc) · 9.05 KB
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
// rarityScale is now imported from animations.js
// rarityOrder is now imported from profile.js
// escapeHtml, displayWordDex, etc are now imported from worddex.js
async function setupDarkMode() {
const toggle = document.getElementById('darkModeToggle');
if (!toggle) return;
// Wait for language to be loaded
await getCurrentLanguage();
return new Promise((resolve) => {
chrome.storage.local.get(['darkMode'], (data) => {
if (data.darkMode) {
document.body.classList.add('dark-mode');
toggle.textContent = t('lightMode');
} else {
toggle.textContent = t('darkMode');
}
toggle.addEventListener('click', () => {
const isDark = document.body.classList.toggle('dark-mode');
toggle.textContent = isDark ? t('lightMode') : t('darkMode');
chrome.storage.local.set({ darkMode: isDark });
});
resolve();
});
});
}
function updateUILanguage() {
// Update header title
const headerTitle = document.querySelector('h3');
// if (headerTitle) {
// headerTitle.textContent = t('wordDex');
// }
// Update search placeholder
const searchBar = document.getElementById('searchBar');
if (searchBar) {
searchBar.placeholder = t('searchPlaceholder');
}
// Update sort buttons
const sortButtons = document.querySelectorAll('.sort-btn');
if (sortButtons.length >= 3) {
sortButtons[0].textContent = t('sortAlpha');
sortButtons[1].textContent = t('sortRecent');
sortButtons[2].textContent = t('sortTag');
}
// Update tabs
const tabs = document.querySelectorAll('.nav-tab');
if (tabs.length >= 3) {
tabs[0].textContent = t('tabDex');
tabs[1].textContent = t('tabQuiz');
tabs[2].textContent = t('tabBattle');
}
// Update quiz button
const quizBtn = document.getElementById('quizBtn');
if (quizBtn) {
quizBtn.textContent = t('quiz');
}
// Update dark mode button
const darkModeToggle = document.getElementById('darkModeToggle');
if (darkModeToggle) {
const isDark = document.body.classList.contains('dark-mode');
darkModeToggle.textContent = isDark ? t('lightMode') : t('darkMode');
}
// Update Profile Button
const authBtn = document.getElementById('authBtn');
if (authBtn && authBtn.classList.contains('dark-mode-toggle')) {
const img = authBtn.querySelector('img');
if (img) {
authBtn.innerHTML = '';
authBtn.appendChild(img);
authBtn.appendChild(document.createTextNode('\n ' + t('tabProfile') + '\n '));
}
}
}
async function setupLanguageToggle() {
const toggle = document.getElementById('langToggle');
if (!toggle) return;
// Load current language and update UI
const lang = await getCurrentLanguage();
toggle.textContent = lang === 'ko' ? 'KO' : 'EN';
updateUILanguage();
toggle.addEventListener('click', async () => {
const currentLang = await getCurrentLanguage();
const newLang = currentLang === 'en' ? 'ko' : 'en';
await setLanguage(newLang);
toggle.textContent = newLang === 'ko' ? 'KO' : 'EN';
// Update all UI text
updateUILanguage();
// Check active tab and refresh appropriate view
const activeTab = document.querySelector('.nav-tab.active')?.getAttribute('data-tab');
if (activeTab === 'quiz') {
const container = document.getElementById('quizContainer');
// Check if we are in the menu (contains start button) or in a quiz
if (container.querySelector('#btnStartCustomQuiz')) {
renderQuizMenu();
} else if (typeof quizData !== 'undefined' && quizData.currentIndex < quizData.total) {
showQuestion();
} else {
showResults();
}
} else if (activeTab === 'battle') {
const container = document.getElementById('battleContainer');
// Check if menu or battle
if (container.querySelector('#btnRandomBattle')) {
renderBattleMenu();
} else if (typeof battleSystem !== 'undefined' && battleSystem && battleSystem.active) {
const oldLog = document.getElementById('battleLog')?.innerHTML;
battleSystem.renderLayout();
battleSystem.updateUI();
if (oldLog && document.getElementById('battleLog')) {
document.getElementById('battleLog').innerHTML = oldLog;
}
} else {
// Default fallback to menu
renderBattleMenu();
}
} else {
// Default to Dex
if (typeof displayWordDex !== 'undefined') {
displayWordDex(currentSort); // currentSort is now global from worddex.js
}
}
});
}
function setupTabs() {
const tabs = document.querySelectorAll('.nav-tab');
tabs.forEach(tab => {
tab.addEventListener('click', () => {
const tabName = tab.getAttribute('data-tab');
switchTab(tabName);
});
});
}
function switchTab(tabName) {
// Update Tab UI
document.querySelectorAll('.nav-tab').forEach(t => {
t.classList.remove('active');
if (t.getAttribute('data-tab') === tabName) t.classList.add('active');
});
// Hide All Views
if (document.getElementById('mainContent')) document.getElementById('mainContent').style.display = 'none';
if (document.getElementById('quizContainer')) document.getElementById('quizContainer').style.display = 'none';
if (document.getElementById('battleContainer')) document.getElementById('battleContainer').style.display = 'none';
if (document.getElementById('profileContainer')) document.getElementById('profileContainer').style.display = 'none';
// Show Controls/Search only on Dex tab
const showControls = (tabName === 'dex');
const searchBar = document.getElementById('searchBar');
const controls = document.querySelector('.controls');
if (searchBar) searchBar.style.display = showControls ? 'block' : 'none';
if (controls) controls.style.display = showControls ? 'flex' : 'none';
// Logic per tab
if (tabName === 'dex') {
document.getElementById('mainContent').style.display = 'block';
if (typeof displayWordDex !== 'undefined') {
displayWordDex(currentSort);
}
} else if (tabName === 'quiz') {
document.getElementById('quizContainer').style.display = 'block';
renderQuizMenu();
} else if (tabName === 'battle') {
startBattle();
} else if (tabName === 'profile') {
if (typeof showProfile !== 'undefined') {
showProfile(false); // Pass false to indicate it's a tab switch, don't hide everything manually if possible (but current implementation does)
}
}
}
function initKofi() {
if (typeof kofiwidget2 !== 'undefined') {
kofiwidget2.init('Support me on Ko-fi', '#72a4f2', 'E1E21LP8M8');
const container = document.getElementById('kofi-container');
if (container) {
container.innerHTML = kofiwidget2.getHTML();
}
}
}
async function initializePopup() {
// Initialize Supabase (from profile.js)
initSupabase();
await setupDarkMode();
await setupLanguageToggle();
// Help Modal Logic
const helpToggle = document.getElementById('helpToggle');
const helpModal = document.getElementById('helpModal');
const closeHelp = document.getElementById('closeHelp');
if (helpToggle && helpModal) {
helpToggle.onclick = () => {
helpModal.style.display = 'flex';
};
if(closeHelp) {
closeHelp.onclick = () => {
helpModal.style.display = 'none';
};
}
// Close on outside click
helpModal.onclick = (e) => {
if (e.target === helpModal) {
helpModal.style.display = 'none';
}
};
}
if (typeof setupSortButtons !== 'undefined') setupSortButtons();
if (typeof setupSearchBar !== 'undefined') setupSearchBar();
setupTabs();
initKofi();
if (typeof displayWordDex !== 'undefined') {
displayWordDex(currentSort);
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initializePopup);
} else {
initializePopup();
}
// Initialize animations for popup (enables word catching animations in popup)
if (typeof initAnimations !== 'undefined') {
initAnimations();
}
// Listen for word caught messages to display animations in popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log('Lingomon Popup: Received message:', message);
if (message.type === 'wordCaught') {
console.log('Lingomon Popup: Showing success popup for:', message.word);
if (typeof showCatchAnimation !== 'undefined') {
showCatchAnimation(message.word, message.origin, message.rarity, message.isNew, message.firstCaught, message.frequency, message.frequencySource, message.tags);
}
// Refresh the display to show the newly caught word
if (typeof displayWordDex !== 'undefined') {
displayWordDex(currentSort);
}
sendResponse({ received: true });
} else if (message.type === 'wordFailed') {
console.log('Lingomon Popup: Showing failure popup for:', message.word);
if (typeof showFailureAnimation !== 'undefined') {
showFailureAnimation(message.word, message.error);
}
sendResponse({ received: true });
}
return true;
});