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;
@ -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,7 +403,6 @@ function removeFieldsFormURL(provider, request)
cancel = true; cancel = true;
} }
}
return { return {
"changes": changes, "changes": changes,
@ -402,6 +410,12 @@ function removeFieldsFormURL(provider, request)
"cancel": cancel "cancel": cancel
}; };
} }
else {
return {
"changes": false
};
}
}
/** /**
* Return the number of parameters query strings. * Return the number of parameters query strings.
@ -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"]
); );