diff --git a/auditlog.php b/auditlog.php index 4f105a70..33ed04ac 100644 --- a/auditlog.php +++ b/auditlog.php @@ -68,14 +68,9 @@ -
-

Important: Note that black- and whitelisted domains are not automatically applied on this page to avoid restarting the DNS service too often. Instead, click on this button, to have the new settings become effective:

- -
- diff --git a/scripts/pi-hole/js/auditlog.js b/scripts/pi-hole/js/auditlog.js index c016ac72..3850b5de 100644 --- a/scripts/pi-hole/js/auditlog.js +++ b/scripts/pi-hole/js/auditlog.js @@ -6,8 +6,7 @@ * Please see LICENSE file for your rights under this license. */ // Define global variables -var auditList = [], - auditTimeout; +var auditTimeout = null; // Credit: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406 function escapeHtml(text) { @@ -89,8 +88,14 @@ function updateTopLists() { $("#domain-frequency .overlay").hide(); $("#ad-frequency .overlay").hide(); - // Update top lists data every second - setTimeout(updateTopLists, 1000); + // Update top lists data every ten seconds + // Updates are also triggered by button actions + // and reset the running timer + if (auditTimeout !== null) { + window.clearTimeout(auditTimeout); + } + + auditTimeout = setTimeout(updateTopLists, 10000); }); } @@ -99,10 +104,32 @@ function add(domain, list) { $.ajax({ url: "scripts/pi-hole/php/add.php", method: "post", - data: { domain: domain, list: list, token: token } + data: { domain: domain, list: list, token: token }, + success: function() { + updateTopLists(); + }, + error: function(jqXHR, exception) { + console.log(exception); + } }); } +function blacklistUrl(url) { + // We add to audit last as it will reload the table on success + add(url, "black"); + add(url, "audit"); +} + +function whitelistUrl(url) { + // We add to audit last as it will reload the table on success + add(url, "white"); + add(url, "audit"); +} + +function auditUrl(url) { + add(url, "audit"); +} + $(document).ready(function() { // Pull in data via AJAX updateTopLists(); @@ -112,9 +139,7 @@ $(document).ready(function() { .parents("tr")[0] .textContent.split(" ")[0]; if ($(this).context.textContent === " Blacklist") { - add(url, "audit"); - add(url, "black"); - $("#gravityBtn").prop("disabled", false); + blacklistUrl(url); } else { auditUrl(url); } @@ -125,33 +150,9 @@ $(document).ready(function() { .parents("tr")[0] .textContent.split(" ")[0]; if ($(this).context.textContent === " Whitelist") { - add(url, "audit"); - add(url, "white"); - $("#gravityBtn").prop("disabled", false); + whitelistUrl(url); } else { auditUrl(url); } }); }); - -function auditUrl(url) { - if (auditList.indexOf(url) > -1) { - return; - } - - if (auditTimeout) { - clearTimeout(auditTimeout); - } - - auditList.push(url); - // wait 3 seconds to see if more domains need auditing - // and batch them all into a single request - auditTimeout = setTimeout(function() { - add(auditList.join(" "), "audit"); - auditList = []; - }, 3000); -} - -$("#gravityBtn").on("click", function() { - window.location.replace("gravity.php?go"); -}); diff --git a/scripts/pi-hole/php/add.php b/scripts/pi-hole/php/add.php index 941c80a4..878b28ff 100644 --- a/scripts/pi-hole/php/add.php +++ b/scripts/pi-hole/php/add.php @@ -47,6 +47,7 @@ require_once("database.php"); $GRAVITYDB = getGravityDBFilename(); $db = SQLite3_connect($GRAVITYDB, SQLITE3_OPEN_READWRITE); +$reload = true; switch($list) { case "white": $domains = array_map('strtolower', $domains); @@ -75,6 +76,7 @@ switch($list) { break; case "audit": + $reload = false; echo add_to_table($db, "domain_audit", $domains); break; @@ -83,6 +85,8 @@ switch($list) { } // Reload lists in pihole-FTL after having added something -echo shell_exec("sudo pihole restartdns reload"); +if ($reload) { + echo shell_exec("sudo pihole restartdns reload-lists"); +} ?>