-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
142 lines (126 loc) · 4.3 KB
/
Copy pathpopup.js
File metadata and controls
142 lines (126 loc) · 4.3 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
/*
* CryptoDripster — © testguard.app (https://testguard.app)
* Developed and owned by testguard.app. Do not remove this notice (see LICENSE).
*/
const DEFAULTS = {
wallet: "", // no default recipient — the user must enter their own wallet
amount: "5",
intervalHours: 8,
keepTabOpen: false,
requestsPerRun: 2,
};
const ALARM_NAME = "faucetAlarm";
const $ = (id) => document.getElementById(id);
const WALLET_RE = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
function fmtTime(iso) {
const d = new Date(iso);
return d.toLocaleTimeString("en-GB", { hour: "2-digit", minute: "2-digit", second: "2-digit" });
}
// ===== Load settings into the form =====
async function loadConfig() {
const c = await chrome.storage.local.get([
"wallet",
"amount",
"intervalHours",
"keepTabOpen",
"requestsPerRun",
]);
$("wallet").value = c.wallet || DEFAULTS.wallet;
$("amount").value = c.amount || DEFAULTS.amount;
$("interval").value = c.intervalHours || DEFAULTS.intervalHours;
$("keepOpen").checked = c.keepTabOpen === true;
$("repeats").value = c.requestsPerRun || DEFAULTS.requestsPerRun;
}
// ===== Status =====
async function renderStatus() {
const data = await chrome.storage.local.get(["lastRun", "lastResult", "lastDetail"]);
const el = $("status");
let html = "";
if (data.lastRun) {
const ok = data.lastResult === "success";
const isErr = data.lastResult === "error";
const isCaptcha = data.lastResult === "captcha";
const cls = ok ? "ok" : isErr ? "err" : isCaptcha ? "warn" : "muted";
const icon = ok ? "✅" : isErr ? "⚠️" : isCaptcha ? "⏳" : "•";
const detail = data.lastDetail || data.lastResult || "result unknown";
html += `<span class="${cls}">${icon} ${escapeHtml(detail)}</span><br>`;
html += `<span class="muted">Last run: ${data.lastRun}</span>`;
} else {
html += `<span class="muted">⏳ Not run yet</span>`;
}
const alarm = await chrome.alarms.get(ALARM_NAME);
if (alarm && alarm.scheduledTime) {
const next = new Date(alarm.scheduledTime).toLocaleString("en-GB");
html += `<br><span class="muted">Next run: ${next}</span>`;
}
el.innerHTML = html;
}
// ===== Logs =====
async function renderLogs() {
const { logs = [] } = await chrome.storage.local.get("logs");
const box = $("logs");
if (!logs.length) {
box.innerHTML = '<div class="muted">No logs yet</div>';
return;
}
// newest on top
box.innerHTML = logs
.slice()
.reverse()
.map((l) => {
const cls = l.level === "success" ? "ok" : l.level === "error" ? "err" : "";
return `<div class="log-entry"><span class="log-time">${fmtTime(l.time)}</span> <span class="${cls}">${escapeHtml(
l.message
)}</span></div>`;
})
.join("");
}
function escapeHtml(s) {
return String(s).replace(/[&<>"]/g, (c) =>
({ "&": "&", "<": "<", ">": ">", '"': """ }[c])
);
}
function refresh() {
renderStatus();
renderLogs();
}
// ===== Actions =====
$("save").addEventListener("click", async () => {
const wallet = $("wallet").value.trim();
const amount = $("amount").value;
const intervalHours = Math.max(1, Math.min(168, parseInt($("interval").value, 10) || DEFAULTS.intervalHours));
const keepTabOpen = $("keepOpen").checked;
const requestsPerRun = Math.max(1, Math.min(5, parseInt($("repeats").value, 10) || DEFAULTS.requestsPerRun));
const hint = $("saveHint");
if (!WALLET_RE.test(wallet)) {
hint.className = "hint err";
hint.textContent = "✖ Invalid Solana address (expected 32–44 base58 chars)";
return;
}
await chrome.storage.local.set({ wallet, amount, intervalHours, keepTabOpen, requestsPerRun });
chrome.runtime.sendMessage({ action: "reschedule" });
hint.className = "hint ok";
hint.textContent = "✔ Saved";
setTimeout(() => {
hint.textContent = "";
refresh();
}, 1500);
});
$("runNow").addEventListener("click", () => {
chrome.runtime.sendMessage({ action: "runNow" });
const btn = $("runNow");
btn.textContent = "⏳ Running…";
setTimeout(() => {
btn.textContent = "▶ Run now";
refresh();
}, 2500);
});
$("clearLogs").addEventListener("click", async () => {
await chrome.storage.local.set({ logs: [] });
renderLogs();
});
// ===== Start =====
loadConfig();
refresh();
// auto-refresh status/logs while the popup is open
setInterval(refresh, 2000);