-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
115 lines (98 loc) · 3.79 KB
/
Copy pathpopup.js
File metadata and controls
115 lines (98 loc) · 3.79 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
const DEFAULT_URL = "https://suno.com/create?wid=948afe35-3e9b-4592-8786-109a0345e85e";
const promptsEl = document.getElementById("prompts");
const targetUrlEl = document.getElementById("targetUrl");
const minIntervalEl = document.getElementById("minInterval");
const maxIntervalEl = document.getElementById("maxInterval");
const instrumentalEl = document.getElementById("instrumental");
const startBtn = document.getElementById("startBtn");
const nextBtn = document.getElementById("nextBtn");
const stopBtn = document.getElementById("stopBtn");
const resetBtn = document.getElementById("resetBtn");
const statusEl = document.getElementById("status");
const logEl = document.getElementById("log");
targetUrlEl.value = DEFAULT_URL;
function parsePrompts(raw) {
return raw
.split("\n")
.map((line) => line.trim().replace(/,\s*$/, ""))
.filter(Boolean);
}
function formatTime(ts) {
const d = new Date(ts);
return d.toLocaleTimeString("uk-UA", { hour: "2-digit", minute: "2-digit", second: "2-digit" });
}
function render(state) {
const running = !!state.running;
const busy = !!state.busy;
const hasRemaining = state.queue.length > 0 && state.index < state.queue.length;
startBtn.disabled = running || busy;
stopBtn.disabled = !running;
nextBtn.disabled = !hasRemaining || busy;
if (state.queue.length === 0) {
statusEl.textContent = "Черга порожня";
} else if (busy) {
statusEl.textContent = `Відправляю ${state.index + 1}/${state.queue.length}...`;
} else if (running && state.nextFireAt) {
statusEl.textContent = `${state.index}/${state.queue.length} відправлено. Наступний автоматично о ${formatTime(state.nextFireAt)}`;
} else if (running) {
statusEl.textContent = `Генерація... ${state.index}/${state.queue.length} відправлено`;
} else if (state.index >= state.queue.length) {
statusEl.textContent = `Готово: ${state.queue.length}/${state.queue.length}`;
} else {
statusEl.textContent = `Зупинено: ${state.index}/${state.queue.length} відправлено`;
}
logEl.innerHTML = "";
const entries = [...(state.log || [])].reverse();
for (const entry of entries) {
const li = document.createElement("li");
li.textContent = `${formatTime(entry.time)} — ${entry.message}`;
if (entry.level === "error") li.className = "error";
logEl.appendChild(li);
}
}
async function refresh() {
const state = await chrome.runtime.sendMessage({ type: "GET_STATE" });
if (state) render(state);
}
const MIN_SECONDS = 5;
function readIntervalRange() {
const min = Math.max(MIN_SECONDS, parseFloat(minIntervalEl.value) || MIN_SECONDS);
let max = Math.max(MIN_SECONDS, parseFloat(maxIntervalEl.value) || min);
if (max < min) max = min;
return { min, max };
}
startBtn.addEventListener("click", async () => {
const queue = parsePrompts(promptsEl.value);
if (queue.length === 0) {
alert("Встав хоча б один промпт у поле вище.");
return;
}
const { min, max } = readIntervalRange();
const targetUrl = targetUrlEl.value.trim() || DEFAULT_URL;
const instrumental = instrumentalEl.checked;
await chrome.runtime.sendMessage({
type: "START",
queue,
minIntervalSeconds: min,
maxIntervalSeconds: max,
targetUrl,
instrumental,
});
await refresh();
});
nextBtn.addEventListener("click", async () => {
nextBtn.disabled = true;
await chrome.runtime.sendMessage({ type: "NEXT" });
await refresh();
});
stopBtn.addEventListener("click", async () => {
await chrome.runtime.sendMessage({ type: "STOP" });
await refresh();
});
resetBtn.addEventListener("click", async () => {
await chrome.runtime.sendMessage({ type: "RESET" });
promptsEl.value = "";
await refresh();
});
refresh();
setInterval(refresh, 1000);