forked from Shpigford/mutetheweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
250 lines (224 loc) · 9.86 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
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
document.addEventListener('DOMContentLoaded', () => {
console.log('🔧 Popup initialized');
const apiKeyInput = document.getElementById('apiKey');
const enabledToggle = document.getElementById('enabled');
const statusDiv = document.getElementById('status');
const filteredCountDiv = document.getElementById('filteredCount');
const showFilteredButton = document.getElementById('showFiltered');
const blurModeToggle = document.getElementById('blur-mode');
const debugToggle = document.getElementById('debug-mode');
let isDebugMode = false;
// Filter toggles
const filterToggles = {
cynical: document.getElementById('filter-cynical'),
sarcastic: document.getElementById('filter-sarcastic'),
threatening: document.getElementById('filter-threatening'),
politics: document.getElementById('filter-politics'),
racism: document.getElementById('filter-racism')
};
// Override console methods for debug mode
const originalConsole = {
log: console.log,
error: console.error
};
function updateConsoleLogging(debugEnabled) {
if (debugEnabled) {
console.log = originalConsole.log;
console.error = originalConsole.error;
} else {
console.log = () => {};
console.error = () => {};
}
}
// Load debug mode setting
chrome.storage.local.get(['debugMode'], (result) => {
isDebugMode = result.debugMode || false;
debugToggle.checked = isDebugMode;
updateConsoleLogging(isDebugMode);
});
// Debug mode toggle handler
debugToggle.addEventListener('change', () => {
isDebugMode = debugToggle.checked;
chrome.storage.local.set({ debugMode: isDebugMode }, () => {
updateConsoleLogging(isDebugMode);
showStatus(`Debug mode ${isDebugMode ? 'enabled' : 'disabled'}!`, 'success');
// Notify content scripts of the debug mode change
chrome.tabs.query({}, function(tabs) {
tabs.forEach(tab => {
chrome.tabs.sendMessage(tab.id, {
action: 'updateDebugMode',
debugMode: isDebugMode
}).catch(() => {
// Ignore errors for inactive tabs
});
});
});
});
});
// Load saved settings and filtered count
chrome.storage.local.get(['apiKey', 'isEnabled', 'filteredPosts', 'filterSettings', 'blurMode', 'processedPosts'], (result) => {
console.log('📥 Loading saved settings:', {
hasApiKey: !!result.apiKey,
isEnabled: result.isEnabled,
blurMode: result.blurMode,
cachedPostsCount: Object.keys(result.processedPosts || {}).length
});
if (result.apiKey) {
apiKeyInput.value = result.apiKey;
}
if (typeof result.isEnabled !== 'undefined') {
enabledToggle.checked = result.isEnabled;
}
if (typeof result.blurMode !== 'undefined') {
blurModeToggle.checked = result.blurMode;
}
// Load filter settings
const filterSettings = result.filterSettings || {
cynical: true,
sarcastic: false,
threatening: false,
politics: false,
racism: false
};
// Apply saved filter settings
Object.entries(filterSettings).forEach(([key, value]) => {
if (filterToggles[key]) {
filterToggles[key].checked = value;
}
});
// Update stats
const filteredCount = result.filteredPosts?.length || 0;
const cachedCount = Object.keys(result.processedPosts || {}).length;
filteredCountDiv.textContent = `${filteredCount} (${cachedCount} cached)`;
showFilteredButton.style.display = filteredCount > 0 ? 'block' : 'none';
});
// Save filter settings when changed
Object.entries(filterToggles).forEach(([key, toggle]) => {
toggle.addEventListener('change', () => {
chrome.storage.local.get(['filterSettings'], (result) => {
const currentSettings = result.filterSettings || {};
const newSettings = {
...currentSettings,
[key]: toggle.checked
};
chrome.storage.local.set({ filterSettings: newSettings }, () => {
if (chrome.runtime.lastError) {
console.error('❌ Error saving filter settings:', chrome.runtime.lastError);
showStatus('Error saving filter settings!', 'error');
} else {
console.log('✅ Filter settings saved:', newSettings);
showStatus(`${key} filter ${toggle.checked ? 'enabled' : 'disabled'}!`, 'success');
}
});
});
});
});
// Show filtered posts
showFilteredButton.addEventListener('click', async () => {
console.log('🔍 Showing filtered posts');
// Get the active tab
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
// Send message to content script to show filtered posts
chrome.tabs.sendMessage(tab.id, { action: 'showFilteredPosts' }, (response) => {
if (chrome.runtime.lastError) {
console.error('❌ Error showing filtered posts:', chrome.runtime.lastError);
showStatus('Error showing filtered posts!', 'error');
} else {
console.log('✅ Showing filtered posts');
showStatus('Showing filtered posts!', 'success');
}
});
});
// Save API key when changed
apiKeyInput.addEventListener('input', () => {
const apiKey = apiKeyInput.value.trim();
console.log('💾 Saving API key:', apiKey ? '(key provided)' : '(empty)');
chrome.storage.local.set({ apiKey }, () => {
if (chrome.runtime.lastError) {
console.error('❌ Error saving API key:', chrome.runtime.lastError);
showStatus('Error saving API key!', 'error');
} else {
console.log('✅ API key saved successfully');
showStatus('API key saved!', 'success');
}
});
});
// Toggle extension
enabledToggle.addEventListener('change', () => {
const isEnabled = enabledToggle.checked;
console.log('🔄 Toggling extension:', isEnabled ? 'enabled' : 'disabled');
chrome.storage.local.set({ isEnabled }, () => {
if (chrome.runtime.lastError) {
console.error('❌ Error saving toggle state:', chrome.runtime.lastError);
showStatus('Error saving settings!', 'error');
} else {
console.log('✅ Toggle state saved successfully');
showStatus(
isEnabled ? 'Filter enabled!' : 'Filter disabled!',
'success'
);
}
});
});
// Save blur mode setting when changed
blurModeToggle.addEventListener('change', () => {
chrome.storage.local.set({ blurMode: blurModeToggle.checked }, () => {
if (chrome.runtime.lastError) {
console.error('❌ Error saving blur mode setting:', chrome.runtime.lastError);
showStatus('Error saving blur mode setting!', 'error');
} else {
console.log('✅ Blur mode setting saved:', blurModeToggle.checked);
showStatus(`Blur mode ${blurModeToggle.checked ? 'enabled' : 'disabled'}!`, 'success');
// Notify content script of the change
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, {
action: 'updateBlurMode',
blurMode: blurModeToggle.checked
});
}
});
}
});
});
// Helper function to show status messages
function showStatus(message, type) {
console.log(`📢 Status message (${type}):`, message);
statusDiv.textContent = message;
statusDiv.className = `status ${type}`;
statusDiv.style.display = 'block';
setTimeout(() => {
statusDiv.style.display = 'none';
}, 3000);
}
// Clear cache function
function clearCache() {
chrome.storage.local.set({ processedPosts: {} }, () => {
if (chrome.runtime.lastError) {
console.error('❌ Error clearing cache:', chrome.runtime.lastError);
showStatus('Error clearing cache!', 'error');
} else {
console.log('✅ Cache cleared');
showStatus('Cache cleared!', 'success');
// Update stats
chrome.storage.local.get(['filteredPosts', 'processedPosts'], (result) => {
const filteredCount = result.filteredPosts?.length || 0;
const cachedCount = Object.keys(result.processedPosts || {}).length;
filteredCountDiv.textContent = `${filteredCount} (${cachedCount} cached)`;
});
}
});
}
// Add clear cache button event listener
const clearCacheButton = document.getElementById('clearCache');
if (clearCacheButton) {
clearCacheButton.addEventListener('click', clearCache);
}
// Add view stats button event listener
const viewStatsButton = document.getElementById('viewStats');
if (viewStatsButton) {
viewStatsButton.addEventListener('click', () => {
chrome.tabs.create({ url: 'stats.html' });
});
}
});