-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
245 lines (218 loc) · 7.45 KB
/
popup.js
File metadata and controls
245 lines (218 loc) · 7.45 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
// Default settings
const defaultSettings = {
categories: {
showIDE: true,
showDeepWiki: true,
showDiagram: true,
showGitingest: true
},
ideOptions: {
showVSCode: true,
showGithubDev: true,
showCodeSandbox: true,
showStackBlitz: true,
showGitpod: true
}
};
// Load settings when popup opens
document.addEventListener('DOMContentLoaded', () => {
// Show loading state
showLoading(true);
// First ensure default settings exist
chrome.storage.sync.get(defaultSettings, (settings) => {
if (chrome.runtime.lastError) {
console.error('Failed to load settings:', chrome.runtime.lastError);
showError('Failed to load settings. Please try again.');
showLoading(false);
return;
}
// Merge with defaults to ensure all properties exist
const mergedSettings = {
categories: { ...defaultSettings.categories, ...settings.categories },
ideOptions: { ...defaultSettings.ideOptions, ...settings.ideOptions }
};
// Save merged settings back if they were incomplete
if (JSON.stringify(settings) !== JSON.stringify(mergedSettings)) {
chrome.storage.sync.set(mergedSettings, () => {
if (chrome.runtime.lastError) {
console.error('Failed to save merged settings:', chrome.runtime.lastError);
showError('Failed to save settings. Please try again.');
showLoading(false);
return;
}
});
}
// Set category toggles
Object.keys(mergedSettings.categories).forEach(id => {
const checkbox = document.getElementById(id);
if (checkbox) {
checkbox.checked = mergedSettings.categories[id];
checkbox.addEventListener('change', () => updateSettings());
} else {
console.warn(`Checkbox with id ${id} not found in popup.html`);
}
});
// Set IDE option toggles
Object.keys(mergedSettings.ideOptions).forEach(id => {
const checkbox = document.getElementById(id);
if (checkbox) {
checkbox.checked = mergedSettings.ideOptions[id];
checkbox.addEventListener('change', () => updateSettings());
} else {
console.warn(`Checkbox with id ${id} not found in popup.html`);
}
});
// Add reset button listener
const resetButton = document.getElementById('resetButton');
if (resetButton) {
resetButton.addEventListener('click', resetToDefaults);
}
// Notify content script to update buttons immediately
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0] && tabs[0].url?.includes('github.com')) {
chrome.tabs.sendMessage(tabs[0].id, {
type: 'SETTINGS_UPDATED',
settings: mergedSettings
}, () => {
// Ignore errors if content script isn't ready
if (chrome.runtime.lastError) {
console.log('Content script not ready yet, settings will be applied on next page load');
}
});
}
});
// Hide loading state
showLoading(false);
});
});
// Show/hide loading state
function showLoading(show) {
const spinner = document.getElementById('loadingSpinner');
const body = document.body;
if (show) {
spinner.style.display = 'block';
body.style.opacity = '0.5';
} else {
spinner.style.display = 'none';
body.style.opacity = '1';
}
}
// Reset settings to defaults
function resetToDefaults() {
if (!confirm('Reset all settings to defaults? This cannot be undone.')) {
return;
}
showLoading(true);
chrome.storage.sync.set(defaultSettings, () => {
if (chrome.runtime.lastError) {
console.error('Failed to reset settings:', chrome.runtime.lastError);
showError('Failed to reset settings. Please try again.');
showLoading(false);
return;
}
// Update checkboxes to reflect defaults
Object.keys(defaultSettings.categories).forEach(id => {
const checkbox = document.getElementById(id);
if (checkbox) {
checkbox.checked = defaultSettings.categories[id];
}
});
Object.keys(defaultSettings.ideOptions).forEach(id => {
const checkbox = document.getElementById(id);
if (checkbox) {
checkbox.checked = defaultSettings.ideOptions[id];
}
});
showSuccess('Settings reset to defaults!');
// Notify content script
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0] && tabs[0].url?.includes('github.com')) {
chrome.tabs.sendMessage(tabs[0].id, {
type: 'SETTINGS_UPDATED',
settings: defaultSettings
}, () => {
if (chrome.runtime.lastError) {
console.log('Content script not ready yet, settings will be applied on next page load');
}
});
}
});
showLoading(false);
});
}
// Show error message to user
function showError(message) {
const errorDiv = document.createElement('div');
errorDiv.style.cssText = `
color: #d73a49;
background: #ffeef0;
border: 1px solid #fdb8c0;
border-radius: 4px;
padding: 8px;
margin: 8px 0;
font-size: 12px;
`;
errorDiv.textContent = message;
document.body.insertBefore(errorDiv, document.body.firstChild);
// Remove after 5 seconds
setTimeout(() => errorDiv.remove(), 5000);
}
// Update settings when checkboxes change
function updateSettings() {
const settings = {
categories: {
showIDE: document.getElementById('showIDE')?.checked ?? false,
showDeepWiki: document.getElementById('showDeepWiki')?.checked ?? false,
showDiagram: document.getElementById('showDiagram')?.checked ?? false,
showGitingest: document.getElementById('showGitingest')?.checked ?? false
},
ideOptions: {
showVSCode: document.getElementById('showVSCode')?.checked ?? false,
showGithubDev: document.getElementById('showGithubDev')?.checked ?? false,
showCodeSandbox: document.getElementById('showCodeSandbox')?.checked ?? false,
showStackBlitz: document.getElementById('showStackBlitz')?.checked ?? false,
showGitpod: document.getElementById('showGitpod')?.checked ?? false
}
};
// Save settings with error handling
chrome.storage.sync.set(settings, () => {
if (chrome.runtime.lastError) {
console.error('Failed to save settings:', chrome.runtime.lastError);
showError('Failed to save settings. Please try again.');
return;
}
// Show success feedback
showSuccess('Settings saved!');
// Notify content script to update buttons
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0] && tabs[0].url?.includes('github.com')) {
chrome.tabs.sendMessage(tabs[0].id, {
type: 'SETTINGS_UPDATED',
settings: settings
}, () => {
// Ignore errors if content script isn't ready
if (chrome.runtime.lastError) {
console.log('Content script not ready yet, settings will be applied on next page load');
}
});
}
});
});
}
// Show success message to user
function showSuccess(message) {
const successDiv = document.createElement('div');
successDiv.style.cssText = `
color: #28a745;
background: #f0fff4;
border: 1px solid #c3e6cb;
border-radius: 4px;
padding: 8px;
margin: 8px 0;
font-size: 12px;
`;
successDiv.textContent = message;
document.body.insertBefore(successDiv, document.body.firstChild);
// Remove after 2 seconds
setTimeout(() => successDiv.remove(), 2000);
}