-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
76 lines (65 loc) · 2.93 KB
/
Copy pathpopup.js
File metadata and controls
76 lines (65 loc) · 2.93 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
// Логика всплывающего окна расширения
// Форматирование времени из секунд в MM:SS
function fmt(sec) {
sec = Math.max(0, Math.floor(sec));
const m = Math.floor(sec / 60), s = sec % 60;
return `${m}:${s.toString().padStart(2,"0")}`;
}
// Загрузка и отображение текущего состояния
async function loadAndRender() {
const infoEl = document.getElementById("info");
const limitEl = document.getElementById("limitMin");
const blockEl = document.getElementById("blockMin");
try {
// Запрашиваем текущее состояние у background скрипта
const res = await browser.runtime.sendMessage({ type: "getState" });
if (!res || !res.state || !res.settings) {
infoEl.textContent = "Ошибка связи";
return;
}
const { state, settings, now } = res;
// Инициализация полей ввода если они пустые
if (!limitEl.value) limitEl.value = Math.max(1, Math.round(settings.limitSec / 60));
if (!blockEl.value) blockEl.value = Math.max(1, Math.round(settings.blockSec / 60));
// Отображение текущего состояния
if (state.blockedUntil && now < state.blockedUntil) {
const remain = Math.ceil((state.blockedUntil - now) / 1000);
infoEl.textContent = "Блокировка: " + fmt(remain);
} else {
infoEl.textContent = "Осталось: " + fmt(state.remaining);
}
} catch (e) {
infoEl.textContent = "Ошибка связи";
console.error(e);
}
}
// Обработчик сохранения настроек
document.getElementById("saveBtn").addEventListener("click", async () => {
const limitMin = Math.max(1, parseInt(document.getElementById("limitMin").value || "30", 10));
const blockMin = Math.max(1, parseInt(document.getElementById("blockMin").value || "30", 10));
try {
await browser.runtime.sendMessage({
type: "setSettings",
limitSec: limitMin * 60,
blockSec: blockMin * 60
});
} catch (e) { console.error(e); }
loadAndRender();
});
// Обработчик сброса таймера
document.getElementById("resetBtn").addEventListener("click", async () => {
try { await browser.runtime.sendMessage({ type: "resetTimer" }); } catch (e) {}
loadAndRender();
});
// Обработчик принудительного запуска блокировки
document.getElementById("startBlockBtn").addEventListener("click", async () => {
try { await browser.runtime.sendMessage({ type: "startBlock" }); } catch (e) {}
loadAndRender();
});
// Обработчик принудительного снятия блокировки
document.getElementById("stopBlockBtn").addEventListener("click", async () => {
try { await browser.runtime.sendMessage({ type: "stopBlock" }); } catch (e) {}
loadAndRender();
});
setInterval(loadAndRender, 1000);
loadAndRender();