Sledgehammer

This commit is contained in:
Kevin Röbert 2018-01-25 23:03:18 +01:00
parent a799d40029
commit 40ff16762b
2 changed files with 676 additions and 675 deletions

View File

@ -20,29 +20,29 @@ var localDataHash;
var os; var os;
/** /**
* Save OS Version * Save OS Version
*/ */
browser.runtime.getPlatformInfo(function(info) { browser.runtime.getPlatformInfo(function(info) {
os = info.os; os = info.os;
});
/**
* Initialize the JSON provider object keys. /**
* * Initialize the JSON provider object keys.
* @param {JSON Object} obj *
*/ * @param {JSON Object} obj
function getKeys(obj){ */
function getKeys(obj){
for(var key in obj){ for(var key in obj){
prvKeys.push(key); prvKeys.push(key);
} }
} }
/** /**
* Initialize the providers form the JSON object. * Initialize the providers form the JSON object.
* *
*/ */
function createProviders() function createProviders()
{ {
for(var p = 0; p < prvKeys.length; p++) for(var p = 0; p < prvKeys.length; p++)
{ {
//Create new provider //Create new provider
@ -69,26 +69,26 @@ function createProviders()
providers[p].addRedirection(data.providers[prvKeys[p]].redirections[re]); providers[p].addRedirection(data.providers[prvKeys[p]].redirections[re]);
} }
} }
} }
/** /**
* Convert the external data to JSON Objects and * Convert the external data to JSON Objects and
* call the create provider function. * call the create provider function.
* *
* @param {String} retrievedText - pure data form github * @param {String} retrievedText - pure data form github
*/ */
function toJSON(retrievedText) { function toJSON(retrievedText) {
data = JSON.parse(retrievedText); data = JSON.parse(retrievedText);
getKeys(data.providers); getKeys(data.providers);
createProviders(); createProviders();
} }
/** /**
* Load local saved data, if the browser is offline or * Load local saved data, if the browser is offline or
* some other network trouble. * some other network trouble.
*/ */
function loadOldDataFromStore() function loadOldDataFromStore()
{ {
browser.storage.local.get('ClearURLsData', function(localData){ browser.storage.local.get('ClearURLsData', function(localData){
if(localData.ClearURLsData){ if(localData.ClearURLsData){
data = localData.ClearURLsData; data = localData.ClearURLsData;
@ -100,18 +100,18 @@ function loadOldDataFromStore()
localDataHash = $.sha256(data); localDataHash = $.sha256(data);
}); });
} }
/** /**
* 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)
{ {
case 1: status_code = "up to date"; case 1: status_code = "up to date";
@ -123,16 +123,16 @@ function storeHashStatus(status_code)
default: status_code = "error"; default: status_code = "error";
} }
browser.storage.local.set({"hashStatus": status_code}); browser.storage.local.set({"hashStatus": 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
fetch("https://raw.githubusercontent.com/KevinRoebert/ClearUrls/master/data/rules.hash?flush_cache=true") fetch("https://raw.githubusercontent.com/KevinRoebert/ClearUrls/master/data/rules.hash?flush_cache=true")
.then(function(response){ .then(function(response){
@ -155,13 +155,13 @@ function getHash()
} }
}); });
}); });
} }
/** /**
* Fetch the Rules & Exception from github. * Fetch the Rules & Exception from github.
*/ */
function fetchFromURL() function fetchFromURL()
{ {
fetch("https://raw.githubusercontent.com/KevinRoebert/ClearUrls/master/data/data.json?flush_cache=true") fetch("https://raw.githubusercontent.com/KevinRoebert/ClearUrls/master/data/data.json?flush_cache=true")
.then(checkResponse); .then(checkResponse);
@ -185,22 +185,22 @@ function fetchFromURL()
} }
}); });
} }
} }
// ################################################################## // ##################################################################
/* /*
* ################################################################## * ##################################################################
* # Supertyp Provider # * # Supertyp Provider #
* ################################################################## * ##################################################################
*/ */
/** /**
* Declare constructor * Declare constructor
* *
* @param {String} _name Provider name * @param {String} _name Provider name
* @param {boolean} completeProvider Set URL Pattern as rule * @param {boolean} completeProvider Set URL Pattern as rule
*/ */
function Provider(_name,_completeProvider = false){ function Provider(_name,_completeProvider = false){
var name = _name; var name = _name;
var urlPattern; var urlPattern;
var rules = []; var rules = [];
@ -327,19 +327,19 @@ function Provider(_name,_completeProvider = false){
return re; return re;
}; };
} }
// ################################################################## // ##################################################################
/** /**
* Helper function which remove the tracking fields * Helper function which remove the tracking fields
* for each provider given as parameter. * for each provider given as parameter.
* *
* @param {Provider} provider Provider-Object * @param {Provider} provider Provider-Object
* @param {webRequest} request webRequest-Object * @param {webRequest} request webRequest-Object
* @return {Array} Array with changes and url fields * @return {Array} Array with changes and url fields
*/ */
function removeFieldsFormURL(provider, request) function removeFieldsFormURL(provider, request)
{ {
var url = request.url; var url = request.url;
var rules = provider.getRules(); var rules = provider.getRules();
var changes = false; var changes = false;
@ -422,30 +422,30 @@ function removeFieldsFormURL(provider, request)
"changes": false "changes": false
}; };
} }
} }
/** /**
* Return the number of parameters query strings. * Return the number of parameters query strings.
* @param {String} url URL as String * @param {String} url URL as String
* @return {int} Number of Parameters * @return {int} Number of Parameters
*/ */
function countFields(url) function countFields(url)
{ {
var matches = (url.match(/[^\/|\?|&]+=[^\/|\?|&]+/gi) || []); var matches = (url.match(/[^\/|\?|&]+=[^\/|\?|&]+/gi) || []);
var count = matches.length; var count = matches.length;
return count; return count;
} }
/** /**
* Function which called from the webRequest to * Function which called from the webRequest to
* remove the tracking fields from the url. * remove the tracking fields from the url.
* *
* @param {webRequest} request webRequest-Object * @param {webRequest} request webRequest-Object
* @return {Array} redirectUrl or none * @return {Array} redirectUrl or none
*/ */
function clearUrl(request) function clearUrl(request)
{ {
if(globalurlcounter === null || typeof(globalurlcounter) == "undefined") if(globalurlcounter === null || typeof(globalurlcounter) == "undefined")
{ {
/** /**
@ -546,16 +546,16 @@ function clearUrl(request)
// Default case // Default case
return {}; return {};
} }
/** /**
* This function get the log on start and load the * This function get the log on start and load the
* json data in to the log variable. * json data in to the log variable.
* If no log in the local storage, this function * If no log in the local storage, this function
* create a foundation json variable. * create a foundation json variable.
*/ */
function getLogOnStart() function getLogOnStart()
{ {
browser.storage.local.get('log', function(data) { browser.storage.local.get('log', function(data) {
if(data.log) if(data.log)
{ {
@ -566,20 +566,20 @@ function getLogOnStart()
log = {"log": []}; log = {"log": []};
} }
}); });
} }
/** /**
* Function to log all activities from ClearUrls. * Function to log all activities from ClearUrls.
* Only logging when activated. * Only logging when activated.
* The log is only temporary saved in the cache and will * The log is only temporary saved in the cache and will
* permanently saved with the saveLogOnClose function. * permanently saved with the saveLogOnClose function.
* *
* @param beforeProcessing the url before the clear process * @param beforeProcessing the url before the clear process
* @param afterProcessing the url after the clear process * @param afterProcessing the url after the clear process
* @param rule the rule that triggered the process * @param rule the rule that triggered the process
*/ */
function pushToLog(beforeProcessing, afterProcessing, rule) function pushToLog(beforeProcessing, afterProcessing, rule)
{ {
if(logging) if(logging)
{ {
log.log.push( log.log.push(
@ -591,15 +591,15 @@ function pushToLog(beforeProcessing, afterProcessing, rule)
} }
); );
} }
} }
/** /**
* This function is triggered by the event windows.onRemoved and tabs.onCreated * This function is triggered by the event windows.onRemoved and tabs.onCreated
* and will save the log permanently to the local storage. * and will save the log permanently to the local storage.
* We only save the log anticyclically based on performance. * We only save the log anticyclically based on performance.
*/ */
function saveLog() function saveLog()
{ {
if(logging) if(logging)
{ {
browser.storage.local.get('resetLog', function(data) { browser.storage.local.get('resetLog', function(data) {
@ -614,15 +614,15 @@ function saveLog()
} }
}); });
} }
} }
/** /**
* Check if the status from logging has changed. * Check if the status from logging has changed.
* *
* The default value is false (off). * The default value is false (off).
*/ */
function getLoggingStatus() function getLoggingStatus()
{ {
browser.storage.local.get('loggingStatus', function(data) { browser.storage.local.get('loggingStatus', function(data) {
if(data.loggingStatus) { if(data.loggingStatus) {
logging = data.loggingStatus; logging = data.loggingStatus;
@ -634,33 +634,33 @@ function getLoggingStatus()
logging = false; logging = false;
} }
}); });
} }
/** /**
* Call by each windows is closed or created. * Call by each windows is closed or created.
*/ */
if(!checkOSAndroid()) if(!checkOSAndroid())
{ {
console.log("ClearURLs: Log listener is added.") console.log("ClearURLs: Log listener is added.")
browser.windows.onRemoved.addListener(saveLog); browser.windows.onRemoved.addListener(saveLog);
} }
browser.tabs.onCreated.addListener(saveLog); browser.tabs.onCreated.addListener(saveLog);
/** /**
* Function that calls some function on storage change. * Function that calls some function on storage change.
*/ */
function reactToStorageChange() function reactToStorageChange()
{ {
setBadgedStatus(); setBadgedStatus();
getLoggingStatus(); getLoggingStatus();
} }
/** /**
* Get the badged status from the browser storage and put the value * Get the badged status from the browser storage and put the value
* into a local variable. * into a local variable.
* *
*/ */
function setBadgedStatus() { function setBadgedStatus() {
browser.storage.local.get('badgedStatus', function(data) { browser.storage.local.get('badgedStatus', function(data) {
if(data.badgedStatus) { if(data.badgedStatus) {
badgedStatus = data.badgedStatus; badgedStatus = data.badgedStatus;
@ -675,9 +675,9 @@ function setBadgedStatus() {
badgedStatus = false; badgedStatus = false;
} }
}); });
} }
/** /**
* Check if it is an android device. * Check if it is an android device.
* @return bool * @return bool
*/ */
@ -692,53 +692,53 @@ function setBadgedStatus() {
} }
} }
/** /**
* Call loadOldDataFromStore, getHash, counter, status and log functions * Call loadOldDataFromStore, getHash, counter, status and log functions
*/ */
loadOldDataFromStore(); loadOldDataFromStore();
getHash(); getHash();
setBadgedStatus(); setBadgedStatus();
getLogOnStart(); getLogOnStart();
/** /**
* Call by each change in the browser storage. * Call by each change in the browser storage.
*/ */
browser.storage.onChanged.addListener(reactToStorageChange); browser.storage.onChanged.addListener(reactToStorageChange);
/** /**
* Call by each tab is updated. * Call by each tab is updated.
* And if url has changed. * And if url has changed.
*/ */
function handleUpdated(tabId, changeInfo, tabInfo) { function handleUpdated(tabId, changeInfo, tabInfo) {
if(changeInfo.url) if(changeInfo.url)
{ {
delete badges[tabId]; delete badges[tabId];
} }
} }
/** /**
* Call by each tab is updated. * Call by each tab is updated.
*/ */
browser.tabs.onUpdated.addListener(handleUpdated); browser.tabs.onUpdated.addListener(handleUpdated);
/** /**
* Call by each tab change to set the actual tab id * Call by each tab change to set the actual tab id
*/ */
function handleActivated(activeInfo) { function handleActivated(activeInfo) {
tabid = activeInfo.tabId; tabid = activeInfo.tabId;
} }
/** /**
* Call by each tab change. * Call by each tab change.
*/ */
browser.tabs.onActivated.addListener(handleActivated); browser.tabs.onActivated.addListener(handleActivated);
/** /**
* Check the request. * Check the request.
*/ */
function promise(requestDetails) function promise(requestDetails)
{ {
if(isDataURL(requestDetails)) if(isDataURL(requestDetails))
{ {
return {}; return {};
@ -748,28 +748,29 @@ function promise(requestDetails)
return ret; return ret;
} }
} }
/** /**
* To prevent long loading on data urls * To prevent long loading on data urls
* we will check here for data urls. * we will check here for data urls.
* *
* @type {requestDetails} * @type {requestDetails}
* @return {boolean} * @return {boolean}
*/ */
function isDataURL(requestDetails) { function isDataURL(requestDetails) {
var s = requestDetails.url; var s = requestDetails.url;
return s.substring(0,4) == "data"; 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(
promise, promise,
{urls: ["<all_urls>"]}, {urls: ["<all_urls>"]},
["blocking"] ["blocking"]
); );
});

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "ClearURLs", "name": "ClearURLs",
"version": "1.2.1.2", "version": "1.2.1.3",
"author": "Kevin R.", "author": "Kevin R.",
"description": "Remove tracking elements form URLs.", "description": "Remove tracking elements form URLs.",
"homepage_url": "https://github.com/KevinRoebert/ClearUrls", "homepage_url": "https://github.com/KevinRoebert/ClearUrls",