2019-03-13 12:20:24 +00:00
|
|
|
/*
|
|
|
|
* ClearURLs
|
|
|
|
* Copyright (c) 2017-2019 Kevin Röbert
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-02-13 17:02:08 +00:00
|
|
|
/*jshint esversion: 6 */
|
2018-05-17 23:31:04 +00:00
|
|
|
var element = $("#statistics_value");
|
|
|
|
var elGlobalPercentage = $("#statistics_value_global_percentage");
|
|
|
|
var elProgressbar_blocked = $('#progress_blocked');
|
|
|
|
var elProgressbar_non_blocked = $('#progress_non_blocked');
|
|
|
|
var elTotal = $('#statistics_total_elements');
|
|
|
|
var globalPercentage = 0;
|
|
|
|
var globalCounter;
|
|
|
|
var globalurlcounter;
|
|
|
|
var globalStatus;
|
|
|
|
var badgedStatus;
|
|
|
|
var hashStatus;
|
|
|
|
var loggingStatus;
|
2018-05-22 16:40:05 +00:00
|
|
|
var statisticsStatus;
|
2018-08-31 18:55:15 +00:00
|
|
|
var currentURL;
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-02-13 17:02:08 +00:00
|
|
|
async function getData()
|
2018-05-17 23:31:04 +00:00
|
|
|
{
|
2019-02-13 17:02:08 +00:00
|
|
|
await browser.runtime.sendMessage({
|
2019-04-01 21:53:28 +00:00
|
|
|
function: "getData",
|
|
|
|
params: ["globalCounter"]
|
|
|
|
}).then((data) => {
|
|
|
|
globalCounter = data.response;
|
|
|
|
});
|
|
|
|
|
|
|
|
await browser.runtime.sendMessage({
|
|
|
|
function: "getData",
|
|
|
|
params: ["globalurlcounter"]
|
2019-02-13 17:02:08 +00:00
|
|
|
}).then((data) => {
|
2019-04-01 21:53:28 +00:00
|
|
|
globalurlcounter = data.response;
|
|
|
|
});
|
2019-02-13 17:02:08 +00:00
|
|
|
|
2019-04-01 21:53:28 +00:00
|
|
|
await browser.runtime.sendMessage({
|
|
|
|
function: "getData",
|
|
|
|
params: ["globalStatus"]
|
|
|
|
}).then((data) => {
|
|
|
|
globalStatus = data.response;
|
|
|
|
});
|
2019-02-13 17:02:08 +00:00
|
|
|
|
2019-04-01 21:53:28 +00:00
|
|
|
await browser.runtime.sendMessage({
|
|
|
|
function: "getData",
|
|
|
|
params: ["badgedStatus"]
|
|
|
|
}).then((data) => {
|
|
|
|
badgedStatus = data.response;
|
|
|
|
});
|
|
|
|
|
|
|
|
await browser.runtime.sendMessage({
|
|
|
|
function: "getData",
|
|
|
|
params: ["hashStatus"]
|
|
|
|
}).then((data) => {
|
|
|
|
hashStatus = data.response;
|
|
|
|
});
|
|
|
|
|
|
|
|
await browser.runtime.sendMessage({
|
|
|
|
function: "getData",
|
|
|
|
params: ["loggingStatus"]
|
|
|
|
}).then((data) => {
|
|
|
|
loggingStatus = data.response;
|
|
|
|
});
|
|
|
|
|
|
|
|
await browser.runtime.sendMessage({
|
|
|
|
function: "getData",
|
|
|
|
params: ["statisticsStatus"]
|
|
|
|
}).then((data) => {
|
|
|
|
statisticsStatus = data.response;
|
|
|
|
});
|
|
|
|
|
|
|
|
await browser.runtime.sendMessage({
|
|
|
|
function: "getCurrentURL",
|
|
|
|
params: []
|
|
|
|
}).then((data) => {
|
|
|
|
currentURL = data.response;
|
|
|
|
});
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 19:19:32 +00:00
|
|
|
/**
|
2017-11-18 00:34:01 +00:00
|
|
|
* Initialize the UI.
|
|
|
|
*
|
|
|
|
*/
|
2017-08-31 19:19:32 +00:00
|
|
|
function init()
|
|
|
|
{
|
2018-05-22 16:40:05 +00:00
|
|
|
setSwitchButton("globalStatus", "globalStatus");
|
|
|
|
setSwitchButton("tabcounter", "badgedStatus");
|
|
|
|
setSwitchButton("logging", "loggingStatus");
|
|
|
|
setSwitchButton("statistics", "statisticsStatus");
|
2017-11-18 00:34:01 +00:00
|
|
|
setHashStatus();
|
|
|
|
changeStatistics();
|
2017-08-31 19:19:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-13 17:02:08 +00:00
|
|
|
* Get the globalCounter and globalurlcounter value from the storage
|
2017-11-18 00:34:01 +00:00
|
|
|
*/
|
2018-05-22 16:40:05 +00:00
|
|
|
function changeStatistics()
|
|
|
|
{
|
2018-05-17 23:31:04 +00:00
|
|
|
globalPercentage = ((globalCounter/globalurlcounter)*100).toFixed(3);
|
2017-11-18 00:34:01 +00:00
|
|
|
|
2018-05-17 23:31:04 +00:00
|
|
|
if(isNaN(Number(globalPercentage))) globalPercentage = 0;
|
2017-11-18 00:34:01 +00:00
|
|
|
|
2018-05-17 23:31:04 +00:00
|
|
|
element.text(globalCounter.toLocaleString());
|
|
|
|
elGlobalPercentage.text(globalPercentage+"%");
|
|
|
|
elProgressbar_blocked.css('width', globalPercentage+'%');
|
|
|
|
elProgressbar_non_blocked.css('width', (100-globalPercentage)+'%');
|
|
|
|
elTotal.text(globalurlcounter.toLocaleString());
|
2017-11-18 00:34:01 +00:00
|
|
|
}
|
2017-08-31 19:19:32 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-22 16:40:05 +00:00
|
|
|
* Set the value for the hashStatus on startUp.
|
2017-11-18 00:34:01 +00:00
|
|
|
*/
|
|
|
|
function setHashStatus()
|
|
|
|
{
|
2019-10-22 23:16:12 +00:00
|
|
|
let element = $('#hashStatus');
|
2017-11-18 00:34:01 +00:00
|
|
|
|
2018-05-17 23:31:04 +00:00
|
|
|
if(hashStatus)
|
|
|
|
{
|
2018-05-21 21:25:20 +00:00
|
|
|
element.text(translate(hashStatus));
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-05-21 21:25:20 +00:00
|
|
|
element.text(translate('hash_status_code_5'));
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 00:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-22 16:40:05 +00:00
|
|
|
* Change the value of a switch button.
|
|
|
|
* @param {string} id HTML id
|
|
|
|
* @param {string} storageID storage internal id
|
2017-11-18 00:34:01 +00:00
|
|
|
*/
|
2018-05-22 16:40:05 +00:00
|
|
|
function changeSwitchButton(id, storageID)
|
|
|
|
{
|
2019-10-22 23:16:12 +00:00
|
|
|
let element = $('#'+id);
|
2018-05-22 16:40:05 +00:00
|
|
|
|
2018-06-19 16:13:44 +00:00
|
|
|
changeVisibility(id, storageID);
|
|
|
|
|
2018-05-22 16:40:05 +00:00
|
|
|
element.on('change', function(){
|
2019-02-13 17:02:08 +00:00
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "setData",
|
|
|
|
params: [storageID, element.is(':checked')]
|
|
|
|
}).then((data) => {
|
2019-10-22 23:16:12 +00:00
|
|
|
if(storageID === "globalStatus"){
|
2019-02-13 17:02:08 +00:00
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "changeIcon",
|
|
|
|
params: []
|
|
|
|
});
|
|
|
|
}
|
2018-06-19 16:13:44 +00:00
|
|
|
changeVisibility(id, storageID);
|
2018-05-22 16:40:05 +00:00
|
|
|
|
2019-02-13 17:02:08 +00:00
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "saveOnExit",
|
|
|
|
params: []
|
|
|
|
});
|
2018-05-22 16:40:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-19 16:13:44 +00:00
|
|
|
/**
|
2019-02-13 17:02:08 +00:00
|
|
|
* Change the visibility of sections.
|
|
|
|
*/
|
2018-06-19 16:13:44 +00:00
|
|
|
function changeVisibility(id, storageID)
|
|
|
|
{
|
2019-10-22 23:16:12 +00:00
|
|
|
let element;
|
2018-06-19 16:13:44 +00:00
|
|
|
|
|
|
|
switch(storageID)
|
|
|
|
{
|
|
|
|
case "loggingStatus":
|
2019-02-13 17:02:08 +00:00
|
|
|
element = $('#log_section');
|
|
|
|
break;
|
2018-06-19 16:13:44 +00:00
|
|
|
case "statisticsStatus":
|
2019-02-13 17:02:08 +00:00
|
|
|
element = $('#statistic_section');
|
|
|
|
break;
|
2018-06-19 16:13:44 +00:00
|
|
|
default:
|
2019-02-13 17:02:08 +00:00
|
|
|
element = "undefine";
|
2018-06-19 16:13:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 23:16:12 +00:00
|
|
|
if(element !== "undefine")
|
2018-06-19 16:13:44 +00:00
|
|
|
{
|
|
|
|
if($('#'+id).is(':checked'))
|
|
|
|
{
|
|
|
|
element.css('display', '');
|
|
|
|
element.css('display', '');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
element.css('display', 'none');
|
|
|
|
element.css('display', 'none');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 16:40:05 +00:00
|
|
|
/**
|
2019-02-13 17:02:08 +00:00
|
|
|
* Set the value of a switch button.
|
|
|
|
* @param {string} id HTML id
|
|
|
|
* @param {string} varname js internal variable name
|
|
|
|
*/
|
2018-05-22 16:40:05 +00:00
|
|
|
function setSwitchButton(id, varname)
|
2017-11-18 00:34:01 +00:00
|
|
|
{
|
2019-10-22 23:16:12 +00:00
|
|
|
let element = $('#'+id);
|
2018-05-22 16:40:05 +00:00
|
|
|
element.prop('checked', this[varname]);
|
2017-11-18 00:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the global statistic
|
|
|
|
*/
|
2017-08-31 19:19:32 +00:00
|
|
|
function resetGlobalCounter(){
|
2019-02-13 17:02:08 +00:00
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "setData",
|
|
|
|
params: ['globalCounter', 0]
|
2018-05-17 23:31:04 +00:00
|
|
|
});
|
2017-08-31 19:19:32 +00:00
|
|
|
|
2019-02-13 17:02:08 +00:00
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "setData",
|
|
|
|
params: ['globalurlcounter', 0]
|
|
|
|
});
|
|
|
|
|
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "saveOnExit",
|
|
|
|
params: []
|
|
|
|
});
|
|
|
|
|
|
|
|
globalCounter = 0;
|
|
|
|
globalurlcounter = 0;
|
|
|
|
|
|
|
|
changeStatistics();
|
2018-06-19 16:13:44 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 19:19:32 +00:00
|
|
|
$(document).ready(function(){
|
2019-02-13 17:02:08 +00:00
|
|
|
getData().then(() => {
|
2018-06-19 16:13:44 +00:00
|
|
|
init();
|
|
|
|
$('#reset_counter_btn').on("click", resetGlobalCounter);
|
|
|
|
changeSwitchButton("globalStatus", "globalStatus");
|
|
|
|
changeSwitchButton("tabcounter", "badgedStatus");
|
|
|
|
changeSwitchButton("logging", "loggingStatus");
|
|
|
|
changeSwitchButton("statistics", "statisticsStatus");
|
|
|
|
$('#loggingPage').attr('href', browser.extension.getURL('./html/log.html'));
|
|
|
|
$('#settings').attr('href', browser.extension.getURL('./html/settings.html'));
|
2019-04-11 14:40:48 +00:00
|
|
|
$('#cleaning_tools').attr('href', browser.extension.getURL('./html/cleaningTool.html'));
|
2018-06-19 16:13:44 +00:00
|
|
|
setText();
|
2019-02-13 17:02:08 +00:00
|
|
|
});
|
2018-06-19 16:13:44 +00:00
|
|
|
|
2017-09-14 01:39:40 +00:00
|
|
|
});
|
2018-05-21 21:25:20 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-22 16:40:05 +00:00
|
|
|
* Set the text for the UI.
|
|
|
|
*/
|
2018-05-21 21:25:20 +00:00
|
|
|
function setText()
|
|
|
|
{
|
2018-06-11 17:46:56 +00:00
|
|
|
injectText('loggingPage','popup_html_log_head');
|
|
|
|
injectText('reset_counter_btn','popup_html_statistics_reset_button');
|
|
|
|
injectText('rules_status_head','popup_html_rules_status_head');
|
|
|
|
injectText('statistics_percentage','popup_html_statistics_percentage');
|
|
|
|
injectText('statistics_blocked','popup_html_statistics_blocked');
|
|
|
|
injectText('statistics_elements','popup_html_statistics_elements');
|
|
|
|
injectText('statistics_head','popup_html_statistics_head');
|
|
|
|
injectText('configs_switch_badges','popup_html_configs_switch_badges');
|
|
|
|
injectText('configs_switch_log','popup_html_configs_switch_log');
|
|
|
|
injectText('configs_switch_filter','popup_html_configs_switch_filter');
|
|
|
|
injectText('configs_head','popup_html_configs_head');
|
|
|
|
injectText('configs_switch_statistics','configs_switch_statistics');
|
2018-09-26 15:35:50 +00:00
|
|
|
$('#donate').prop('title', translate('donate_button'));
|
2018-06-11 17:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-13 17:02:08 +00:00
|
|
|
* Helper function to inject the translated text and tooltip.
|
|
|
|
*
|
|
|
|
* @param {string} id ID of the HTML element
|
|
|
|
* @param {string} attribute Name of the attribute used for localization
|
2019-10-22 23:16:12 +00:00
|
|
|
* @param {string} tooltip
|
2019-02-13 17:02:08 +00:00
|
|
|
*/
|
2019-10-22 23:16:12 +00:00
|
|
|
function injectText(id, attribute, tooltip = "")
|
2018-06-11 17:46:56 +00:00
|
|
|
{
|
2019-10-22 23:16:12 +00:00
|
|
|
let object = $('#'+id);
|
2018-06-11 17:46:56 +00:00
|
|
|
object.text(translate(attribute));
|
|
|
|
|
|
|
|
/*
|
2019-02-13 17:02:08 +00:00
|
|
|
This function will throw an error if no translation
|
|
|
|
is found for the tooltip. This is a planned error.
|
|
|
|
*/
|
2018-06-11 17:46:56 +00:00
|
|
|
tooltip = translate(attribute+"_title");
|
|
|
|
|
2019-10-22 23:16:12 +00:00
|
|
|
if(tooltip !== "")
|
2018-06-11 17:46:56 +00:00
|
|
|
{
|
|
|
|
object.prop('title', tooltip);
|
|
|
|
}
|
2018-05-21 21:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate a string with the i18n API.
|
|
|
|
*
|
|
|
|
* @param {string} string Name of the attribute used for localization
|
|
|
|
*/
|
|
|
|
function translate(string)
|
|
|
|
{
|
|
|
|
return browser.i18n.getMessage(string);
|
|
|
|
}
|
2018-08-31 18:55:15 +00:00
|
|
|
|
2019-02-13 17:02:08 +00:00
|
|
|
function handleError(error) {
|
|
|
|
console.log(`Error: ${error}`);
|
|
|
|
}
|