2019-03-13 12:20:24 +00:00
|
|
|
/*
|
2019-03-14 16:59:16 +00:00
|
|
|
* ClearURLs
|
|
|
|
* Copyright (c) 2017-2019 Kevin Röbert
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-03-13 12:20:24 +00:00
|
|
|
|
2019-02-13 17:02:08 +00:00
|
|
|
/*jshint esversion: 6 */
|
2017-08-04 01:08:46 +00:00
|
|
|
/*
|
2019-02-13 17:02:08 +00:00
|
|
|
* This script is responsible for the core functionalities.
|
2017-09-14 01:39:40 +00:00
|
|
|
*/
|
2017-08-04 02:20:12 +00:00
|
|
|
var providers = [];
|
|
|
|
var prvKeys = [];
|
2017-08-18 22:30:47 +00:00
|
|
|
var badges = [];
|
|
|
|
var tabid = 0;
|
2018-01-05 16:26:17 +00:00
|
|
|
var siteBlockedAlert = 'javascript:void(0)';
|
2017-11-18 00:34:01 +00:00
|
|
|
var dataHash;
|
|
|
|
var localDataHash;
|
2018-01-25 21:37:02 +00:00
|
|
|
var os;
|
2018-08-31 18:55:15 +00:00
|
|
|
var currentURL;
|
2017-11-18 00:34:01 +00:00
|
|
|
|
2019-04-10 16:08:10 +00:00
|
|
|
/**
|
|
|
|
* Helper function which remove the tracking fields
|
|
|
|
* for each provider given as parameter.
|
|
|
|
*
|
|
|
|
* @param {Provider} provider Provider-Object
|
|
|
|
* @return {Array} Array with changes and url fields
|
|
|
|
*/
|
|
|
|
function removeFieldsFormURL(provider, pureUrl)
|
|
|
|
{
|
|
|
|
var url = pureUrl;
|
2019-06-12 18:17:49 +00:00
|
|
|
var domain = "";
|
|
|
|
var fragments = "";
|
2019-04-10 16:08:10 +00:00
|
|
|
var fields = "";
|
|
|
|
var rules = provider.getRules();
|
|
|
|
var changes = false;
|
|
|
|
var cancel = false;
|
2019-07-30 18:29:41 +00:00
|
|
|
var rawRules = provider.getRawRules();
|
|
|
|
|
2019-09-11 16:08:41 +00:00
|
|
|
if(storage.localHostsSkipping && checkLocalURL(pureUrl)) {
|
|
|
|
return {
|
|
|
|
"changes": false,
|
|
|
|
"url": url,
|
|
|
|
"cancel": false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:29:41 +00:00
|
|
|
/*
|
|
|
|
* Apply raw rules to the URL.
|
|
|
|
*/
|
|
|
|
rawRules.forEach(function(rawRule) {
|
|
|
|
var beforReplace = url;
|
|
|
|
url = url.replace(new RegExp(rawRule, "gi"), "");
|
|
|
|
|
|
|
|
if(beforReplace !== url) {
|
|
|
|
//Log the action
|
|
|
|
if(storage.loggingStatus)
|
|
|
|
{
|
|
|
|
pushToLog(beforReplace, url, rawRule);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(badges[tabid] == null) badges[tabid] = 0;
|
|
|
|
|
|
|
|
increaseURLCounter();
|
|
|
|
|
2019-09-11 16:08:41 +00:00
|
|
|
checkOSAndroid().then((res) => {
|
|
|
|
if(!res) {
|
|
|
|
|
|
|
|
if(storage.badgedStatus) {
|
|
|
|
browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
browser.browserAction.setBadgeText({text: "", tabId: tabid});
|
|
|
|
}
|
2019-07-30 18:29:41 +00:00
|
|
|
}
|
2019-09-11 16:08:41 +00:00
|
|
|
});
|
2019-07-30 18:29:41 +00:00
|
|
|
|
|
|
|
changes = true;
|
|
|
|
}
|
|
|
|
});
|
2019-04-10 16:08:10 +00:00
|
|
|
|
2019-06-12 18:17:49 +00:00
|
|
|
if(existsFragments(url)) {
|
|
|
|
domain = url.replace(new RegExp("#.*", "i"), "");
|
|
|
|
}
|
|
|
|
if(existsFields(url)) {
|
|
|
|
domain = url.replace(new RegExp("\\?.*", "i"), "");
|
|
|
|
}
|
|
|
|
|
2019-04-10 16:08:10 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
//Log the action
|
|
|
|
pushToLog(pureUrl, url, translate('log_redirect'));
|
|
|
|
|
|
|
|
return {
|
|
|
|
"redirect": true,
|
|
|
|
"url": url
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:24:46 +00:00
|
|
|
if(existsFields(url)) {
|
|
|
|
fields = "?"+extractFileds(url).rmEmpty().join("&");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(existsFragments(url)) {
|
|
|
|
fragments = "#"+extractFragments(url).rmEmpty().join("&");
|
|
|
|
}
|
|
|
|
|
2019-04-10 16:08:10 +00:00
|
|
|
/**
|
2019-06-12 18:17:49 +00:00
|
|
|
* Only test for matches, if there are fields or fragments that can be cleaned.
|
2019-04-10 16:08:10 +00:00
|
|
|
*/
|
2019-06-13 23:24:46 +00:00
|
|
|
if(fields !== "" || fragments !== "")
|
2019-04-10 16:08:10 +00:00
|
|
|
{
|
|
|
|
rules.forEach(function(rule) {
|
|
|
|
var beforReplace = fields;
|
2019-06-12 18:17:49 +00:00
|
|
|
var beforReplaceFragments = fragments;
|
2019-04-10 16:08:10 +00:00
|
|
|
fields = fields.replace(new RegExp(rule, "gi"), "");
|
2019-06-12 18:17:49 +00:00
|
|
|
fragments = fragments.replace(new RegExp(rule, "gi"), "");
|
2019-04-10 16:08:10 +00:00
|
|
|
|
2019-06-12 18:17:49 +00:00
|
|
|
if(beforReplace !== fields || beforReplaceFragments !== fragments)
|
2019-04-10 16:08:10 +00:00
|
|
|
{
|
|
|
|
//Log the action
|
|
|
|
if(storage.loggingStatus)
|
|
|
|
{
|
2019-06-12 18:17:49 +00:00
|
|
|
var tempURL = domain;
|
|
|
|
var tempBeforeURL = domain;
|
|
|
|
|
2019-06-13 23:24:46 +00:00
|
|
|
if(fields !== "") tempURL += "?"+fields.replace("?&", "?").replace("?", "");
|
|
|
|
if(fragments !== "") tempURL += "#"+fragments.replace("#&","#").replace("#", "");
|
|
|
|
if(beforReplace !== "") tempBeforeURL += "?"+beforReplace.replace("?&", "?").replace("?", "");
|
|
|
|
if(beforReplaceFragments !== "") tempBeforeURL += "#"+beforReplaceFragments.replace("#&","#").replace("#", "");
|
2019-06-12 18:17:49 +00:00
|
|
|
|
|
|
|
pushToLog(tempBeforeURL, tempURL, rule);
|
2019-04-10 16:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(badges[tabid] == null) badges[tabid] = 0;
|
|
|
|
|
|
|
|
increaseURLCounter();
|
|
|
|
|
2019-09-11 16:08:41 +00:00
|
|
|
checkOSAndroid().then((res) => {
|
|
|
|
if(!res) {
|
|
|
|
|
|
|
|
if(storage.badgedStatus) {
|
|
|
|
browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
browser.browserAction.setBadgeText({text: "", tabId: tabid});
|
|
|
|
}
|
2019-04-10 16:08:10 +00:00
|
|
|
}
|
2019-09-11 16:08:41 +00:00
|
|
|
});
|
|
|
|
|
2019-04-10 16:08:10 +00:00
|
|
|
changes = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-12 18:17:49 +00:00
|
|
|
var finalURL = domain;
|
|
|
|
|
2019-06-13 23:24:46 +00:00
|
|
|
if(fields !== "") finalURL += "?"+fields.replace("?", "");
|
|
|
|
if(fragments !== "") finalURL += "#"+fragments.replace("#", "");
|
2019-06-12 18:17:49 +00:00
|
|
|
|
2019-06-13 23:24:46 +00:00
|
|
|
url = finalURL.replace(new RegExp("\\?&"), "?").replace(new RegExp("#&"), "#");
|
2019-04-10 16:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(provider.isCaneling()){
|
|
|
|
pushToLog(pureUrl, pureUrl, translate('log_domain_blocked'));
|
|
|
|
if(badges[tabid] == null)
|
|
|
|
{
|
|
|
|
badges[tabid] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
increaseURLCounter();
|
|
|
|
|
2019-09-11 16:08:41 +00:00
|
|
|
checkOSAndroid().then((res) => {
|
|
|
|
if(!res) {
|
|
|
|
if(storage.badgedStatus) {
|
|
|
|
browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
browser.browserAction.setBadgeText({text: "", tabId: tabid});
|
|
|
|
}
|
2019-04-10 16:08:10 +00:00
|
|
|
}
|
2019-09-11 16:08:41 +00:00
|
|
|
});
|
2019-04-10 16:08:10 +00:00
|
|
|
|
|
|
|
cancel = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"changes": changes,
|
|
|
|
"url": url,
|
|
|
|
"cancel": cancel
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-13 17:02:08 +00:00
|
|
|
function start()
|
2018-05-17 23:31:04 +00:00
|
|
|
{
|
2018-01-25 22:03:18 +00:00
|
|
|
/**
|
2018-05-17 23:31:04 +00:00
|
|
|
* Save OS Version
|
2018-01-25 22:03:18 +00:00
|
|
|
*/
|
2019-04-01 21:53:28 +00:00
|
|
|
chrome.runtime.getPlatformInfo(function(info) {
|
2018-05-17 23:31:04 +00:00
|
|
|
os = info.os;
|
2019-03-18 16:39:47 +00:00
|
|
|
changeIcon();
|
2017-08-04 02:20:12 +00:00
|
|
|
|
|
|
|
|
2018-05-17 23:31:04 +00:00
|
|
|
/**
|
|
|
|
* Initialize the JSON provider object keys.
|
|
|
|
*
|
|
|
|
* @param {JSON Object} obj
|
|
|
|
*/
|
|
|
|
function getKeys(obj){
|
|
|
|
for(var key in obj){
|
|
|
|
prvKeys.push(key);
|
2018-01-25 22:03:18 +00:00
|
|
|
}
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the providers form the JSON object.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function createProviders()
|
|
|
|
{
|
|
|
|
data = storage.ClearURLsData;
|
2017-09-18 15:50:36 +00:00
|
|
|
|
2018-05-17 23:31:04 +00:00
|
|
|
for(var p = 0; p < prvKeys.length; p++)
|
2018-01-25 22:03:18 +00:00
|
|
|
{
|
2018-05-17 23:31:04 +00:00
|
|
|
//Create new provider
|
2019-08-04 17:48:26 +00:00
|
|
|
providers.push(new Provider(prvKeys[p], data.providers[prvKeys[p]].completeProvider,
|
|
|
|
data.providers[prvKeys[p]].forceRedirection));
|
2017-08-04 02:20:12 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
//Add URL Pattern
|
|
|
|
providers[p].setURLPattern(data.providers[prvKeys[p]].urlPattern);
|
2017-08-31 19:19:51 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
//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]);
|
|
|
|
}
|
2017-08-31 19:19:51 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
//Add raw rules to provider
|
|
|
|
for(var raw = 0; raw < data.providers[prvKeys[p]].rawRules.length; raw++)
|
|
|
|
{
|
|
|
|
providers[p].addRawRule(data.providers[prvKeys[p]].rawRules[raw]);
|
|
|
|
}
|
2019-07-30 18:29:41 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
//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]);
|
|
|
|
}
|
2017-11-18 00:34:01 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
//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]);
|
|
|
|
}
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-31 19:19:51 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Convert the external data to Objects and
|
|
|
|
* call the create provider function.
|
|
|
|
*
|
|
|
|
* @param {String} retrievedText - pure data form github
|
|
|
|
*/
|
|
|
|
function toObject(retrievedText) {
|
|
|
|
getKeys(storage.ClearURLsData.providers);
|
|
|
|
createProviders();
|
|
|
|
}
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* 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(storage.hashURL)
|
|
|
|
.then(function(response){
|
|
|
|
var responseTextHash = response.clone().text().then(function(responseTextHash){
|
|
|
|
if(response.ok && $.trim(responseTextHash))
|
2018-05-17 23:31:04 +00:00
|
|
|
{
|
2019-08-04 17:48:26 +00:00
|
|
|
dataHash = responseTextHash;
|
|
|
|
|
|
|
|
if($.trim(dataHash) !== $.trim(localDataHash))
|
|
|
|
{
|
|
|
|
fetchFromURL();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
toObject(storage.ClearURLsData);
|
|
|
|
storeHashStatus(1);
|
2019-09-12 16:53:33 +00:00
|
|
|
saveOnDisk(['hashStatus']);
|
2019-08-04 17:48:26 +00:00
|
|
|
}
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-08-04 17:48:26 +00:00
|
|
|
dataHash = false;
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
2019-08-04 17:48:26 +00:00
|
|
|
});
|
2018-05-17 23:31:04 +00:00
|
|
|
});
|
2019-08-04 17:48:26 +00:00
|
|
|
}
|
2018-01-25 22:03:18 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/*
|
|
|
|
* ##################################################################
|
|
|
|
* # Fetch Rules & Exception from URL #
|
|
|
|
* ##################################################################
|
|
|
|
*/
|
|
|
|
function fetchFromURL()
|
2018-05-17 23:31:04 +00:00
|
|
|
{
|
2019-08-04 17:48:26 +00:00
|
|
|
fetch(storage.ruleURL)
|
|
|
|
.then(checkResponse);
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
function checkResponse(response)
|
|
|
|
{
|
|
|
|
var responseText = response.clone().text().then(function(responseText){
|
|
|
|
if(response.ok && $.trim(responseText))
|
2018-05-17 23:31:04 +00:00
|
|
|
{
|
2019-08-04 17:48:26 +00:00
|
|
|
var downloadedFileHash = $.sha256(responseText);
|
|
|
|
|
|
|
|
if($.trim(downloadedFileHash) === $.trim(dataHash))
|
|
|
|
{
|
|
|
|
storage.ClearURLsData = responseText;
|
|
|
|
storage.dataHash = downloadedFileHash;
|
|
|
|
storeHashStatus(2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
storeHashStatus(3);
|
|
|
|
}
|
|
|
|
storage.ClearURLsData = JSON.parse(storage.ClearURLsData);
|
|
|
|
toObject(storage.ClearURLsData);
|
2019-09-12 16:45:56 +00:00
|
|
|
saveOnDisk(['ClearURLsData', 'dataHash', 'hashStatus']);
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
2019-08-04 17:48:26 +00:00
|
|
|
});
|
|
|
|
}
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
2017-08-04 01:08:46 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
// ##################################################################
|
2017-08-04 00:43:36 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/*
|
|
|
|
* ##################################################################
|
|
|
|
* # Supertyp Provider #
|
|
|
|
* ##################################################################
|
2018-05-17 23:31:04 +00:00
|
|
|
*/
|
|
|
|
/**
|
2019-08-04 17:48:26 +00:00
|
|
|
* Declare constructor
|
2018-05-17 23:31:04 +00:00
|
|
|
*
|
2019-08-04 17:48:26 +00:00
|
|
|
* @param {String} _name Provider name
|
|
|
|
* @param {boolean} _completeProvider Set URL Pattern as rule
|
|
|
|
* @param {boolean} _forceRedirection Whether redirects should be enforced via a "tabs.update"
|
|
|
|
* @param {boolean} _isActive Is the provider active?
|
2018-05-17 23:31:04 +00:00
|
|
|
*/
|
2019-08-04 17:48:26 +00:00
|
|
|
function Provider(_name, _completeProvider = false, _forceRedirection = false, _isActive = true){
|
|
|
|
var name = _name;
|
|
|
|
var urlPattern;
|
|
|
|
var enabled_rules = {};
|
|
|
|
var disabled_rules = {};
|
|
|
|
var enabled_exceptions = {};
|
|
|
|
var disabled_exceptions = {};
|
|
|
|
var canceling = _completeProvider;
|
|
|
|
var enabled_redirections = {};
|
|
|
|
var disabled_redirections = {};
|
|
|
|
var active = _isActive;
|
|
|
|
var enabled_rawRules = {};
|
|
|
|
var disabled_rawRules = {};
|
|
|
|
|
|
|
|
if(_completeProvider){
|
|
|
|
enabled_rules[".*"] = true;
|
|
|
|
}
|
2017-08-04 00:43:36 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Returns whether redirects should be enforced via a "tabs.update"
|
|
|
|
* @return {boolean} whether redirects should be enforced
|
|
|
|
*/
|
|
|
|
this.shouldForceRedirect = function() {
|
|
|
|
return _forceRedirection;
|
|
|
|
};
|
2017-08-04 00:43:36 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Returns the provider name.
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
this.getName = function() {
|
|
|
|
return name;
|
|
|
|
};
|
2017-08-04 00:43:36 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Add URL pattern.
|
|
|
|
*
|
|
|
|
* @require urlPatterns as RegExp
|
|
|
|
*/
|
|
|
|
this.setURLPattern = function(urlPatterns) {
|
|
|
|
urlPattern = new RegExp(urlPatterns, "i");
|
|
|
|
};
|
2019-03-12 17:13:23 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Return if the Provider Request is canceled
|
|
|
|
* @return {Boolean} isCanceled
|
|
|
|
*/
|
|
|
|
this.isCaneling = function() {
|
|
|
|
return canceling;
|
|
|
|
};
|
2019-02-13 17:02:08 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Check the url is matching the ProviderURL.
|
|
|
|
*
|
|
|
|
* @return {boolean} ProviderURL as RegExp
|
|
|
|
*/
|
|
|
|
this.matchURL = function(url) {
|
|
|
|
return urlPattern.test(url) && !(this.matchException(url));
|
|
|
|
};
|
2019-02-13 17:02:08 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Add a rule to the rule array
|
|
|
|
* and replace old rule with new rule.
|
|
|
|
*
|
|
|
|
* @param {String} rule RegExp as string
|
|
|
|
* @param {boolean} isActive Is this rule active?
|
|
|
|
*/
|
|
|
|
this.addRule = function(rule, isActive = true) {
|
|
|
|
rule = "([\\/|\\?|#]|(&|&))+("+rule+"=[^\\/|\\?|&]*)";
|
|
|
|
|
|
|
|
if(isActive)
|
2019-03-12 17:13:23 +00:00
|
|
|
{
|
2019-08-04 17:48:26 +00:00
|
|
|
enabled_rules[rule] = true;
|
|
|
|
|
|
|
|
if(disabled_rules[rule] !== undefined)
|
|
|
|
{
|
|
|
|
delete disabled_rules[rule];
|
|
|
|
}
|
2019-03-12 17:13:23 +00:00
|
|
|
}
|
2019-08-04 17:48:26 +00:00
|
|
|
else {
|
|
|
|
disabled_rules[rule] = true;
|
2017-09-13 21:35:40 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
if(enabled_rules[rule] !== undefined)
|
|
|
|
{
|
|
|
|
delete enabled_rules[rule];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-08-04 00:43:36 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Return all active rules as an array.
|
|
|
|
*
|
|
|
|
* @return Array RegExp strings
|
|
|
|
*/
|
|
|
|
this.getRules = function() {
|
|
|
|
return Object.keys(enabled_rules);
|
|
|
|
};
|
2019-07-30 18:29:41 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Add a raw rule to the raw rule array
|
|
|
|
* and replace old raw rule with new raw rule.
|
|
|
|
*
|
|
|
|
* @param {String} rule RegExp as string
|
|
|
|
* @param {boolean} isActive Is this rule active?
|
|
|
|
*/
|
|
|
|
this.addRawRule = function(rule, isActive = true) {
|
|
|
|
if(isActive)
|
2019-07-30 18:29:41 +00:00
|
|
|
{
|
2019-08-04 17:48:26 +00:00
|
|
|
enabled_rawRules[rule] = true;
|
2019-07-30 18:29:41 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
if(disabled_rawRules[rule] !== undefined)
|
|
|
|
{
|
|
|
|
delete disabled_rawRules[rule];
|
|
|
|
}
|
2019-07-30 18:29:41 +00:00
|
|
|
}
|
2019-08-04 17:48:26 +00:00
|
|
|
else {
|
|
|
|
disabled_rawRules[rule] = true;
|
2019-07-30 18:29:41 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
if(enabled_rawRules[rule] !== undefined)
|
|
|
|
{
|
|
|
|
delete enabled_rawRules[rule];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-07-30 18:29:41 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Return all active raw rules as an array.
|
|
|
|
*
|
|
|
|
* @return Array RegExp strings
|
|
|
|
*/
|
|
|
|
this.getRawRules = function() {
|
|
|
|
return Object.keys(enabled_rawRules);
|
|
|
|
};
|
2019-03-12 17:13:23 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Add a exception to the exceptions array
|
|
|
|
* and replace old with new exception.
|
|
|
|
*
|
|
|
|
* @param {String} exception RegExp as string
|
|
|
|
* @param {Boolean} isActive Is this exception acitve?
|
|
|
|
*/
|
|
|
|
this.addException = function(exception, isActive = true) {
|
|
|
|
if(isActive)
|
2019-02-13 17:02:08 +00:00
|
|
|
{
|
2019-08-04 17:48:26 +00:00
|
|
|
enabled_exceptions[exception] = true;
|
2019-02-13 17:02:08 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
if(disabled_exceptions[exception] !== undefined)
|
|
|
|
{
|
|
|
|
delete disabled_exceptions[exception];
|
|
|
|
}
|
2019-03-12 17:13:23 +00:00
|
|
|
}
|
2019-08-04 17:48:26 +00:00
|
|
|
else {
|
|
|
|
disabled_exceptions[exception] = true;
|
2017-09-13 21:35:40 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
if(enabled_exceptions[exception] !== undefined)
|
|
|
|
{
|
|
|
|
delete enabled_exceptions[exception];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-09-18 15:50:36 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Private helper method to check if the url
|
|
|
|
* an exception.
|
|
|
|
*
|
|
|
|
* @param {String} url RegExp as string
|
|
|
|
* @return {boolean} if matching? true: false
|
|
|
|
*/
|
|
|
|
this.matchException = function(url) {
|
|
|
|
var result = false;
|
2017-09-18 15:50:36 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
//Add the site blocked alert to every exception
|
|
|
|
if(url == siteBlockedAlert) return true;
|
2017-09-18 15:50:36 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
for(var exception in enabled_exceptions) {
|
|
|
|
if(result) break;
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
exception_regex = new RegExp(exception, "i");
|
|
|
|
result = exception_regex.test(url);
|
|
|
|
}
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
return result;
|
|
|
|
};
|
2019-03-12 17:13:23 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Add a redirection to the redirections array
|
|
|
|
* and replace old with new redirection.
|
|
|
|
*
|
|
|
|
* @param {String} redirection RegExp as string
|
|
|
|
* @param {Boolean} isActive Is this redirection active?
|
|
|
|
*/
|
|
|
|
this.addRedirection = function(redirection, isActive = true) {
|
|
|
|
if(isActive)
|
2019-03-12 17:13:23 +00:00
|
|
|
{
|
2019-08-04 17:48:26 +00:00
|
|
|
enabled_redirections[redirection] = true;
|
|
|
|
|
|
|
|
if(disabled_redirections[redirection] !== undefined)
|
|
|
|
{
|
|
|
|
delete disabled_redirections[redirection];
|
|
|
|
}
|
2019-02-13 17:02:08 +00:00
|
|
|
}
|
2019-08-04 17:48:26 +00:00
|
|
|
else {
|
|
|
|
disabled_redirections[redirection] = true;
|
2019-02-13 17:02:08 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
if(enabled_redirections[redirection] !== undefined)
|
|
|
|
{
|
|
|
|
delete enabled_redirections[redirection];
|
|
|
|
}
|
2019-03-12 17:13:23 +00:00
|
|
|
}
|
2019-08-04 17:48:26 +00:00
|
|
|
};
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Return all redirection.
|
|
|
|
*
|
|
|
|
* @return url
|
|
|
|
*/
|
|
|
|
this.getRedirection = function(url) {
|
|
|
|
var re = null;
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
for(var redirection in enabled_redirections) {
|
|
|
|
result = (url.match(new RegExp(redirection, "i")));
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
if (result && result.length > 0 && redirection)
|
|
|
|
{
|
|
|
|
re = (new RegExp(redirection, "i")).exec(url)[1];
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
return re;
|
2018-01-25 22:03:18 +00:00
|
|
|
};
|
2019-08-04 17:48:26 +00:00
|
|
|
}
|
|
|
|
// ##################################################################
|
2017-09-14 01:39:40 +00:00
|
|
|
|
2019-08-04 17:48:26 +00: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
|
|
|
|
*/
|
|
|
|
function clearUrl(request)
|
|
|
|
{
|
|
|
|
var URLbeforeReplaceCount = countFields(request.url);
|
2018-06-16 19:06:51 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
//Add Fields form Request to global url counter
|
|
|
|
increaseGlobalURLCounter(URLbeforeReplaceCount);
|
2018-01-25 22:03:18 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
if(storage.globalStatus){
|
|
|
|
var result = {
|
|
|
|
"changes": false,
|
|
|
|
"url": "",
|
|
|
|
"redirect": false,
|
|
|
|
"cancel": false
|
|
|
|
};
|
2018-01-25 22:03:18 +00:00
|
|
|
|
|
|
|
/*
|
2019-08-04 17:48:26 +00:00
|
|
|
* Call for every provider the removeFieldsFormURL method.
|
2018-01-25 22:03:18 +00:00
|
|
|
*/
|
2019-08-04 17:48:26 +00:00
|
|
|
for (var i = 0; i < providers.length; i++) {
|
2018-01-25 22:03:18 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
if(providers[i].matchURL(request.url))
|
|
|
|
{
|
|
|
|
result = removeFieldsFormURL(providers[i], request.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand urls and bypass tracking.
|
|
|
|
* Cancel the active request.
|
|
|
|
*/
|
|
|
|
if(result.redirect)
|
|
|
|
{
|
|
|
|
if(providers[i].shouldForceRedirect()) {
|
|
|
|
browser.tabs.update(request.tabId, {url: result.url});
|
|
|
|
return {cancel: true};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
redirectUrl: result.url
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* a loop.
|
|
|
|
*/
|
|
|
|
if(result.changes){
|
|
|
|
return {
|
|
|
|
redirectUrl: result.url
|
|
|
|
};
|
|
|
|
}
|
2018-01-25 22:03:18 +00:00
|
|
|
}
|
2017-09-14 01:39:40 +00:00
|
|
|
}
|
2018-01-25 22:03:18 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
// Default case
|
|
|
|
return {};
|
|
|
|
}
|
2018-01-05 16:26:17 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Call loadOldDataFromStore, getHash, counter, status and log functions
|
|
|
|
*/
|
2017-11-18 00:34:01 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
loadOldDataFromStore();
|
|
|
|
getHash();
|
|
|
|
setBadgedStatus();
|
2017-11-18 00:34:01 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Call by each tab is updated.
|
|
|
|
* And if url has changed.
|
|
|
|
*/
|
|
|
|
function handleUpdated(tabId, changeInfo, tabInfo) {
|
|
|
|
if(changeInfo.url)
|
|
|
|
{
|
|
|
|
delete badges[tabId];
|
|
|
|
}
|
|
|
|
currentURL = tabInfo.url;
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
2017-11-18 00:34:01 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Call by each tab is updated.
|
|
|
|
*/
|
|
|
|
browser.tabs.onUpdated.addListener(handleUpdated);
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Call by each tab change to set the actual tab id
|
|
|
|
*/
|
|
|
|
function handleActivated(activeInfo) {
|
|
|
|
tabid = activeInfo.tabId;
|
|
|
|
browser.tabs.get(tabid).then(function (tab) {
|
|
|
|
currentURL = tab.url;
|
|
|
|
});
|
|
|
|
}
|
2018-01-25 21:37:02 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Call by each tab change.
|
|
|
|
*/
|
|
|
|
browser.tabs.onActivated.addListener(handleActivated);
|
2017-11-18 00:34:01 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Check the request.
|
|
|
|
*/
|
|
|
|
function promise(requestDetails)
|
2018-05-17 23:31:04 +00:00
|
|
|
{
|
2019-08-04 17:48:26 +00:00
|
|
|
if(isDataURL(requestDetails))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var ret = clearUrl(requestDetails);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-05-17 23:31:04 +00:00
|
|
|
}
|
2017-08-23 00:24:16 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2018-05-17 23:31:04 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
return s.substring(0,4) == "data";
|
|
|
|
}
|
2017-12-16 22:50:50 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* Call by each Request and checking the url.
|
|
|
|
*
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
|
|
|
browser.webRequest.onBeforeRequest.addListener(
|
|
|
|
promise,
|
|
|
|
{urls: ["<all_urls>"], types: getData("types")},
|
|
|
|
["blocking"]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2019-04-10 16:08:10 +00:00
|
|
|
|
2019-08-04 17:48:26 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
2019-04-10 16:08:10 +00:00
|
|
|
{
|
2019-08-04 17:48:26 +00:00
|
|
|
if(storage.loggingStatus)
|
|
|
|
{
|
|
|
|
storage.log.log.push(
|
|
|
|
{
|
|
|
|
"before": beforeProcessing,
|
|
|
|
"after": afterProcessing,
|
|
|
|
"rule": rule,
|
|
|
|
"timestamp": Date.now()
|
|
|
|
}
|
|
|
|
);
|
2019-09-12 16:45:56 +00:00
|
|
|
deferSaveOnDisk('log');
|
2019-08-04 17:48:26 +00:00
|
|
|
}
|
2019-04-10 16:08:10 +00:00
|
|
|
}
|