From 0076e32ada1852afd4e941d6923584a1e557cb60 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 27 Dec 2023 20:38:23 +0100 Subject: [PATCH] Ensure blocking mode timer is started even when set via a third-party API call Signed-off-by: DL6ER --- scripts/pi-hole/js/footer.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/scripts/pi-hole/js/footer.js b/scripts/pi-hole/js/footer.js index d85f6fe7..39239b5c 100644 --- a/scripts/pi-hole/js/footer.js +++ b/scripts/pi-hole/js/footer.js @@ -39,10 +39,16 @@ function secondsTimeSpanToHMS(s) { return h + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s); //zero padding on minutes and seconds } -function piholeChanged(blocking) { - var status = $("#status"); - var ena = $("#pihole-enable"); - var dis = $("#pihole-disable"); +function piholeChanged(blocking, timer = null) { + const status = $("#status"); + const ena = $("#pihole-enable"); + const dis = $("#pihole-disable"); + const enaT = $("#enableTimer"); + + if (timer !== null && parseFloat(timer) > 0) { + enaT.html(Date.now() + parseFloat(timer) * 1000); + setTimeout(countDown, 100); + } switch (blocking) { case "enabled": { @@ -83,7 +89,7 @@ function piholeChanged(blocking) { function countDown() { var ena = $("#enableLabel"); var enaT = $("#enableTimer"); - var target = new Date(parseInt(enaT.html(), 10)); + var target = new Date(parseInt(enaT.text(), 10)); var seconds = Math.round((target.getTime() - Date.now()) / 1000); //Stop and remove timer when user enabled early @@ -97,7 +103,7 @@ function countDown() { ena.text("Enable Blocking (" + secondsTimeSpanToHMS(seconds) + ")"); } else { ena.text("Enable Blocking"); - piholeChanged("enabled"); + piholeChanged("enabled", null); if (localStorage) { localStorage.removeItem("countDownTarget"); } @@ -116,7 +122,7 @@ function checkBlocking() { method: "GET", }) .done(function (data) { - piholeChanged(data.blocking); + piholeChanged(data.blocking, data.timer); utils.setTimer(checkBlocking, REFRESH_INTERVAL.blocking); }) .fail(function (data) { @@ -126,8 +132,7 @@ function checkBlocking() { } function piholeChange(action, duration) { - var enaT = $("#enableTimer"); - var btnStatus; + let btnStatus = null; switch (action) { case "enable": @@ -155,11 +160,7 @@ function piholeChange(action, duration) { .done(function (data) { if (data.blocking === action + "d") { btnStatus.html(""); - piholeChanged(data.blocking); - if (duration > 0) { - enaT.html(Date.now() + duration * 1000); - setTimeout(countDown, 100); - } + piholeChanged(data.blocking, data.timer); } }) .fail(function (data) {