Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate js #1376

Merged
merged 4 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions auditlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
</div>
<!-- /.row -->

<script src="scripts/pi-hole/js/utils.js"></script>
<script src="scripts/pi-hole/js/auditlog.js"></script>

<?php
Expand Down
1 change: 1 addition & 0 deletions db_graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
</div>

<script src="scripts/vendor/daterangepicker.js"></script>
<script src="scripts/pi-hole/js/utils.js"></script>
<script src="scripts/pi-hole/js/db_graph.js"></script>

<?php
Expand Down
1 change: 1 addition & 0 deletions db_lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
</div>

<script src="scripts/vendor/daterangepicker.js"></script>
<script src="scripts/pi-hole/js/utils.js"></script>
<script src="scripts/pi-hole/js/db_lists.js"></script>

<?php
Expand Down
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ function getinterval()
<!-- /.row -->
<?php } ?>

<script src="scripts/pi-hole/js/utils.js"></script>
<script src="scripts/pi-hole/js/index.js"></script>

<?php
Expand Down
21 changes: 4 additions & 17 deletions scripts/pi-hole/js/auditlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,11 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

/* global utils:false */

// Define global variables
var auditTimeout = null;

// 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 updateTopLists() {
$.getJSON("api.php?topItems=audit", function (data) {
if ("FTLnotrunning" in data) {
Expand All @@ -38,7 +25,7 @@ function updateTopLists() {
for (domain in data.top_queries) {
if (Object.prototype.hasOwnProperty.call(data.top_queries, domain)) {
// Sanitize domain
domain = escapeHtml(domain);
domain = utils.escapeHtml(domain);
url = '<a href="queries.php?domain=' + domain + '">' + domain + "</a>";
domaintable.append(
"<tr><td>" +
Expand All @@ -57,7 +44,7 @@ function updateTopLists() {
if (Object.prototype.hasOwnProperty.call(data.top_ads, domain)) {
var input = domain.split(" ");
// Sanitize domain
var printdomain = escapeHtml(input[0]);
var printdomain = utils.escapeHtml(input[0]);
if (input.length > 1) {
url =
'<a href="queries.php?domain=' +
Expand Down
48 changes: 13 additions & 35 deletions scripts/pi-hole/js/db_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

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

var start__ = moment().subtract(6, "days");
var from = moment(start__).utc().valueOf() / 1000;
Expand Down Expand Up @@ -50,28 +50,6 @@ $(function () {
);
});

function padNumber(num) {
return ("00" + num).substr(-2, 2);
}

// Helper function needed for converting the Objects to Arrays

function objectToArray(p) {
var keys = Object.keys(p);
keys.sort(function (a, b) {
return a - b;
});

var arr = [],
idx = [];
for (var i = 0; i < keys.length; i++) {
arr.push(p[keys[i]]);
idx.push(keys[i]);
}

return [idx, arr];
}

var timeLineChart;

function compareNumbers(a, b) {
Expand Down Expand Up @@ -110,8 +88,8 @@ function updateQueriesOverTime() {
"api_db.php?getGraphData&from=" + from + "&until=" + until + "&interval=" + interval,
function (data) {
// convert received objects to arrays
data.domains_over_time = objectToArray(data.domains_over_time);
data.ads_over_time = objectToArray(data.ads_over_time);
data.domains_over_time = utils.objectToArray(data.domains_over_time);
data.ads_over_time = utils.objectToArray(data.ads_over_time);
// Remove possibly already existing data
timeLineChart.data.labels = [];
timeLineChart.data.datasets[0].data = [];
Expand Down Expand Up @@ -215,28 +193,28 @@ $(document).ready(function () {
var fromDate =
time.getFullYear() +
"-" +
padNumber(time.getMonth() + 1) +
utils.padNumber(time.getMonth() + 1) +
"-" +
padNumber(time.getDate());
utils.padNumber(time.getDate());
var fromTime =
padNumber(time.getHours()) +
utils.padNumber(time.getHours()) +
":" +
padNumber(time.getMinutes()) +
utils.padNumber(time.getMinutes()) +
":" +
padNumber(time.getSeconds());
utils.padNumber(time.getSeconds());
time = new Date(time.valueOf() + 1000 * interval);
var untilDate =
time.getFullYear() +
"-" +
padNumber(time.getMonth() + 1) +
utils.padNumber(time.getMonth() + 1) +
"-" +
padNumber(time.getDate());
utils.padNumber(time.getDate());
var untilTime =
padNumber(time.getHours()) +
utils.padNumber(time.getHours()) +
":" +
padNumber(time.getMinutes()) +
utils.padNumber(time.getMinutes()) +
":" +
padNumber(time.getSeconds());
utils.padNumber(time.getSeconds());

if (fromDate === untilDate) {
// Abbreviated form for intervals on the same day
Expand Down
35 changes: 10 additions & 25 deletions scripts/pi-hole/js/db_lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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:false */
/* global utils:false, moment:false */

var start__ = moment().subtract(6, "days");
var from = moment(start__).utc().valueOf() / 1000;
Expand Down Expand Up @@ -52,21 +52,6 @@ $(function () {
);
});

// 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 updateTopClientsChart() {
$("#client-frequency .overlay").show();
$.getJSON("api_db.php?topClients&from=" + from + "&until=" + until, function (data) {
Expand All @@ -84,10 +69,10 @@ function updateTopClientsChart() {
for (client in data.top_sources) {
if (Object.prototype.hasOwnProperty.call(data.top_sources, client)) {
// Sanitize client
client = escapeHtml(client);
if (escapeHtml(client) !== client) {
client = utils.escapeHtml(client);
if (utils.escapeHtml(client) !== client) {
// Make a copy with the escaped index if necessary
data.top_sources[escapeHtml(client)] = data.top_sources[client];
data.top_sources[utils.escapeHtml(client)] = data.top_sources[client];
}

if (client.indexOf("|") !== -1) {
Expand Down Expand Up @@ -138,10 +123,10 @@ function updateTopDomainsChart() {
for (domain in data.top_domains) {
if (Object.prototype.hasOwnProperty.call(data.top_domains, domain)) {
// Sanitize domain
domain = escapeHtml(domain);
if (escapeHtml(domain) !== domain) {
domain = utils.escapeHtml(domain);
if (utils.escapeHtml(domain) !== domain) {
// Make a copy with the escaped index if necessary
data.top_domains[escapeHtml(domain)] = data.top_domains[domain];
data.top_domains[utils.escapeHtml(domain)] = data.top_domains[domain];
}

percentage = (data.top_domains[domain] / sum) * 100;
Expand Down Expand Up @@ -185,10 +170,10 @@ function updateTopAdsChart() {
for (ad in data.top_ads) {
if (Object.prototype.hasOwnProperty.call(data.top_ads, ad)) {
// Sanitize ad
ad = escapeHtml(ad);
if (escapeHtml(ad) !== ad) {
ad = utils.escapeHtml(ad);
if (utils.escapeHtml(ad) !== ad) {
// Make a copy with the escaped index if necessary
data.top_ads[escapeHtml(ad)] = data.top_ads[ad];
data.top_ads[utils.escapeHtml(ad)] = data.top_ads[ad];
}

percentage = (data.top_ads[ad] / sum) * 100;
Expand Down
Loading