Fixed counter issue

Fixed https://github.com/ClearURLs/Addon/issues/234
This commit is contained in:
Kevin R
2022-07-26 21:48:08 +02:00
parent 2096fa8382
commit 0f08b0f7e3
7 changed files with 83 additions and 59 deletions

View File

@@ -27,7 +27,7 @@ let badges = {};
* Increases the badged by one.
*/
function increaseBadged(quiet = false, request) {
if (!quiet) increaseURLCounter();
if (!quiet) increaseCleanedCounter();
if(request === null) return;
@@ -74,4 +74,4 @@ function handleUpdated(tabId, changeInfo, tabInfo) {
/**
* Call by each tab is updated.
*/
browser.tabs.onUpdated.addListener(handleUpdated);
browser.tabs.onUpdated.addListener(handleUpdated);

View File

@@ -23,8 +23,8 @@ var elProgressbar_blocked = document.getElementById('progress_blocked');
var elProgressbar_non_blocked = document.getElementById('progress_non_blocked');
var elTotal = document.getElementById('statistics_total_elements');
var globalPercentage = 0;
var globalCounter;
var globalurlcounter;
var cleanedCounter;
var totalCounter;
var globalStatus;
var badgedStatus;
var hashStatus;
@@ -47,19 +47,19 @@ function init()
}
/**
* Get the globalCounter and globalurlcounter value from the storage
* Get the cleanedCounter and totalCounter value from the storage
*/
function changeStatistics()
{
globalPercentage = ((globalCounter/globalurlcounter)*100).toFixed(3);
globalPercentage = ((cleanedCounter/totalCounter)*100).toFixed(3);
if(isNaN(Number(globalPercentage))) globalPercentage = 0;
element.textContent = globalCounter.toLocaleString();
element.textContent = cleanedCounter.toLocaleString();
elGlobalPercentage.textContent = globalPercentage+"%";
elProgressbar_blocked.style.width = globalPercentage+'%';
elProgressbar_non_blocked.style.width = (100-globalPercentage)+'%';
elTotal.textContent = globalurlcounter.toLocaleString();
elTotal.textContent = totalCounter.toLocaleString();
}
/**
@@ -161,12 +161,12 @@ function setSwitchButton(id, varname)
function resetGlobalCounter(){
browser.runtime.sendMessage({
function: "setData",
params: ['globalCounter', 0]
params: ['cleanedCounter', 0]
}).catch(handleError);
browser.runtime.sendMessage({
function: "setData",
params: ['globalurlcounter', 0]
params: ['totalCounter', 0]
}).catch(handleError);
browser.runtime.sendMessage({
@@ -174,15 +174,15 @@ function resetGlobalCounter(){
params: []
}).catch(handleError);
globalCounter = 0;
globalurlcounter = 0;
cleanedCounter = 0;
totalCounter = 0;
changeStatistics();
}
(function() {
loadData("globalCounter")
.then(() => loadData("globalurlcounter"))
loadData("cleanedCounter")
.then(() => loadData("totalCounter"))
.then(() => loadData("globalStatus"))
.then(() => loadData("badgedStatus"))
.then(() => loadData("hashStatus"))

View File

@@ -46,7 +46,7 @@ function _cleaning(url, quiet = false) {
if (!quiet) {
//Add Fields form Request to global url counter
increaseGlobalURLCounter(URLbeforeReplaceCount);
increaseTotalCounter(URLbeforeReplaceCount);
}
for (let i = 0; i < providers.length; i++) {

View File

@@ -63,6 +63,13 @@ function storageDataAsString(key) {
}
}
/**
* Delete key from browser storage.
*/
function deleteFromDisk(key) {
browser.storage.local.remove(key).catch(handleError);
}
/**
* Save multiple keys on the disk.
* @param {String[]} keys
@@ -159,6 +166,20 @@ function setData(key, value) {
case "logLimit":
storage[key] = Math.max(0, Number(value));
break;
case "globalurlcounter":
// migrate from old key
storage["totalCounter"] = value;
delete storage[key];
deleteFromDisk(key);
saveOnExit();
break;
case "globalCounter":
// migrate from old key
storage["cleanedCounter"] = value;
delete storage[key];
deleteFromDisk(key);
saveOnExit();
break;
default:
storage[key] = value;
}
@@ -186,8 +207,8 @@ function initSettings() {
storage.dataHash = "";
storage.badgedStatus = true;
storage.globalStatus = true;
storage.globalurlcounter = 0;
storage.globalCounter = 0;
storage.totalCounter = 0;
storage.cleanedCounter = 0;
storage.hashStatus = "error";
storage.loggingStatus = false;
storage.log = {"log": []};

View File

@@ -114,7 +114,7 @@ function checkLocalURL(url) {
* @return {int} Number of Parameters
*/
function countFields(url) {
return new URL(url).searchParams.entries.length;
return [...new URL(url).searchParams].length
}
/**
@@ -158,23 +158,23 @@ function loadOldDataFromStore() {
}
/**
* Increase by {number} the GlobalURLCounter
* Increase by {number} the total counter
* @param {int} number
*/
function increaseGlobalURLCounter(number) {
function increaseTotalCounter(number) {
if (storage.statisticsStatus) {
storage.globalurlcounter += number;
deferSaveOnDisk('globalurlcounter');
storage.totalCounter += number;
deferSaveOnDisk('totalCounter');
}
}
/**
* Increase by one the URLCounter
* Increase by one the cleaned counter
*/
function increaseURLCounter() {
function increaseCleanedCounter() {
if (storage.statisticsStatus) {
storage.globalCounter++;
deferSaveOnDisk('globalCounter');
storage.cleanedCounter++;
deferSaveOnDisk('cleanedCounter');
}
}