-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
108 lines (96 loc) · 3.36 KB
/
popup.js
File metadata and controls
108 lines (96 loc) · 3.36 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
// Cross-browser compatibility: Firefox uses browser.*, Chrome uses chrome.*
const browserAPI = typeof browser !== 'undefined' ? browser : chrome;
// DOM elements
const enabledToggle = document.getElementById('enabled-toggle');
const replacementCount = document.getElementById('replacement-count');
// Helper to handle both promise and callback-based storage APIs
function storageGet(storageArea, keys) {
return new Promise((resolve, reject) => {
const storage = browserAPI.storage[storageArea];
const result = storage.get(keys, (data) => {
if (browserAPI.runtime.lastError) {
reject(browserAPI.runtime.lastError);
} else {
resolve(data);
}
});
// If it returns a promise (Firefox), use that instead
if (result && typeof result.then === 'function') {
result.then(resolve).catch(reject);
}
});
}
function storageSet(storageArea, data) {
return new Promise((resolve, reject) => {
const storage = browserAPI.storage[storageArea];
const result = storage.set(data, () => {
if (browserAPI.runtime.lastError) {
reject(browserAPI.runtime.lastError);
} else {
resolve();
}
});
// If it returns a promise (Firefox), use that instead
if (result && typeof result.then === 'function') {
result.then(resolve).catch(reject);
}
});
}
// Initialize popup with current state
async function initializePopup() {
try {
// Load enabled state from sync storage
const syncData = await storageGet('sync', ['enabled']);
const isEnabled = syncData.enabled !== false; // Default to true
enabledToggle.checked = isEnabled;
// Load replacement count from local storage
const localData = await storageGet('local', ['replacementCount']);
const count = localData.replacementCount || 0;
updateReplacementCount(count);
} catch (error) {
console.error('Error initializing popup:', error);
// Set defaults on error
enabledToggle.checked = true;
updateReplacementCount(0);
}
}
// Update replacement count display
function updateReplacementCount(count) {
replacementCount.textContent = count.toLocaleString();
}
// Handle toggle change
enabledToggle.addEventListener('change', async (event) => {
const isEnabled = event.target.checked;
try {
// Save enabled state to sync storage
await storageSet('sync', { enabled: isEnabled });
// Send message to all tabs to update their state
browserAPI.tabs.query({}, (tabs) => {
for (const tab of tabs) {
browserAPI.tabs.sendMessage(tab.id, {
type: 'toggleState',
enabled: isEnabled
}).catch(() => {
// Ignore errors for tabs that don't have content script
});
}
});
} catch (error) {
console.error('Error toggling extension:', error);
// Revert toggle on error
enabledToggle.checked = !isEnabled;
}
});
// Listen for storage changes to update count in real-time
browserAPI.storage.onChanged.addListener((changes, areaName) => {
if (areaName === 'local' && changes.replacementCount) {
const newCount = changes.replacementCount.newValue || 0;
updateReplacementCount(newCount);
}
if (areaName === 'sync' && changes.enabled) {
const newEnabled = changes.enabled.newValue !== false;
enabledToggle.checked = newEnabled;
}
});
// Initialize on load
document.addEventListener('DOMContentLoaded', initializePopup);