Fix 26 and performance improvements

(+ Better indentation in the code)
+ Line 17: Do nothing is better as redirect to nothing
+ Line 212: Add function to return the provider name (for debugging)
+ Correct typos
+ Line 239: performance improvement
+ Line 286: performance improvement
+ Line 541: performance improvement
+ Add function "promise" for async request handling (performance
improvement)
+ Add function isDataURL to prevent long loading on data urls (Fix #26 )
This commit is contained in:
Kevin Röbert 2018-01-05 17:26:17 +01:00
parent 1810d5db90
commit 57ea6b4315

View File

@ -14,7 +14,7 @@ var badgedStatus;
var tabid = 0; var tabid = 0;
var globalCounter; var globalCounter;
var globalurlcounter; var globalurlcounter;
var siteBlockedAlert = browser.extension.getURL('./'); var siteBlockedAlert = 'javascript:void(0)';
var dataHash; var dataHash;
var localDataHash; var localDataHash;
@ -96,13 +96,13 @@ function loadOldDataFromStore()
} }
/** /**
* Save the hash status to the local storage. * Save the hash status to the local storage.
* The status can have the following values: * The status can have the following values:
* 1 "up to date" * 1 "up to date"
* 2 "updated" * 2 "updated"
* 3 "update available" * 3 "update available"
* @param status_code the number for the status * @param status_code the number for the status
*/ */
function storeHashStatus(status_code) function storeHashStatus(status_code)
{ {
switch(status_code) switch(status_code)
@ -119,11 +119,11 @@ function storeHashStatus(status_code)
} }
/** /**
* Get the hash for the rule file on github. * Get the hash for the rule file on github.
* Check the hash with the hash form the local file. * Check the hash with the hash form the local file.
* If the hash has changed, then download the new rule file. * If the hash has changed, then download the new rule file.
* Else do nothing. * Else do nothing.
*/ */
function getHash() function getHash()
{ {
//Get the target hash from github //Get the target hash from github
@ -205,6 +205,14 @@ function Provider(_name,_completeProvider = false){
rules.push(".*"); rules.push(".*");
} }
/**
* Returns the provider name.
* @return {String}
*/
this.getName = function() {
return name;
};
/** /**
* Add URL pattern. * Add URL pattern.
* *
@ -225,10 +233,10 @@ function Provider(_name,_completeProvider = false){
/** /**
* Check the url is matching the ProviderURL. * Check the url is matching the ProviderURL.
* *
* @return {String} ProviderURL as RegExp * @return {boolean} ProviderURL as RegExp
*/ */
this.matchURL = function(url) { this.matchURL = function(url) {
return !(this.matchException(url)) && (url.match(urlPattern) != null) && (url.match(urlPattern).length > 0); return !(this.matchException(url)) && urlPattern.test(url);
}; };
/** /**
@ -274,7 +282,8 @@ function Provider(_name,_completeProvider = false){
for (var i = 0; i < exceptions.length; i++) { for (var i = 0; i < exceptions.length; i++) {
if(result) { break; } if(result) { break; }
result = (url.match(new RegExp(exceptions[i], "gi"))) && (url.match(new RegExp(exceptions[i], "gi")).length > 0); exception_regex = new RegExp(exceptions[i], "gi");
result = exception_regex.test(url);
} }
return result; return result;
@ -394,13 +403,18 @@ function removeFieldsFormURL(provider, request)
cancel = true; cancel = true;
} }
}
return { return {
"changes": changes, "changes": changes,
"url": url, "url": url,
"cancel": cancel "cancel": cancel
}; };
}
else {
return {
"changes": false
};
}
} }
/** /**
@ -465,7 +479,6 @@ function clearUrl(request)
globalurlcounter += URLbeforeReplaceCount; globalurlcounter += URLbeforeReplaceCount;
browser.storage.local.set({"globalurlcounter": globalurlcounter}); browser.storage.local.set({"globalurlcounter": globalurlcounter});
browser.storage.local.get('globalStatus', clear); browser.storage.local.get('globalStatus', clear);
function clear(data){ function clear(data){
@ -513,7 +526,7 @@ function clearUrl(request)
/* /*
* Ensure that the function go not into * Ensure that the function go not into
* an loop. * a loop.
*/ */
if(result.changes){ if(result.changes){
return { return {
@ -523,6 +536,9 @@ function clearUrl(request)
} }
} }
} }
// Default case
return {};
} }
/** /**
@ -692,13 +708,47 @@ function handleActivated(activeInfo) {
*/ */
browser.tabs.onActivated.addListener(handleActivated); browser.tabs.onActivated.addListener(handleActivated);
/**
* Handle everything asynchronously.
* @type {Promise}
*/
function promise(requestDetails)
{
var timeout = 1000;
return new Promise((resolve,reject) => {
window.setTimeout(() => {
if(isDataURL(requestDetails))
{
resolve({});
}
else {
var ret = clearUrl(requestDetails);
resolve(ret);
}
},timeout);
});
}
/**
* To prevent long loading on data urls
* we will check here for data urls.
*
* @type {requestDetails}
* @return {boolean}
*/
function isDataURL(requestDetails) {
var s = requestDetails.url;
return s.substring(0,4) == "data";
}
/** /**
* Call by each Request and checking the url. * Call by each Request and checking the url.
* *
* @type {Array} * @type {Array}
*/ */
browser.webRequest.onBeforeRequest.addListener( browser.webRequest.onBeforeRequest.addListener(
clearUrl, promise,
{urls: ["<all_urls>"]}, {urls: ["<all_urls>"]},
["blocking"] ["blocking"]
); );