From 7c7f48206e17ebe0dbc20eafa61300668d127a52 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 7 May 2023 13:11:23 +0200 Subject: [PATCH] Ported search.lp (was queryads.php) Signed-off-by: DL6ER --- scripts/pi-hole/js/queryads.js | 87 --------------------- scripts/pi-hole/js/search.js | 139 +++++++++++++++++++++++++++++++++ scripts/pi-hole/lua/sidebar.lp | 4 +- queryads.php => search.lp | 21 +++-- 4 files changed, 151 insertions(+), 100 deletions(-) delete mode 100644 scripts/pi-hole/js/queryads.js create mode 100644 scripts/pi-hole/js/search.js rename queryads.php => search.lp (67%) diff --git a/scripts/pi-hole/js/queryads.js b/scripts/pi-hole/js/queryads.js deleted file mode 100644 index 000c9786..00000000 --- a/scripts/pi-hole/js/queryads.js +++ /dev/null @@ -1,87 +0,0 @@ -/* Pi-hole: A black hole for Internet advertisements - * (c) 2017 Pi-hole, LLC (https://pi-hole.net) - * Network-wide ad blocking via your own hardware. - * - * This file is copyright under the latest version of the EUPL. - * Please see LICENSE file for your rights under this license. */ - -var exact = ""; -var showAll = ""; - -function eventsource() { - var ta = $("#output"); - // process with the current visible domain input field - var domain = $("input[id^='domain']:visible").val().trim().toLowerCase(); - var unlimited = $("#show-all").is(":checked"); - - if (domain.length === 0) { - return; - } - - if (unlimited === true) { - showAll = "&showall"; - } - - var queryURL = "scripts/pi-hole/php/queryads.php?domain=" + domain + exact + showAll; - - // IE does not support EventSource - load whole content at once - if (typeof EventSource !== "function") { - $.ajax({ - method: "GET", - url: queryURL + "&IE", - async: false, - }).done(function (data) { - ta.show(); - ta.empty(); - ta.append(data); - }); - return; - } - - var source = new EventSource(queryURL); - - // Reset and show field - ta.empty(); - ta.show(); - - source.addEventListener( - "message", - function (e) { - ta.append(e.data); - }, - false - ); - - // Will be called when script has finished - source.addEventListener( - "error", - function () { - source.close(); - }, - false - ); - - // Reset option variables - exact = ""; - showAll = ""; -} - -// Handle enter key -$("#domain").on("keypress", function (e) { - if (e.which === 13) { - // Enter was pressed, and the input has focus - exact = ""; - eventsource(); - } -}); - -// Handle search buttons -$("button[id^='btnSearch']").on("click", function () { - exact = ""; - - if (this.id.match("^btnSearchExact")) { - exact = "&exact"; - } - - eventsource(); -}); diff --git a/scripts/pi-hole/js/search.js b/scripts/pi-hole/js/search.js new file mode 100644 index 00000000..7cb5ce77 --- /dev/null +++ b/scripts/pi-hole/js/search.js @@ -0,0 +1,139 @@ +/* Pi-hole: A black hole for Internet advertisements + * (c) 2017 Pi-hole, LLC (https://pi-hole.net) + * Network-wide ad blocking via your own hardware. + * + * This file is copyright under the latest version of the EUPL. + * Please see LICENSE file for your rights under this license. */ + +/* global utils:false, apiFailure:false */ + +function eventsource(partial) { + const ta = $("#output"); + // process with the current visible domain input field + const q = $("input[id^='domain']:visible").val().trim().toLowerCase(); + const N = $("#number").val(); + + if (q.length === 0) { + return; + } + + var verb = partial ? "partially" : "exactly"; + + $.ajax({ + method: "GET", + url: "/api/search/" + encodeURIComponent(q), + async: false, + data: { + partial: partial, + N: N, + }, + }) + .done(function (data) { + ta.empty(); + ta.show(); + + const res = data.search; + var result = ""; + if (res.domains.length > 0) { + result = + "Found " + + res.domains.length + + " domain(s) " + + verb + + " matching '" + + utils.escapeHtml(q) + + "':

"; + for (const domain of res.domains) { + result += + " - " + + utils.escapeHtml(domain.domain) + + "
" + + domain.kind + + " " + + domain.type + + " domain
added " + + utils.renderTimestamp(domain.date_added, "display") + + "
last modified " + + utils.renderTimestamp(domain.date_modified, "display") + + "
" + + (domain.enabled ? "enabled" : "disabled") + + ", used in " + + domain.groups.length + + " group" + + (domain.groups.length === 1 ? "" : "s") + + (domain.comment !== null && domain.comment.length > 0 + ? '
comment: "' + utils.escapeHtml(domain.comment) + '"' + : "
no comment") + + ")

"; + } + } + + if (res.gravity.length > 0) { + result = + "Found " + + res.gravity.length + + " adlists " + + verb + + " matching '" + + utils.escapeHtml(q) + + "':

"; + for (const adlist of res.gravity) { + result += + " - " + + utils.escapeHtml(adlist.domain) + + "" + + "
" + + utils.escapeHtml(adlist.address) + + "
added " + + utils.renderTimestamp(adlist.date_added, "display") + + "
last modified " + + utils.renderTimestamp(adlist.date_modified, "display") + + "
last updated " + + utils.renderTimestamp(adlist.date_updated, "display") + + " (" + + adlist.number.toLocaleString() + + " domains)" + + "
" + + (adlist.enabled ? "enabled" : "disabled") + + ", used in " + + adlist.groups.length + + " group" + + (adlist.groups.length === 1 ? "" : "s") + + (adlist.comment !== null && adlist.comment.length > 0 + ? '
comment: "' + utils.escapeHtml(adlist.comment) + '"' + : "
no comment") + + "

"; + } + } + + result += "Search took " + (1000 * data.took).toFixed(1) + " ms"; + + ta.append(result); + }) + .fail(function (data) { + apiFailure(data); + }); +} + +// Handle enter key +$("#domain").on("keypress", function (e) { + if (e.which === 13) { + // Enter was pressed, and the input has focus + eventsource(false); + } +}); + +// Handle search buttons +$("button[id^='btnSearch']").on("click", function () { + var partial = false; + + if (!this.id.match("^btnSearchExact")) { + partial = true; + } + + eventsource(partial); +}); diff --git a/scripts/pi-hole/lua/sidebar.lp b/scripts/pi-hole/lua/sidebar.lp index 168ca262..6412c37c 100644 --- a/scripts/pi-hole/lua/sidebar.lp +++ b/scripts/pi-hole/lua/sidebar.lp @@ -222,8 +222,8 @@ -
  • "> - +
  • "> + Search Adlists
  • diff --git a/queryads.php b/search.lp similarity index 67% rename from queryads.php rename to search.lp index 0ce64d5c..88d2a41a 100644 --- a/queryads.php +++ b/search.lp @@ -1,12 +1,11 @@ - @@ -31,8 +30,8 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    - - + +
    @@ -41,5 +40,5 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r') - +