Addon/clearurls.js

526 lines
13 KiB
JavaScript
Raw Normal View History

2017-08-04 04:08:46 +03:00
/*
* ##################################################################
* # Fetch Rules & Exception from URL #
* ##################################################################
*/
2017-08-04 05:20:12 +03:00
var data = [];
var providers = [];
var prvKeys = [];
2017-08-06 21:19:53 +03:00
var globalStatus;
2017-08-19 01:30:47 +03:00
var badges = [];
var badgedStatus;
2017-08-19 01:30:47 +03:00
var tabid = 0;
var globalCounter;
var globalurlcounter;
var siteBlockedAlert = browser.extension.getURL ('./siteBlockedAlert.html');
2017-08-04 05:20:12 +03:00
/**
* Initialize the JSON provider object keys.
*
* @param {JSON Object} obj
*/
2017-08-04 05:20:12 +03:00
function getKeys(obj){
for(var key in obj){
prvKeys.push(key);
}
}
2017-08-04 05:20:12 +03:00
/**
* Initialize the providers form the JSON object.
*
*/
2017-08-04 05:20:12 +03:00
function createProviders()
2017-08-04 04:08:46 +03:00
{
2017-08-04 05:20:12 +03:00
for(var p = 0; p < prvKeys.length; p++)
{
//Create new provider
providers.push(new Provider(prvKeys[p],data.providers[prvKeys[p]].completeProvider));
//Add URL Pattern
providers[p].setURLPattern(data.providers[prvKeys[p]].urlPattern);
//Add rules to provider
for(var r = 0; r < data.providers[prvKeys[p]].rules.length; r++)
{
providers[p].addRule(data.providers[prvKeys[p]].rules[r]);
}
//Add exceptions to provider
for(var e = 0; e < data.providers[prvKeys[p]].exceptions.length; e++)
{
providers[p].addException(data.providers[prvKeys[p]].exceptions[e]);
}
//Add redirections to provider
for(var re = 0; re < data.providers[prvKeys[p]].redirections.length; re++)
{
providers[p].addRedirection(data.providers[prvKeys[p]].redirections[re]);
}
2017-08-04 05:20:12 +03:00
}
}
2017-08-04 05:20:12 +03:00
2017-08-31 22:19:51 +03:00
/**
* Convert the external data to JSON Objects and
* call the create provider function.
*
* @param {String} retrievedText - pure data form github
*/
function toJSON(retrievedText) {
2017-08-31 22:19:51 +03:00
data = JSON.parse(retrievedText);
getKeys(data.providers);
createProviders();
}
/**
* Load local saved data, if the browser is offline or
* some other network trouble.
*
*/
2017-08-31 22:19:51 +03:00
function loadOldDataFromStore()
{
browser.storage.local.get('ClearURLsData', function(data){
2017-08-31 22:19:51 +03:00
if(data.ClearURLsData){
data = data.ClearURLsData;
}
else {
data = "";
}
toJSON(data);
});
}
2017-08-04 05:20:12 +03:00
/**
* Fetch the Rules & Exception github.
*
*/
2017-08-04 05:20:12 +03:00
function fetchFromURL()
{
2017-08-23 16:32:40 +03:00
fetch("https://raw.githubusercontent.com/KevinRoebert/ClearUrls/master/data/data.json?flush_cache=true")
2017-08-31 22:19:51 +03:00
.then(checkResponse)
.catch(function(error){
loadOldDataFromStore();
});
2017-08-04 04:08:46 +03:00
2017-08-31 22:19:51 +03:00
function checkResponse(response)
{
var responseText = response.clone().text().then(function(responseText){
if(response.ok)
{
2017-08-31 22:19:51 +03:00
browser.storage.local.set({"ClearURLsData": responseText});
toJSON(responseText);
}
else {
loadOldDataFromStore();
2017-08-31 22:19:51 +03:00
}
});
}
2017-08-04 04:08:46 +03:00
}
2017-08-31 22:19:51 +03:00
2017-08-04 04:08:46 +03:00
// ##################################################################
2017-08-04 03:43:36 +03:00
/*
* ##################################################################
* # Supertyp Provider #
* ##################################################################
*/
2017-08-04 03:43:36 +03:00
/**
* Declare constructor
*
* @param {String} _name Provider name
* @param {boolean} completeProvider Set URL Pattern as rule
*/
function Provider(_name,_completeProvider = false){
var name = _name;
2017-08-04 03:43:36 +03:00
var urlPattern;
var rules = [];
var exceptions = [];
var canceling = _completeProvider;
var redirections = [];
2017-08-04 03:43:36 +03:00
if(_completeProvider){
rules.push(".*");
}
2017-08-04 03:43:36 +03:00
/**
* Add URL pattern.
*
* @require urlPatterns as RegExp
*/
2017-08-04 03:43:36 +03:00
this.setURLPattern = function(urlPatterns) {
urlPattern = new RegExp(urlPatterns, "mgi");
};
/**
* Return if the Provider Request is canceled
* @return {Boolean} isCanceled
*/
this.isCaneling = function() {
return canceling;
};
2017-08-04 03:43:36 +03:00
/**
* Check the url is matching the ProviderURL.
*
* @return {String} ProviderURL as RegExp
*/
this.matchURL = function(url) {
2017-08-23 16:21:09 +03:00
return !(this.matchException(url)) && (url.match(urlPattern) != null) && (url.match(urlPattern).length > 0);
2017-08-04 03:43:36 +03:00
};
/**
* Add a rule to the rule array.
*
* @param String rule RegExp as string
*/
2017-08-04 03:43:36 +03:00
this.addRule = function(rule) {
rules.push(rule);
};
/**
* Return all rules as an array.
*
* @return Array RegExp strings
*/
2017-08-04 03:43:36 +03:00
this.getRules = function() {
return rules;
};
/**
* Add a exception to the exceptions array.
*
* @param String exception RegExp as string
*/
2017-08-04 03:43:36 +03:00
this.addException = function(exception) {
exceptions.push(exception);
};
/**
* Private helper method to check if the url
* an exception.
*
* @param {String} url RegExp as string
* @return {boolean} if matching? true: false
*/
2017-08-23 16:21:09 +03:00
this.matchException = function(url) {
2017-08-04 03:43:36 +03:00
var result = false;
//Add the site blocked alert to every exception
if(url == siteBlockedAlert) return true;
2017-08-04 03:43:36 +03:00
for (var i = 0; i < exceptions.length; i++) {
if(result) { break; }
result = (url.match(new RegExp(exceptions[i], "gi"))) && (url.match(new RegExp(exceptions[i], "gi")).length > 0);
2017-08-23 16:21:09 +03:00
}
2017-08-04 03:43:36 +03:00
return result;
};
/**
* Add a redirection to the redirections array.
*
* @param String redirection RegExp as string
*/
this.addRedirection = function(redirection) {
redirections.push(redirection);
};
/**
* Return all redirection.
*
* @return url
*/
this.getRedirection = function(url) {
var re = null;
for(var i = 0; i < redirections.length; i++)
{
result = (url.match(new RegExp(redirections[i], "gi")));
if (result && result.length > 0)
{
re = (new RegExp(redirections[i], "gi")).exec(url)[1];
break;
}
}
return re;
};
2017-08-04 03:43:36 +03:00
}
// ##################################################################
/**
* 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
*/
2017-08-04 03:43:36 +03:00
function removeFieldsFormURL(provider, request)
{
var url = request.url;
var rules = provider.getRules();
var changes = false;
var cancel = false;
2017-08-04 03:43:36 +03:00
if(provider.matchURL(url))
{
/*
* Expand the url by provider redirections. So no tracking on
* url redirections form sites to sites.
*/
var re = provider.getRedirection(url);
if(re !== null)
{
url = decodeURIComponent(re);
return {
"redirect": true,
"url": url
};
}
2017-08-04 03:43:36 +03:00
for (var i = 0; i < rules.length; i++) {
var bevorReplace = url;
2017-08-04 03:43:36 +03:00
url = url.replace(new RegExp(rules[i], "gi"), "");
if(bevorReplace != url)
{
2017-08-19 01:30:47 +03:00
if(badges[tabid] == null)
{
badges[tabid] = 0;
}
2017-08-23 15:01:48 +03:00
browser.storage.local.set({"globalCounter": ++globalCounter});
if(badgedStatus) {
browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid});
}
else
{
browser.browserAction.setBadgeText({text: "", tabId: tabid});
}
2017-08-04 03:43:36 +03:00
changes = true;
}
}
if(provider.isCaneling()){
2017-08-23 15:01:48 +03:00
if(badges[tabid] == null)
{
badges[tabid] = 0;
}
browser.storage.local.set({"globalCounter": ++globalCounter});
if(badgedStatus) {
browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid});
}
else
{
browser.browserAction.setBadgeText({text: "", tabId: tabid});
}
2017-08-23 15:01:48 +03:00
cancel = true;
}
2017-08-04 03:43:36 +03:00
}
return {
"changes": changes,
"url": url,
"cancel": cancel
};
}
2017-08-04 03:43:36 +03:00
2017-08-23 15:01:48 +03:00
/**
* Return the number of parameters query strings.
* @param {String} url URL as String
* @return {int} Number of Parameters
*/
2017-08-23 15:01:48 +03:00
function countFields(url)
{
var matches = (url.match(/[^\/|\?|&]+=[^\/|\?|&]+/gi) || []);
var count = matches.length;
2017-08-23 15:01:48 +03:00
return count;
}
2017-08-04 03:43:36 +03:00
/**
* Function which called from the webRequest to
* remove the tracking fields from the url.
*
* @param {webRequest} request webRequest-Object
* @return {Array} redirectUrl or none
*/
2017-08-04 03:43:36 +03:00
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;
}
2017-08-06 21:19:53 +03:00
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;
}
2017-08-06 21:19:53 +03:00
return clearUrl(request);
});
2017-08-04 03:43:36 +03:00
}
else {
var URLbeforeReplaceCount = countFields(request.url);
2017-08-06 21:19:53 +03:00
//Add Fields form Request to global url counter
globalurlcounter += URLbeforeReplaceCount;
2017-08-06 21:23:45 +03:00
browser.storage.local.set({"globalurlcounter": globalurlcounter});
browser.storage.local.get('globalStatus', clear);
2017-08-23 15:01:48 +03:00
function clear(data){
globalStatus = data.globalStatus;
if(globalStatus == null){
globalStatus = true;
2017-08-06 21:19:53 +03:00
}
}
if(globalStatus){
var result = {
"changes": false,
"url": "",
"redirect": false,
"cancel": false
};
2017-08-23 15:01:48 +03:00
/*
* Call for every provider the removeFieldsFormURL method.
*/
for (var i = 0; i < providers.length; i++) {
result = removeFieldsFormURL(providers[i], request);
/*
* Expand urls and bypass tracking.
* Cancel the active request.
*/
if(result.redirect)
{
browser.tabs.update(request.tabId, {url: result.url});
return {cancel: true};
}
/*
* Cancel the Request and redirect to the site blocked alert page,
* to inform the user about the full url blocking.
*/
if(result.cancel){
return {
redirectUrl: siteBlockedAlert
};
}
/*
* Ensure that the function go not into
* an loop.
*/
if(result.changes){
return {
redirectUrl: result.url
};
}
}
2017-08-23 15:01:48 +03:00
}
}
}
2017-08-04 03:43:36 +03:00
2017-08-19 01:30:47 +03:00
/**
* Call by each tab is closed.
*/
2017-08-19 01:30:47 +03:00
function handleRemoved(tabId, removeInfo) {
delete badges[tabId];
}
/**
* 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;
browser.browserAction.setBadgeBackgroundColor({
'color': 'orange'
});
}
else if(data.badgedStatus === null || typeof(data.badgedStatus) == "undefined"){
badgedStatus = false;
}
else {
badgedStatus = false;
}
});
}
2017-08-31 22:19:51 +03:00
/**
* Call the fetch, counter and status functions
*/
2017-08-31 22:19:51 +03:00
fetchFromURL();
setBadgedStatus();
/**
* Call by each change in the browser storage.
*/
browser.storage.onChanged.addListener(setBadgedStatus);
2017-08-19 01:30:47 +03:00
/**
* Call by each tab is closed.
*/
2017-08-19 01:30:47 +03:00
browser.tabs.onRemoved.addListener(handleRemoved);
/**
* Call by each tab change to set the actual tab id
*/
2017-08-19 01:30:47 +03:00
function handleActivated(activeInfo) {
tabid = activeInfo.tabId;
}
/**
* Call by each tab change.
*/
2017-08-19 01:30:47 +03:00
browser.tabs.onActivated.addListener(handleActivated);
2017-08-04 03:43:36 +03:00
/**
* Call by each Request and checking the url.
*
* @type {Array}
*/
2017-08-04 03:43:36 +03:00
browser.webRequest.onBeforeRequest.addListener(
clearUrl,
{urls: ["<all_urls>"]},
["blocking"]
);