LocalCDN-Firefox-Chrome-Brave/pages/logging/logging.js

203 lines
5.8 KiB
JavaScript
Raw Normal View History

2021-02-21 19:55:31 +01:00
/**
* Logging Page
* Belongs to LocalCDN
*
* @author nobody
* @since 2021-02-19
*
* @license MPL 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';
/**
* Logging Page
*/
var logging = {};
logging._onDocumentLoaded = function () {
logging._getLoggingData()
.then(logging._generateTable);
document.getElementById('btn-reload').addEventListener('click', logging._resfresh);
2021-02-24 07:01:41 +01:00
document.getElementById('btn-copy').addEventListener('click', logging._toggleCopySection);
2021-02-22 07:31:57 +01:00
document.getElementById('btn-delete').addEventListener('click', logging._deleteLogs);
document.getElementById('btn-raw').addEventListener('click', function () { logging._prepareCopiedData('btn-raw'); });
document.getElementById('btn-markdown').addEventListener('click', function () { logging._prepareCopiedData('btn-markdown'); });
2021-02-21 19:55:31 +01:00
};
logging._generateTable = function () {
let data = logging._data;
if (data.length === 0) {
logging._showTable(false);
return;
}
for (let i = data.length - 1; i >= 0; i--) {
2023-01-20 06:48:18 +01:00
let tbody, row, cell, content, redirect, initiator;
2021-02-21 19:55:31 +01:00
tbody = document.getElementById('logging-content').getElementsByTagName('tbody')[0];
row = tbody.insertRow();
if (Object.values(data[i])[3]) {
row.classList.add('missing');
}
cell = row.insertCell();
content = document.createTextNode(i + 1);
cell.appendChild(content);
2023-01-20 06:48:18 +01:00
if (Object.values(data[i])[4] === '') {
initiator = Object.values(data[i])[0];
} else {
initiator = `${Object.values(data[i])[0]}\n(i) ${Object.values(data[i])[4]}`;
}
2021-02-21 19:55:31 +01:00
cell = row.insertCell();
2023-01-20 06:48:18 +01:00
content = document.createTextNode(initiator);
2021-02-21 19:55:31 +01:00
cell.appendChild(content);
cell = row.insertCell();
content = document.createTextNode(Object.values(data[i])[1]);
cell.appendChild(content);
cell = row.insertCell();
if (Object.values(data[i])[2] === '') {
redirect = '-';
} else {
redirect = Object.values(data[i])[2];
}
content = document.createTextNode(redirect);
cell.appendChild(content);
}
logging._showTable(true);
};
logging._deleteLogs = function () {
if (confirm('Are you sure you want to delete these logs?')) {
let message = {
'topic': 'logs:delete',
'value': '',
};
chrome.runtime.sendMessage(message);
logging._resfresh();
}
};
logging._getLoggingData = function () {
return new Promise((resolve) => {
let message = {
'topic': 'logs:get'
};
chrome.runtime.sendMessage(message, function (data) {
logging._data = data['logs'];
resolve();
});
});
};
logging._showTable = function (value) {
let v1, v2;
v1 = value ? 'block' : 'none';
v2 = value ? 'none' : 'block';
document.getElementById('logging-content').style.display = v1;
document.getElementById('btn-delete').style.display = v1;
2021-02-22 07:31:57 +01:00
document.getElementById('btn-copy').style.display = v1;
2021-02-21 19:55:31 +01:00
document.getElementById('no-logging-content').style.display = v2;
};
logging._resfresh = function () {
location.reload();
};
2021-02-24 07:01:41 +01:00
logging._toggleCopySection = function () {
let copyBtn, copySection;
copyBtn = document.getElementById('btn-copy');
copySection = document.getElementById('copy-to-clipboard-section');
if (copyBtn.classList.contains('button-active')) {
copyBtn.textContent = 'Copy';
copyBtn.classList.remove('button-active');
copySection.style.display = 'none';
} else {
copyBtn.classList.add('button-active');
copyBtn.textContent = 'Close';
copySection.style.display = 'block';
}
2021-02-22 07:31:57 +01:00
logging._prepareCopiedData('btn-raw');
};
logging._prepareCopiedData = function (type) {
let data, str;
data = logging._data;
if (type === 'btn-markdown') {
str = '| Number | Initiator | Resource | Redirected to |\n';
str += '| -------- | -------- | -------- | -------- |\n';
for (let i = 0; i < data.length; i++) {
str += String(`| \`${i + 1}\` |`);
str += String(` \`${Object.values(data[i])[0]}\` |`);
str += String(` \`${Object.values(data[i])[1]}\` |`);
str += String(` \`${Object.values(data[i])[2]}\` |\n`);
}
} else {
str = '"number";"initiator";"resource";"redirected to";\n';
for (let i = 0; i < data.length; i++) {
str += String(`"${i + 1}";`);
str += String(`"${Object.values(data[i])[0]}";`);
str += String(`"${Object.values(data[i])[1]}";`);
str += String(`"${Object.values(data[i])[2]}";\n`);
}
}
2021-02-24 07:01:41 +01:00
2021-02-22 07:31:57 +01:00
document.getElementById('copied-data').value = str;
2021-02-24 07:01:41 +01:00
2021-02-22 07:31:57 +01:00
logging._copy();
2021-02-24 07:01:41 +01:00
logging._switchExportType(type);
2021-02-22 07:31:57 +01:00
};
logging._copy = function () {
let textArea = document.getElementById('copied-data');
navigator.clipboard.writeText(textArea.value).then(
function () {
textArea.select();
},
function () {
alert('Rule set cannot be copied!');
}
);
};
2021-02-24 07:01:41 +01:00
logging._switchExportType = function (type) {
let btnRaw, btnMarkdown;
btnRaw = document.getElementById('btn-raw');
btnMarkdown = document.getElementById('btn-markdown');
if (type === 'btn-raw') {
btnMarkdown.classList.remove('button-active');
btnRaw.classList.add('button-active');
} else {
btnMarkdown.classList.add('button-active');
btnRaw.classList.remove('button-active');
}
};
2021-02-21 19:55:31 +01:00
logging._data = [];
document.addEventListener('DOMContentLoaded', logging._onDocumentLoaded);