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

View File

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