Ported search.lp (was queryads.php)

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2023-05-07 13:11:23 +02:00
parent 3b9a677a1f
commit 7c7f48206e
4 changed files with 151 additions and 100 deletions

View File

@@ -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();
});

View File

@@ -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) <em>" +
verb +
"</em> matching '<strong class='text-blue'>" +
utils.escapeHtml(q) +
"</strong>':<br><br>";
for (const domain of res.domains) {
result +=
" - <a href='groups-domains.lp?domainid=" +
domain.id +
"' target='_blank'><strong class='text-green'>" +
utils.escapeHtml(domain.domain) +
"</strong></a><br> " +
domain.kind +
" " +
domain.type +
" domain<br> added " +
utils.renderTimestamp(domain.date_added, "display") +
"<br> last modified " +
utils.renderTimestamp(domain.date_modified, "display") +
"<br> " +
(domain.enabled ? "enabled" : "disabled") +
", used in " +
domain.groups.length +
" group" +
(domain.groups.length === 1 ? "" : "s") +
(domain.comment !== null && domain.comment.length > 0
? '<br> comment: "' + utils.escapeHtml(domain.comment) + '"'
: "<br> no comment") +
")<br><br>";
}
}
if (res.gravity.length > 0) {
result =
"Found " +
res.gravity.length +
" adlists <em>" +
verb +
"</em> matching '<strong class='text-blue'>" +
utils.escapeHtml(q) +
"</strong>':<br><br>";
for (const adlist of res.gravity) {
result +=
" - <strong class='text-green'>" +
utils.escapeHtml(adlist.domain) +
"</strong>" +
"<br> <a href='groups-adlists.lp?adlistid=" +
adlist.id +
"' target='_blank'>" +
utils.escapeHtml(adlist.address) +
"</a><br> added " +
utils.renderTimestamp(adlist.date_added, "display") +
"<br> last modified " +
utils.renderTimestamp(adlist.date_modified, "display") +
"<br> last updated " +
utils.renderTimestamp(adlist.date_updated, "display") +
" (" +
adlist.number.toLocaleString() +
" domains)" +
"<br> " +
(adlist.enabled ? "enabled" : "disabled") +
", used in " +
adlist.groups.length +
" group" +
(adlist.groups.length === 1 ? "" : "s") +
(adlist.comment !== null && adlist.comment.length > 0
? '<br> comment: "' + utils.escapeHtml(adlist.comment) + '"'
: "<br> no comment") +
"<br><br>";
}
}
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);
});

View File

@@ -222,8 +222,8 @@
</ul> </ul>
</li> </li>
<!-- Query Lists --> <!-- Query Lists -->
<li class="<? if scriptname == 'queryads.lp' then mg.write(" active") end ?>"> <li class="<? if scriptname == 'search.lp' then mg.write(" active") end ?>">
<a href="queryads.lp"> <a href="search.lp">
<i class="fa fa-fw menu-icon fa-search"></i> <span class="text-red">Search Adlists</span> <i class="fa fa-fw menu-icon fa-search"></i> <span class="text-red">Search Adlists</span>
</a> </a>
</li> </li>

View File

@@ -1,12 +1,11 @@
<?php <? --[[
/* * Pi-hole: A black hole for Internet advertisements
* Pi-hole: A black hole for Internet advertisements * (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* (c) 2017 Pi-hole, LLC (https://pi-hole.net) * Network-wide ad blocking via your own hardware.
* Network-wide ad blocking via your own hardware.
* *
* This file is copyright under the latest version of the EUPL. * This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. * Please see LICENSE file for your rights under this license.
*/ --]]
mg.include('scripts/pi-hole/lua/header_authenticated.lp','r') mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
?> ?>
@@ -31,8 +30,8 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
<div class="box-body"> <div class="box-body">
<!-- /domain-search-block --> <!-- /domain-search-block -->
<div id="limitbox-block"> <div id="limitbox-block">
<input type="checkbox" id="show-all"> <input type="number" min="0" value="20" id="number">
<label for="show-all"><strong>Show unlimited results.</strong> <br class="hidden-md hidden-lg">This can be very slow if too many domains are returned. <span class="text-red">Use with caution</span>.</label></i> <label for="number"><strong>Maximum of results to be returned</label></i>
</div> </div>
</div> </div>
</div> </div>
@@ -41,5 +40,5 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
<pre id="output" style="width: 100%; height: 100%;" hidden></pre> <pre id="output" style="width: 100%; height: 100%;" hidden></pre>
<script src="<?=pihole.fileversion('scripts/pi-hole/js/queryads.js')?>"></script> <script src="<?=pihole.fileversion('scripts/pi-hole/js/search.js')?>"></script>
<? mg.include('scripts/pi-hole/lua/footer.lp','r')?> <? mg.include('scripts/pi-hole/lua/footer.lp','r')?>