Skip to content

Commit

Permalink
Merge pull request #1073 from XhmikosR/js-2
Browse files Browse the repository at this point in the history
Assorted JS tweaks
  • Loading branch information
PromoFaux authored Dec 16, 2019
2 parents 6864aac + 9c18468 commit d966c7f
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 243 deletions.
17 changes: 8 additions & 9 deletions scripts/pi-hole/js/auditlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

// Define global variables
var timeLineChart, queryTypeChart, forwardDestinationChart, auditList = [], auditTimeout;
var auditList = [], auditTimeout;

// Credit: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406
function escapeHtml(text) {
Expand Down Expand Up @@ -35,7 +36,7 @@ function updateTopLists() {
var adtable = $("#ad-frequency").find("tbody:last");
var url, domain;
for (domain in data.top_queries) {
if ({}.hasOwnProperty.call(data.top_queries,domain)){
if (Object.prototype.hasOwnProperty.call(data.top_queries,domain)){
// Sanitize domain
domain = escapeHtml(domain);
url = "<a href=\"queries.php?domain="+domain+"\">"+domain+"</a>";
Expand All @@ -45,7 +46,7 @@ function updateTopLists() {
}

for (domain in data.top_ads) {
if ({}.hasOwnProperty.call(data.top_ads,domain)){
if (Object.prototype.hasOwnProperty.call(data.top_ads,domain)){
var input = domain.split(" ");
// Sanitize domain
var printdomain = escapeHtml(input[0]);
Expand All @@ -71,7 +72,6 @@ function updateTopLists() {
});
}


function add(domain,list) {
var token = $("#token").html();
$.ajax({
Expand All @@ -87,8 +87,8 @@ $(document).ready(function() {
updateTopLists();

$("#domain-frequency tbody").on( "click", "button", function () {
var url = ($(this).parents("tr"))[0].innerText.split(" ")[0];
if($(this).context.innerText === " Blacklist")
var url = ($(this).parents("tr"))[0].textContent.split(" ")[0];
if($(this).context.textContent === " Blacklist")
{
add(url,"audit");
add(url,"black");
Expand All @@ -101,8 +101,8 @@ $(document).ready(function() {
});

$("#ad-frequency tbody").on( "click", "button", function () {
var url = ($(this).parents("tr"))[0].innerText.split(" ")[0].split(" ")[0];
if($(this).context.innerText === " Whitelist")
var url = ($(this).parents("tr"))[0].textContent.split(" ")[0].split(" ")[0];
if($(this).context.textContent === " Whitelist")
{
add(url,"audit");
add(url,"white");
Expand Down Expand Up @@ -131,7 +131,6 @@ function auditUrl(url) {
}, 3000);
}


$("#gravityBtn").on("click", function() {
window.location.replace("gravity.php?go");
});
19 changes: 13 additions & 6 deletions scripts/pi-hole/js/customdns.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/* 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 table;

function showAlert(type, message)
Expand All @@ -7,10 +14,10 @@ function showAlert(type, message)

switch (type)
{
case 'info': alertElement = $('#alInfo'); break;
case 'success': alertElement = $('#alSuccess'); break;
case 'warning': alertElement = $('#alWarning'); messageElement = $('#warn'); break;
case 'error': alertElement = $('#alFailure'); messageElement = $('#err'); break;
case 'info': alertElement = $('#alInfo'); break;
case 'success': alertElement = $('#alSuccess'); break;
case 'warning': alertElement = $('#alWarning'); messageElement = $('#warn'); break;
case 'error': alertElement = $('#alFailure'); messageElement = $('#err'); break;
default: return;
}

Expand All @@ -35,7 +42,7 @@ $(document).ready(function() {
"</button>";
}
} ],
"drawCallback": function( settings ) {
"drawCallback": function() {
$('.deleteCustomDNS').on('click', deleteCustomDNS);
}
});
Expand All @@ -60,7 +67,7 @@ function addCustomDNS()
else
showAlert('error', response.message);
},
error: function(jqXHR, exception) {
error: function() {
showAlert('error', "Error while adding this custom DNS entry");
}
});
Expand Down
35 changes: 8 additions & 27 deletions scripts/pi-hole/js/db_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

/*global
moment
*/
/* global Chart:false, moment:false */

var start__ = moment().subtract(6, "days");
var from = moment(start__).utc().valueOf()/1000;
Expand Down Expand Up @@ -42,22 +40,8 @@ $(function () {
until = moment(endt).utc().valueOf()/1000;
});


});

// Credit: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406
function escapeHtml(text) {
var map = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
"\"": "&quot;",
"'": "&#039;"
};

return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}

function padNumber(num) {
return ("00" + num).substr(-2,2);
}
Expand Down Expand Up @@ -100,13 +84,13 @@ function updateQueriesOverTime() {
var dates = [], hour;

for (hour in data.domains_over_time[0]) {
if ({}.hasOwnProperty.call(data.domains_over_time[0], hour)) {
if (Object.prototype.hasOwnProperty.call(data.domains_over_time[0], hour)) {
dates.push(parseInt(data.domains_over_time[0][hour]));
}
}

for (hour in data.ads_over_time[0]) {
if ({}.hasOwnProperty.call(data.ads_over_time[0], hour)) {
if (Object.prototype.hasOwnProperty.call(data.ads_over_time[0], hour)) {
if(dates.indexOf(parseInt(data.ads_over_time[0][hour])) === -1)
{
dates.push(parseInt(data.ads_over_time[0][hour]));
Expand All @@ -118,7 +102,7 @@ function updateQueriesOverTime() {

// Add data for each hour that is available
for (hour in dates) {
if ({}.hasOwnProperty.call(dates, hour)) {
if (Object.prototype.hasOwnProperty.call(dates, hour)) {
var d, dom = 0, ads = 0;
d = new Date(1000*dates[hour]);

Expand Down Expand Up @@ -147,7 +131,6 @@ function updateQueriesOverTime() {
});
}

/* global Chart */
$(document).ready(function() {
var ctx = document.getElementById("queryOverTimeChart").getContext("2d");
timeLineChart = new Chart(ctx, {
Expand Down Expand Up @@ -187,7 +170,7 @@ $(document).ready(function() {
responsive: true,
mode: "x-axis",
callbacks: {
title: function(tooltipItem, data) {
title: function(tooltipItem) {
var label = tooltipItem[0].xLabel;
var time = new Date(label);
var date = time.getFullYear()+"-"+padNumber(time.getMonth()+1)+"-"+padNumber(time.getDate());
Expand All @@ -209,10 +192,8 @@ $(document).ready(function() {
}
return data.datasets[tooltipItems.datasetIndex].label + ": " + tooltipItems.yLabel + " (" + percentage.toFixed(1) + "%)";
}
else
{
return data.datasets[tooltipItems.datasetIndex].label + ": " + tooltipItems.yLabel;
}

return data.datasets[tooltipItems.datasetIndex].label + ": " + tooltipItems.yLabel;
}
}
},
Expand Down Expand Up @@ -257,7 +238,7 @@ $("#queryOverTimeChart").click(function(evt){
if(activePoints.length > 0)
{
//get the internal index in the chart
var clickedElementindex = activePoints[0]["_index"];
var clickedElementindex = activePoints[0]._index;

//get specific label by index
var label = timeLineChart.data.labels[clickedElementindex];
Expand Down
16 changes: 7 additions & 9 deletions scripts/pi-hole/js/db_lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

/*global
moment
*/
/* global moment:false */

var start__ = moment().subtract(6, "days");
var from = moment(start__).utc().valueOf()/1000;
Expand Down Expand Up @@ -67,14 +65,14 @@ function updateTopClientsChart() {
var client, percentage, clientname, clientip;
var sum = 0;
for (client in data.top_sources) {
if ({}.hasOwnProperty.call(data.top_sources, client)){
if (Object.prototype.hasOwnProperty.call(data.top_sources, client)){
sum += data.top_sources[client];
}
}

for (client in data.top_sources) {

if ({}.hasOwnProperty.call(data.top_sources, client)){
if (Object.prototype.hasOwnProperty.call(data.top_sources, client)){
// Sanitize client
client = escapeHtml(client);
if(escapeHtml(client) !== client)
Expand Down Expand Up @@ -121,14 +119,14 @@ function updateTopDomainsChart() {
var domain, percentage;
var sum = 0;
for (domain in data.top_domains) {
if ({}.hasOwnProperty.call(data.top_domains, domain)){
if (Object.prototype.hasOwnProperty.call(data.top_domains, domain)){
sum += data.top_domains[domain];
}
}

for (domain in data.top_domains) {

if ({}.hasOwnProperty.call(data.top_domains, domain)){
if (Object.prototype.hasOwnProperty.call(data.top_domains, domain)){
// Sanitize domain
domain = escapeHtml(domain);
if(escapeHtml(domain) !== domain)
Expand Down Expand Up @@ -163,14 +161,14 @@ function updateTopAdsChart() {
var ad, percentage;
var sum = 0;
for (ad in data.top_ads) {
if ({}.hasOwnProperty.call(data.top_ads, ad)){
if (Object.prototype.hasOwnProperty.call(data.top_ads, ad)){
sum += data.top_ads[ad];
}
}

for (ad in data.top_ads) {

if ({}.hasOwnProperty.call(data.top_ads, ad)){
if (Object.prototype.hasOwnProperty.call(data.top_ads, ad)){
// Sanitize ad
ad = escapeHtml(ad);
if(escapeHtml(ad) !== ad)
Expand Down
40 changes: 14 additions & 26 deletions scripts/pi-hole/js/db_queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

/*global
moment
*/
/* global moment:false */

var start__ = moment().subtract(6, "days");
var from = moment(start__).utc().valueOf()/1000;
Expand All @@ -26,8 +24,8 @@ location.search.substr(1).split("&").forEach(function(item) {GETDict[item.split(

if("from" in GETDict && "until" in GETDict)
{
from = parseInt(GETDict["from"]);
until = parseInt(GETDict["until"]);
from = parseInt(GETDict.from);
until = parseInt(GETDict.until);
start__ = moment(1000*from);
end__ = moment(1000*until);
instantquery = true;
Expand Down Expand Up @@ -60,15 +58,6 @@ $(function () {

var tableApi, statistics;

function escapeRegex(text) {
var map = {
"(": "\\(",
")": "\\)",
".": "\\.",
};
return text.replace(/[().]/g, function(m) { return map[m]; });
}

function add(domain,list) {
var token = $("#token").html();
var alInfo = $("#alInfo");
Expand Down Expand Up @@ -113,7 +102,7 @@ function add(domain,list) {
alDomain.html("");
});
},
error: function(jqXHR, exception) {
error: function() {
alFailure.show();
err.html("");
alFailure.delay(1000).fadeOut(2000, function() {
Expand All @@ -127,7 +116,7 @@ function add(domain,list) {
}
});
}
function handleAjaxError( xhr, textStatus, error ) {
function handleAjaxError( xhr, textStatus ) {
if ( textStatus === "timeout" )
{
alert( "The server took too long to send the data." );
Expand Down Expand Up @@ -223,9 +212,8 @@ function refreshTableData() {
}

$(document).ready(function() {
var status;

var APIstring;

if(instantquery)
{
APIstring = "api_db.php?getAllQueries&from="+from+"&until="+until;
Expand All @@ -242,7 +230,7 @@ $(document).ready(function() {
}

tableApi = $("#all-queries").DataTable( {
"rowCallback": function( row, data, index ){
"rowCallback": function( row, data ){
var blocked, fieldtext, buttontext, color;
switch (data[4])
{
Expand All @@ -268,31 +256,31 @@ $(document).ready(function() {
blocked = true;
color = "red";
fieldtext = "Blocked <br class='hidden-lg'>(regex/wildcard)";
buttontext = "<button class=\"text-green text-nowrap\"><i class=\"fas fa-check\"></i> Whitelist</button>" ;
buttontext = "<button class=\"text-green text-nowrap\"><i class=\"fas fa-check\"></i> Whitelist</button>";
break;
case 5:
blocked = true;
color = "red";
fieldtext = "Blocked <br class='hidden-lg'>(blacklist)";
buttontext = "<button class=\"text-green text-nowrap\"><i class=\"fas fa-check\"></i> Whitelist</button>" ;
buttontext = "<button class=\"text-green text-nowrap\"><i class=\"fas fa-check\"></i> Whitelist</button>";
break;
case 6:
blocked = true;
color = "red";
fieldtext = "Blocked <br class='hidden-lg'>(external, IP)";
buttontext = "" ;
buttontext = "";
break;
case 7:
blocked = true;
color = "red";
fieldtext = "Blocked <br class='hidden-lg'>(external, NULL)";
buttontext = "" ;
buttontext = "";
break;
case 8:
blocked = true;
color = "red";
fieldtext = "Blocked <br class='hidden-lg'>(external, NXRA)";
buttontext = "" ;
buttontext = "";
break;
default:
blocked = false;
Expand Down Expand Up @@ -325,12 +313,12 @@ $(document).ready(function() {
"deferRender": true,
"order" : [[0, "desc"]],
"columns": [
{ "width" : "15%", "render": function (data, type, full, meta) { if(type === "display"){return moment.unix(Math.floor(data/1e6)).format("Y-MM-DD [<br class='hidden-lg'>]HH:mm:ss z");}else{return data;} }},
{ "width" : "15%", "render": function (data, type) { if(type === "display"){return moment.unix(Math.floor(data/1e6)).format("Y-MM-DD [<br class='hidden-lg'>]HH:mm:ss z");}return data; }},
{ "width" : "10%" },
{ "width" : "40%" },
{ "width" : "20%" },
{ "width" : "10%" },
{ "width" : "5%" },
{ "width" : "5%" }
],
"lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
"columnDefs": [ {
Expand Down
Loading

0 comments on commit d966c7f

Please sign in to comment.