-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
62 lines (54 loc) · 1.91 KB
/
Copy pathpopup.js
File metadata and controls
62 lines (54 loc) · 1.91 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
const CURRENCY_SYMBOLS = {
USD: "$",
EUR: "€",
GBP: "£",
CAD: "CA$",
AUD: "A$",
JPY: "¥",
CNY: "¥",
INR: "₹",
};
document.addEventListener("DOMContentLoaded", () => {
const toggle = document.getElementById("toggle");
const status = document.getElementById("status");
const settingsBtn = document.getElementById("settings-btn");
const blockedEl = document.getElementById("blocked-count");
const savedEl = document.getElementById("money-saved");
let currency = "USD";
let lastStats = null;
function renderStats(stats) {
lastStats = stats || {};
const sym = CURRENCY_SYMBOLS[currency] || "$";
blockedEl.textContent = (lastStats.blocked ?? 0).toLocaleString();
savedEl.textContent = sym + Math.round(lastStats.saved ?? 0).toLocaleString();
}
chrome.storage.sync.get({ enabled: true, currency: "USD" }, (data) => {
currency = data.currency || "USD";
toggle.checked = data.enabled;
status.textContent = data.enabled ? "Locking buttons" : "Paused";
chrome.storage.local.get("stats", ({ stats }) => renderStats(stats));
});
chrome.storage.onChanged.addListener((changes, area) => {
if (area === "local" && changes.stats) {
renderStats(changes.stats.newValue);
}
if (area === "sync" && changes.currency) {
currency = changes.currency.newValue || "USD";
renderStats(lastStats);
}
});
toggle.addEventListener("change", () => {
const enabled = toggle.checked;
chrome.storage.sync.set({ enabled });
status.textContent = enabled ? "Locking buttons" : "Paused";
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]?.id) {
chrome.tabs.sendMessage(tabs[0].id, { type: "toggle", enabled });
}
});
});
settingsBtn.addEventListener("click", () => {
const ret = chrome.runtime.openOptionsPage?.();
if (ret && typeof ret.catch === "function") ret.catch(() => {});
});
});