Version 1.9.2

#290
This commit is contained in:
Kevin Röbert
2019-11-09 01:40:17 +01:00
parent 61b911921e
commit 61712e77a2
8 changed files with 874 additions and 817 deletions

View File

@@ -25,10 +25,9 @@ var hasPendingSaves = false;
var pendingSaves = new Set();
/**
* Writes the storage variable to the disk.
*/
function saveOnExit()
{
* Writes the storage variable to the disk.
*/
function saveOnExit() {
saveOnDisk(Object.keys(storage));
}
@@ -65,14 +64,13 @@ function storageDataAsString(key) {
}
/**
* Save multiple keys on the disk.
* @param {String[]} keys
*/
function saveOnDisk(keys)
{
* Save multiple keys on the disk.
* @param {String[]} keys
*/
function saveOnDisk(keys) {
let json = {};
keys.forEach(function(key) {
keys.forEach(function (key) {
json[key] = storageDataAsString(key);
});
@@ -81,17 +79,16 @@ function saveOnDisk(keys)
}
/**
* Schedule to save a key to disk in 30 seconds.
* @param {String} key
*/
function deferSaveOnDisk(key)
{
* Schedule to save a key to disk in 30 seconds.
* @param {String} key
*/
function deferSaveOnDisk(key) {
if (hasPendingSaves) {
pendingSaves.add(key);
return;
}
setTimeout(function() {
setTimeout(function () {
saveOnDisk(Array.from(pendingSaves));
pendingSaves.clear();
hasPendingSaves = false;
@@ -100,10 +97,9 @@ function deferSaveOnDisk(key)
}
/**
* Start sequence for ClearURLs.
*/
function genesis()
{
* Start sequence for ClearURLs.
*/
function genesis() {
browser.storage.local.get(null).then((items) => {
initStorage(items);
@@ -119,35 +115,32 @@ function genesis()
}
/**
* Return the value under the key.
* @param {String} key
* @return {Object}
*/
function getData(key)
{
* Return the value under the key.
* @param {String} key
* @return {Object}
*/
function getData(key) {
return storage[key];
}
/**
* Return the entire storage object.
* @return {Object}
*/
function getEntireData()
{
* Return the entire storage object.
* @return {Object}
*/
function getEntireData() {
return storage;
}
/**
* Save the value under the key on the RAM.
*
* Note: To store the data on the hard disk, one of
* deferSaveOnDisk(), saveOnDisk(), or saveOnExit()
* must be called.
* @param {String} key
* @param {Object} value
*/
function setData(key, value)
{
* Save the value under the key on the RAM.
*
* Note: To store the data on the hard disk, one of
* deferSaveOnDisk(), saveOnDisk(), or saveOnExit()
* must be called.
* @param {String} key
* @param {Object} value
*/
function setData(key, value) {
switch (key) {
case "ClearURLsData":
case "log":
@@ -169,23 +162,21 @@ function setData(key, value)
}
/**
* Write error on console.
*/
function error(e)
{
* Write error on console.
*/
function error(e) {
console.log(translate('core_error'));
console.error(e);
}
/**
* Set default values, if the storage is empty.
* @param {Object} items
*/
function initStorage(items)
{
* Set default values, if the storage is empty.
* @param {Object} items
*/
function initStorage(items) {
initSettings();
if(!isEmpty(items)) {
if (!isEmpty(items)) {
Object.entries(items).forEach(([key, value]) => {
setData(key, value);
});
@@ -193,10 +184,9 @@ function initStorage(items)
}
/**
* Set default values for the settings.
*/
function initSettings()
{
* Set default values for the settings.
*/
function initSettings() {
storage.ClearURLsData = [];
storage.dataHash = "";
storage.badgedStatus = true;
@@ -208,15 +198,15 @@ function initSettings()
storage.log = {"log": []};
storage.statisticsStatus = true;
storage.badged_color = "ffa500";
storage.hashURL = "https://gitlab.com/KevinRoebert/ClearUrls/-/jobs/artifacts/master/raw/rules.min.hash?job=hash%20rules";
storage.ruleURL = "https://gitlab.com/KevinRoebert/ClearUrls/raw/master/data/data.min.json";
storage.hashURL = "https://gitlab.com/KevinRoebert/ClearUrls/-/jobs/artifacts/master/raw/rules.minify.hash?job=hash%20rules";
storage.ruleURL = "https://gitlab.com/KevinRoebert/ClearUrls/raw/master/data/data.minify.json";
storage.contextMenuEnabled = true;
storage.historyListenerEnabled = true;
storage.localHostsSkipping = true;
storage.referralMarketing = false;
storage.logLimit = -1;
if(getBrowser() === "Firefox") {
if (getBrowser() === "Firefox") {
storage.types = ["font", "image", "imageset", "main_frame", "media", "object", "object_subrequest", "other", "script", "stylesheet", "sub_frame", "websocket", "xbl", "xml_dtd", "xmlhttprequest", "xslt"];
} else if (getBrowser() === "Chrome") {
storage.types = ["main_frame", "sub_frame", "stylesheet", "script", "image", "font", "object", "xmlhttprequest", "ping", "csp_report", "media", "websocket", "other"];
@@ -224,53 +214,57 @@ function initSettings()
}
/**
* Replace the old URLs with the
* new GitLab URLs.
*/
function replaceOldURLs(url)
{
* Replace the old URLs with the
* new GitLab URLs.
*/
function replaceOldURLs(url) {
switch (url) {
case "https://raw.githubusercontent.com/KevinRoebert/ClearUrls/master/data/rules.hash?flush_cache=true":
return "https://gitlab.com/KevinRoebert/ClearUrls/-/jobs/artifacts/master/raw/rules.min.hash?job=hash%20rules";
return "https://gitlab.com/KevinRoebert/ClearUrls/-/jobs/artifacts/master/raw/rules.min.hash?job=hash%20rules";
case "https://raw.githubusercontent.com/KevinRoebert/ClearUrls/master/data/data.json?flush_cache=true":
return "https://gitlab.com/KevinRoebert/ClearUrls/raw/master/data/data.min.json";
return "https://gitlab.com/KevinRoebert/ClearUrls/raw/master/data/data.min.json";
case "https://gitlab.com/KevinRoebert/ClearUrls/raw/master/data/rules.hash":
return "https://gitlab.com/KevinRoebert/ClearUrls/-/jobs/artifacts/master/raw/rules.min.hash?job=hash%20rules";
return "https://gitlab.com/KevinRoebert/ClearUrls/-/jobs/artifacts/master/raw/rules.min.hash?job=hash%20rules";
case "https://gitlab.com/KevinRoebert/ClearUrls/raw/master/data/data.json":
return "https://gitlab.com/KevinRoebert/ClearUrls/raw/master/data/data.min.json";
return "https://gitlab.com/KevinRoebert/ClearUrls/raw/master/data/data.min.json";
case "https://gitlab.com/KevinRoebert/ClearUrls/-/jobs/artifacts/master/raw/rules.min.hash?job=hash%20rules":
return "https://gitlab.com/KevinRoebert/ClearUrls/-/jobs/artifacts/master/raw/rules.minify.hash?job=hash%20rules";
case "https://gitlab.com/KevinRoebert/ClearUrls/raw/master/data/data.min.json":
return "https://gitlab.com/KevinRoebert/ClearUrls/-/jobs/artifacts/master/raw/data.minify.json?job=hash%20rules";
default:
return url;
return url;
}
}
/**
* 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() {
localDataHash = storage.dataHash;
}
/**
* Save the hash status to the local storage (RAM).
* 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 = "hash_status_code_1";
break;
case 2: status_code = "hash_status_code_2";
break;
case 3: status_code = "hash_status_code_3";
break;
default: status_code = "hash_status_code_4";
* Save the hash status to the local storage (RAM).
* 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 = "hash_status_code_1";
break;
case 2:
status_code = "hash_status_code_2";
break;
case 3:
status_code = "hash_status_code_3";
break;
default:
status_code = "hash_status_code_4";
}
storage.hashStatus = status_code;

View File

@@ -24,53 +24,49 @@
/*
* To support Waterfox.
*/
Array.prototype.rmEmpty = function() {
Array.prototype.rmEmpty = function () {
return this.filter(v => v);
};
/*
* To support Waterfox.
*/
Array.prototype.flatten = function() {
Array.prototype.flatten = function () {
return this.reduce((a, b) => a.concat(b), []);
};
/**
* Check if an object is empty.
* @param {Object} obj
* @return {Boolean}
*/
function isEmpty(obj)
{
* Check if an object is empty.
* @param {Object} obj
* @return {Boolean}
*/
function isEmpty(obj) {
return (Object.getOwnPropertyNames(obj).length === 0);
}
/**
* Translate a string with the i18n API.
*
* @param {string} string Name of the attribute used for localization
*/
function translate(string)
{
* Translate a string with the i18n API.
*
* @param {string} string Name of the attribute used for localization
*/
function translate(string) {
return browser.i18n.getMessage(string);
}
/**
* Reloads the extension.
*/
function reload()
{
* Reloads the extension.
*/
function reload() {
browser.runtime.reload();
}
/**
* Check if it is an android device.
* @return bool
*/
async function checkOSAndroid()
{
if(os === undefined || os === null || os === "") {
await chrome.runtime.getPlatformInfo(function(info) {
* Check if it is an android device.
* @return bool
*/
async function checkOSAndroid() {
if (os === undefined || os === null || os === "") {
await chrome.runtime.getPlatformInfo(function (info) {
os = info.os;
});
}
@@ -79,10 +75,10 @@ async function checkOSAndroid()
}
/**
* Extract the host without port from an url.
* @param {String} url URL as String
* @return {String} host as string
*/
* Extract the host without port from an url.
* @param {String} url URL as String
* @return {String} host as string
*/
function extractHost(url) {
let parsed_url = new URL(url);
@@ -90,36 +86,34 @@ function extractHost(url) {
}
/**
* Returns true if the url has a local host.
* @param {String} url URL as String
* @return {boolean}
*/
* Returns true if the url has a local host.
* @param {String} url URL as String
* @return {boolean}
*/
function checkLocalURL(url) {
let host = extractHost(url);
return ipRangeCheck(host, ["10.0.0.0/8", "172.16.0.0/12",
"192.168.0.0/16", "100.64.0.0/10",
"169.254.0.0/16", "127.0.0.1"])||
host === 'localhost';
"192.168.0.0/16", "100.64.0.0/10",
"169.254.0.0/16", "127.0.0.1"]) ||
host === 'localhost';
}
/**
* 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) {
return extractFileds(url).length;
}
/**
* Returns true if fields exists.
* @param {String} url URL as String
* @return {boolean}
*/
function existsFields(url)
{
* Returns true if fields exists.
* @param {String} url URL as String
* @return {boolean}
*/
function existsFields(url) {
let matches = (url.match(/\?.+/i) || []);
let count = matches.length;
@@ -127,15 +121,14 @@ function existsFields(url)
}
/**
* Extract the fields from an url.
* @param {String} url URL as String
* @return {Array} Fields as array
*/
function extractFileds(url)
{
if(existsFields(url)) {
* Extract the fields from an url.
* @param {String} url URL as String
* @return {Array} Fields as array
*/
function extractFileds(url) {
if (existsFields(url)) {
let fields = url.replace(new RegExp(".*?\\?", "i"), "");
if(existsFragments(url)) {
if (existsFragments(url)) {
fields = fields.replace(new RegExp("#.*", "i"), "");
}
@@ -146,23 +139,21 @@ function extractFileds(url)
}
/**
* Return the number of fragments query strings.
* @param {String} url URL as String
* @return {int} Number of fragments
*/
function countFragments(url)
{
* Return the number of fragments query strings.
* @param {String} url URL as String
* @return {int} Number of fragments
*/
function countFragments(url) {
return extractFragments(url).length;
}
/**
* Extract the fragments from an url.
* @param {String} url URL as String
* @return {Array} fragments as array
*/
function extractFragments(url)
{
if(existsFragments(url)) {
* Extract the fragments from an url.
* @param {String} url URL as String
* @return {Array} fragments as array
*/
function extractFragments(url) {
if (existsFragments(url)) {
let fragments = url.replace(new RegExp(".*?#", "i"), "");
return (fragments.match(/[^&]+=?[^&]*/gi) || []);
}
@@ -171,12 +162,11 @@ function extractFragments(url)
}
/**
* Returns true if fragments exists.
* @param {String} url URL as String
* @return {boolean}
*/
function existsFragments(url)
{
* Returns true if fragments exists.
* @param {String} url URL as String
* @return {boolean}
*/
function existsFragments(url) {
let matches = (url.match(/\#.+/i) || []);
let count = matches.length;
@@ -184,73 +174,69 @@ function existsFragments(url)
}
/**
* 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() {
localDataHash = storage.dataHash;
}
/**
* Save the hash status to the local storage (RAM).
* 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 = "hash_status_code_1";
break;
case 2: status_code = "hash_status_code_2";
break;
case 3: status_code = "hash_status_code_3";
break;
default: status_code = "hash_status_code_4";
* Save the hash status to the local storage (RAM).
* 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 = "hash_status_code_1";
break;
case 2:
status_code = "hash_status_code_2";
break;
case 3:
status_code = "hash_status_code_3";
break;
default:
status_code = "hash_status_code_4";
}
storage.hashStatus = status_code;
}
/**
* Increase by {number} the GlobalURLCounter
* @param {int} number
*/
function increaseGlobalURLCounter(number)
{
if(storage.statisticsStatus)
{
* Increase by {number} the GlobalURLCounter
* @param {int} number
*/
function increaseGlobalURLCounter(number) {
if (storage.statisticsStatus) {
storage.globalurlcounter += number;
deferSaveOnDisk('globalurlcounter');
}
}
/**
* Increase by one the URLCounter
*/
function increaseURLCounter()
{
if(storage.statisticsStatus)
{
* Increase by one the URLCounter
*/
function increaseURLCounter() {
if (storage.statisticsStatus) {
storage.globalCounter++;
deferSaveOnDisk('globalCounter');
}
}
/**
* Change the icon.
*/
function changeIcon()
{
* Change the icon.
*/
function changeIcon() {
checkOSAndroid().then((res) => {
if(!res) {
if(storage.globalStatus){
if (!res) {
if (storage.globalStatus) {
browser.browserAction.setIcon({path: "img/clearurls_128x128.png"});
} else{
} else {
browser.browserAction.setIcon({path: "img/clearurls_gray_128x128.png"});
}
}
@@ -258,35 +244,33 @@ function changeIcon()
}
/**
* 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() {
checkOSAndroid().then((res) => {
if(!res && storage.badgedStatus) {
if (!res && storage.badgedStatus) {
browser.browserAction.setBadgeBackgroundColor({
'color': '#'+storage.badged_color
'color': '#' + storage.badged_color
});
}
});
}
/**
* Returns the current URL.
* @return {String} [description]
*/
function getCurrentURL()
{
* Returns the current URL.
* @return {String} [description]
*/
function getCurrentURL() {
return currentURL;
}
/**
* Check for browser.
*/
* Check for browser.
*/
function getBrowser() {
if(typeof InstallTrigger !== 'undefined') {
if (typeof InstallTrigger !== 'undefined') {
return "Firefox";
} else {
return "Chrome";
@@ -299,9 +283,19 @@ function getBrowser() {
*/
function decodeURL(url) {
const rtn = decodeURIComponent(url);
if(rtn.indexOf("http://") === -1 && rtn.indexOf("https://") === -1) {
if (rtn.indexOf("http://") === -1 && rtn.indexOf("https://") === -1) {
return decodeURL(rtn);
}
return rtn;
}
}
/*
* Gets the value of at `key` an object. If the resolved value is `undefined`, the `defaultValue` is returned in its place.
*
* @param {string} key the key of the object
* @param {object} defaultValue the default value
*/
Object.prototype.getOrDefault = function (key, defaultValue) {
return this[key] === undefined ? defaultValue : this[key];
};