Fix global counter issue

The total elements field need async to long to load, so that this value
is NaN by scanning the first url. We check now first of NaN or Null and
get the current value. The browser is waiting until the value is loaded.
This commit is contained in:
Kevin Röbert 2017-09-14 03:39:40 +02:00
parent 986ee20233
commit 47c4882e9b
2 changed files with 203 additions and 191 deletions

View File

@ -11,7 +11,7 @@ var badges = [];
var badgedStatus;
var tabid = 0;
var globalCounter;
var globalURLCounter;
var globalurlcounter;
var siteBlockedAlert = browser.extension.getURL ('./siteBlockedAlert.html');
/**
@ -310,10 +310,46 @@ function countFields(url)
*/
function clearUrl(request)
{
if(globalurlcounter === null || typeof(globalurlcounter) == "undefined")
{
/**
* Get the globalURLCounter value from the browser storage
* @param {(data){} Return value form browser.storage.local.get
*/
browser.storage.local.get('globalurlcounter', function(data){
if(data.globalurlcounter){
globalurlcounter = data.globalurlcounter;
}
else {
globalurlcounter = 0;
}
return clearUrl(request);
});
}
else if(globalCounter === null || typeof(globalCounter) == "undefined") {
/**
* Get the globalCounter value from the browser storage
* @param {(data){} Return value form browser.storage.local.get
*/
browser.storage.local.get('globalCounter', function(data){
if(data.globalCounter){
globalCounter = data.globalCounter;
}
else {
globalCounter = 0;
}
return clearUrl(request);
});
}
else {
var URLbeforeReplaceCount = countFields(request.url);
//Add Fields form Request to global url counter
globalURLCounter += URLbeforeReplaceCount;
browser.storage.local.set({"globalURLCounter": globalURLCounter});
globalurlcounter += URLbeforeReplaceCount;
browser.storage.local.set({"globalurlcounter": globalurlcounter});
browser.storage.local.get('globalStatus', clear);
@ -359,6 +395,7 @@ function clearUrl(request)
}
}
}
}
};
/**
@ -368,29 +405,6 @@ function handleRemoved(tabId, removeInfo) {
delete badges[tabId];
}
/**
* Get the globalCounter value from the browser storage
* @param {(data){} Return value form browser.storage.local.get
*/
function setGlobalCounter() {
browser.storage.local.get('globalCounter', function(data){
if(data.globalCounter){
globalCounter = data.globalCounter;
}
else {
globalCounter = 0;
}
});
browser.storage.local.get('globalURLCounter', function(data){
if(data.globalURLCounter){
globalURLCounter = data.globalURLCounter;
}
else {
globalURLCounter = 0;
}
});
}
/**
* Get the badged status from the browser storage and put the value
* into a local variable.
@ -418,12 +432,10 @@ function setBadgedStatus() {
*/
fetchFromURL();
setBadgedStatus();
setGlobalCounter();
/**
* Call by each change in the browser storage.
*/
browser.storage.onChanged.addListener(setGlobalCounter);
browser.storage.onChanged.addListener(setBadgedStatus);
/**

View File

@ -21,7 +21,7 @@ function changeStatistics(){
var elTotal = $('#statistics_total_elements');
var globalPercentage = 0;
var globalCounter;
var globalURLCounter;
var globalurlcounter;
browser.storage.local.get('globalCounter', function(data){
if(data.globalCounter){
@ -34,22 +34,22 @@ function changeStatistics(){
element.text(globalCounter.toLocaleString());
});
browser.storage.local.get('globalURLCounter', function(data){
if(data.globalURLCounter){
globalURLCounter = data.globalURLCounter;
browser.storage.local.get('globalurlcounter', function(data){
if(data.globalurlcounter){
globalurlcounter = data.globalurlcounter;
}
else {
globalURLCounter = 0;
globalurlcounter = 0;
}
globalPercentage = ((globalCounter/globalURLCounter)*100).toFixed(3);
globalPercentage = ((globalCounter/globalurlcounter)*100).toFixed(3);
if(isNaN(Number(globalPercentage))) globalPercentage = 0;
elGlobalPercentage.text(globalPercentage+"%");
elProgressbar_blocked.css('width', globalPercentage+'%');
elProgressbar_non_blocked.css('width', (100-globalPercentage)+'%');
elTotal.text(globalURLCounter);
elTotal.text(globalurlcounter.toLocaleString());
});
};
@ -122,7 +122,7 @@ function setTabcounter() {
*/
function resetGlobalCounter(){
browser.storage.local.set({"globalCounter": 0});
browser.storage.local.set({"globalURLCounter": 0});
browser.storage.local.set({"globalurlcounter": 0});
};
$(document).ready(function(){