-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
266 lines (227 loc) · 8.02 KB
/
Copy pathpopup.js
File metadata and controls
266 lines (227 loc) · 8.02 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
// popup.js - Handles popup UI logic
console.log("Popup script loaded");
// DOM Elements
const saveBtn = document.getElementById('saveBtn');
const exportBtn = document.getElementById('exportBtn');
const clearBtn = document.getElementById('clearBtn');
const autoSaveToggle = document.getElementById('autoSaveToggle');
const formatSelect = document.getElementById('formatSelect');
const crateDetails = document.getElementById('crateDetails');
const savedCount = document.getElementById('savedCount');
const lastSaved = document.getElementById('lastSaved');
const status = document.getElementById('status');
let currentCrate = null;
let savedCrates = [];
// Update status display
function updateStatus(message, isError = false, timeout = 3000) {
status.textContent = message;
status.className = 'status ' + (isError ? 'error' : 'success');
if (timeout > 0) {
setTimeout(() => {
status.textContent = 'Ready';
status.className = 'status';
}, timeout);
}
}
// Show loader in status
function showLoader(message = 'Loading...') {
status.innerHTML = `<div class="loader"></div>`;
status.className = 'status';
}
// Get current tab's crate info
async function getCurrentCrateInfo() {
try {
// Get the active tab
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
const currentTab = tabs[0];
// Check if we're on crates.io
if (!currentTab.url.includes('crates.io')) {
crateDetails.textContent = 'Not on crates.io. Visit a crate page to save.';
saveBtn.disabled = true;
return null;
}
// Send message to content script to get crate info
const response = await browser.tabs.sendMessage(currentTab.id, {
action: "getCurrentCrate"
});
if (response && response.success && response.crate) {
currentCrate = response.crate;
updateCrateDisplay(response.crate);
saveBtn.disabled = false;
saveBtn.textContent = `Save ${response.crate.name}`;
return response.crate;
} else {
crateDetails.textContent = 'Not on a crate page. Visit crates.io/crates/* to save.';
saveBtn.disabled = true;
return null;
}
} catch (error) {
console.error("Error getting crate info:", error);
crateDetails.textContent = 'Error loading crate info. Refresh the page.';
saveBtn.disabled = true;
return null;
}
}
// Update crate info display
function updateCrateDisplay(crate) {
crateDetails.innerHTML = `
<div><strong>Name:</strong> ${crate.name}</div>
<div><strong>Version:</strong> ${crate.version}</div>
<div><strong>Description:</strong> ${crate.description || 'N/A'}</div>
<div><strong>URL:</strong> ${crate.url}</div>
`;
}
// Load saved crates count
async function loadSavedCrates() {
try {
const response = await browser.runtime.sendMessage({
action: "getSavedCrates"
});
if (response.success) {
savedCrates = response.crates;
savedCount.textContent = savedCrates.length;
// Update last saved timestamp
if (savedCrates.length > 0) {
const last = savedCrates[savedCrates.length - 1];
const date = new Date(last.timestamp);
lastSaved.textContent = date.toLocaleDateString();
} else {
lastSaved.textContent = 'Never';
}
}
} catch (error) {
console.error("Error loading saved crates:", error);
}
}
// Load settings
async function loadSettings() {
try {
const response = await browser.runtime.sendMessage({
action: "getSettings"
});
if (response.success && response.settings) {
autoSaveToggle.checked = response.settings.autoSave !== false;
formatSelect.value = response.settings.saveFormat || 'toml';
}
} catch (error) {
console.error("Error loading settings:", error);
}
}
// Save current crate
async function saveCurrentCrate() {
if (!currentCrate) return;
showLoader('Saving crate...');
saveBtn.disabled = true;
try {
// Get current tab
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
const currentTab = tabs[0];
// Send save command to content script
const response = await browser.tabs.sendMessage(currentTab.id, {
action: "saveCurrentCrate"
});
if (response && response.success) {
updateStatus(`Saved ${currentCrate.name}@${currentCrate.version}`, false);
// Reload saved count
await loadSavedCrates();
// Update button text if still same crate
saveBtn.textContent = `Update ${currentCrate.name}`;
} else {
updateStatus('Failed to save crate', true);
}
} catch (error) {
console.error("Save error:", error);
updateStatus('Error saving crate', true);
} finally {
saveBtn.disabled = false;
}
}
// Export all crates
async function exportAllCrates() {
showLoader('Exporting...');
exportBtn.disabled = true;
try {
const format = formatSelect.value;
const response = await browser.runtime.sendMessage({
action: "exportCrates",
format: format
});
if (response.success) {
updateStatus(`Exported ${response.count} crates to ${response.filename}`, false);
} else {
updateStatus(`Export failed: ${response.error}`, true);
}
} catch (error) {
console.error("Export error:", error);
updateStatus('Export failed', true);
} finally {
exportBtn.disabled = false;
}
}
// Clear all saved crates
async function clearAllCrates() {
if (!confirm('Are you sure you want to clear all saved crates?')) {
return;
}
showLoader('Clearing...');
clearBtn.disabled = true;
try {
const response = await browser.runtime.sendMessage({
action: "clearCrates"
});
if (response.success) {
updateStatus('Cleared all saved crates', false);
savedCrates = [];
savedCount.textContent = '0';
lastSaved.textContent = 'Never';
} else {
updateStatus('Failed to clear crates', true);
}
} catch (error) {
console.error("Clear error:", error);
updateStatus('Error clearing crates', true);
} finally {
clearBtn.disabled = false;
}
}
// Save settings
async function saveSettings() {
const settings = {
autoSave: autoSaveToggle.checked,
saveFormat: formatSelect.value
};
try {
await browser.runtime.sendMessage({
action: "saveSettings",
settings: settings
});
updateStatus('Settings saved', false, 1500);
} catch (error) {
console.error("Error saving settings:", error);
}
}
// Initialize popup
async function initializePopup() {
console.log("Initializing popup...");
// Load current crate info
await getCurrentCrateInfo();
// Load saved crates count
await loadSavedCrates();
// Load settings
await loadSettings();
console.log("Popup initialized");
}
// Event listeners
saveBtn.addEventListener('click', saveCurrentCrate);
exportBtn.addEventListener('click', exportAllCrates);
clearBtn.addEventListener('click', clearAllCrates);
autoSaveToggle.addEventListener('change', saveSettings);
formatSelect.addEventListener('change', saveSettings);
// Initialize when popup opens
document.addEventListener('DOMContentLoaded', initializePopup);
// Auto-refresh crate info every 2 seconds while popup is open
setInterval(async () => {
if (document.visibilityState === 'visible') {
await getCurrentCrateInfo();
}
}, 2000);