Version 1.2.2.0

Project memory management. I will test this version first and upload it to mozilla after the tests.
This commit is contained in:
Kevin Röbert 2018-05-18 01:31:04 +02:00
parent 5746828d35
commit ff88d9c0ae
4 changed files with 690 additions and 759 deletions

View File

@ -3,26 +3,28 @@
* # Fetch Rules & Exception from URL # * # Fetch Rules & Exception from URL #
* ################################################################## * ##################################################################
*/ */
var data = [];
var providers = []; var providers = [];
var prvKeys = []; var prvKeys = [];
var globalStatus;
var badges = []; var badges = [];
var log = [];
var logging = false;
var badgedStatus;
var tabid = 0; var tabid = 0;
var globalCounter;
var globalurlcounter;
var siteBlockedAlert = 'javascript:void(0)'; var siteBlockedAlert = 'javascript:void(0)';
var dataHash; var dataHash;
var localDataHash; var localDataHash;
var os; var os;
/** var storage = [];
* Save OS Version
*/ getDataFromDisk();
browser.runtime.getPlatformInfo(function(info) {
function start(items)
{
initStorage(items);
/**
* Save OS Version
*/
browser.runtime.getPlatformInfo(function(info) {
os = info.os; os = info.os;
@ -43,6 +45,8 @@ browser.runtime.getPlatformInfo(function(info) {
*/ */
function createProviders() function createProviders()
{ {
data = storage.ClearURLsData;
for(var p = 0; p < prvKeys.length; p++) for(var p = 0; p < prvKeys.length; p++)
{ {
//Create new provider //Create new provider
@ -78,8 +82,8 @@ browser.runtime.getPlatformInfo(function(info) {
* @param {String} retrievedText - pure data form github * @param {String} retrievedText - pure data form github
*/ */
function toJSON(retrievedText) { function toJSON(retrievedText) {
data = JSON.parse(retrievedText); storage.ClearURLsData = JSON.parse(retrievedText);
getKeys(data.providers); getKeys(storage.ClearURLsData.providers);
createProviders(); createProviders();
} }
@ -89,17 +93,7 @@ browser.runtime.getPlatformInfo(function(info) {
*/ */
function loadOldDataFromStore() function loadOldDataFromStore()
{ {
browser.storage.local.get('ClearURLsData', function(localData){ localDataHash = $.sha256(storage.ClearURLsData);
if(localData.ClearURLsData){
data = localData.ClearURLsData;
}
else {
data = "";
}
localDataHash = $.sha256(data);
});
} }
/** /**
@ -122,7 +116,8 @@ browser.runtime.getPlatformInfo(function(info) {
break; break;
default: status_code = "error"; default: status_code = "error";
} }
browser.storage.local.set({"hashStatus": status_code});
storage.hashStatus = status_code;
} }
/** /**
@ -134,7 +129,7 @@ browser.runtime.getPlatformInfo(function(info) {
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("http://127.0.0.1/clearurls/rules.hash")
.then(function(response){ .then(function(response){
var responseTextHash = response.clone().text().then(function(responseTextHash){ var responseTextHash = response.clone().text().then(function(responseTextHash){
if(response.ok) if(response.ok)
@ -146,7 +141,7 @@ browser.runtime.getPlatformInfo(function(info) {
fetchFromURL(); fetchFromURL();
} }
else { else {
toJSON(data); toJSON(storage.ClearURLsData);
storeHashStatus(1); storeHashStatus(1);
} }
} }
@ -162,7 +157,7 @@ browser.runtime.getPlatformInfo(function(info) {
*/ */
function fetchFromURL() function fetchFromURL()
{ {
fetch("https://raw.githubusercontent.com/KevinRoebert/ClearUrls/master/data/data.json?flush_cache=true") fetch("http://127.0.0.1/clearurls/data.json")
.then(checkResponse); .then(checkResponse);
function checkResponse(response) function checkResponse(response)
@ -174,14 +169,13 @@ browser.runtime.getPlatformInfo(function(info) {
if($.trim(downloadedFileHash) === $.trim(dataHash)) if($.trim(downloadedFileHash) === $.trim(dataHash))
{ {
data = responseText; storage.ClearURLsData = responseText;
browser.storage.local.set({"ClearURLsData": responseText});
storeHashStatus(2); storeHashStatus(2);
} }
else { else {
storeHashStatus(3); storeHashStatus(3);
} }
toJSON(data); toJSON(storage.ClearURLsData);
} }
}); });
} }
@ -378,11 +372,11 @@ browser.runtime.getPlatformInfo(function(info) {
{ {
badges[tabid] = 0; badges[tabid] = 0;
} }
storage.globalCounter++;
browser.storage.local.set({"globalCounter": ++globalCounter});
if(!checkOSAndroid()) if(!checkOSAndroid())
{ {
if(badgedStatus) { if(storage.badgedStatus) {
browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid}); browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid});
} }
else else
@ -402,10 +396,11 @@ browser.runtime.getPlatformInfo(function(info) {
badges[tabid] = 0; badges[tabid] = 0;
} }
browser.storage.local.set({"globalCounter": ++globalCounter}); storage.globalCounter++;
if(!checkOSAndroid()) if(!checkOSAndroid())
{ {
if(badgedStatus) { if(storage.badgedStatus) {
browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid}); browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid});
} }
else else
@ -452,57 +447,13 @@ browser.runtime.getPlatformInfo(function(info) {
*/ */
function clearUrl(request) 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;
}
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;
}
return clearUrl(request);
});
}
else {
var URLbeforeReplaceCount = countFields(request.url); var URLbeforeReplaceCount = countFields(request.url);
//Add Fields form Request to global url counter //Add Fields form Request to global url counter
globalurlcounter += URLbeforeReplaceCount;
browser.storage.local.set({"globalurlcounter": globalurlcounter}); storage.globalurlcounter += URLbeforeReplaceCount;
browser.storage.local.get('globalStatus', clear);
function clear(data){ if(storage.globalStatus){
globalStatus = data.globalStatus;
if(globalStatus == null){
globalStatus = true;
}
}
if(globalStatus){
var result = { var result = {
"changes": false, "changes": false,
@ -548,32 +499,11 @@ browser.runtime.getPlatformInfo(function(info) {
} }
} }
} }
}
// Default case // Default case
return {}; 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()
{
browser.storage.local.get('log', function(data) {
if(data.log)
{
log = JSON.parse(data.log);
}
else{
//Create foundation for log variable
log = {"log": []};
}
});
}
/** /**
* Function to log all activities from ClearUrls. * Function to log all activities from ClearUrls.
* Only logging when activated. * Only logging when activated.
@ -586,9 +516,9 @@ browser.runtime.getPlatformInfo(function(info) {
*/ */
function pushToLog(beforeProcessing, afterProcessing, rule) function pushToLog(beforeProcessing, afterProcessing, rule)
{ {
if(logging) if(storage.loggingStatus)
{ {
log.log.push( storage.log.log.push(
{ {
"before": beforeProcessing, "before": beforeProcessing,
"after": afterProcessing, "after": afterProcessing,
@ -599,90 +529,17 @@ browser.runtime.getPlatformInfo(function(info) {
} }
} }
/**
* 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) {
if(data.resetLog)
{
log = {"log": []}; // Delete the old log
browser.storage.local.set({"resetLog": false});
}
else
{
browser.storage.local.set({"log": JSON.stringify(log)});
}
});
}
}
/**
* 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;
}
else if(data.loggingStatus === null || typeof(data.loggingStatus) == "undefined"){
logging = false;
}
else {
logging = false;
}
});
}
/**
* 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);
/**
* Function that calls some function on storage change.
*/
function reactToStorageChange()
{
setBadgedStatus();
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() {
if(!checkOSAndroid()){ if(!checkOSAndroid() && storage.badgedStatus){
browser.storage.local.get('badgedStatus', function(data) {
if(data.badgedStatus) {
badgedStatus = data.badgedStatus;
browser.browserAction.setBadgeBackgroundColor({ browser.browserAction.setBadgeBackgroundColor({
'color': 'orange' 'color': 'orange'
}); });
} }
else if(data.badgedStatus === null || typeof(data.badgedStatus) == "undefined"){
badgedStatus = false;
}
else {
badgedStatus = false;
}
});
}
} }
/** /**
@ -707,12 +564,6 @@ browser.runtime.getPlatformInfo(function(info) {
loadOldDataFromStore(); loadOldDataFromStore();
getHash(); getHash();
setBadgedStatus(); setBadgedStatus();
getLogOnStart();
/**
* Call by each change in the browser storage.
*/
browser.storage.onChanged.addListener(reactToStorageChange);
/** /**
* Call by each tab is updated. * Call by each tab is updated.
@ -781,4 +632,113 @@ browser.runtime.getPlatformInfo(function(info) {
{urls: ["<all_urls>"]}, {urls: ["<all_urls>"]},
["blocking"] ["blocking"]
); );
}); });
}
/**
* Writes the storage variable to the disk.
*/
window.onbeforeunload = saveOnExit();
function saveOnExit()
{
var json = {};
Object.entries(storage).forEach(([key, value]) => {
switch (key) {
case "ClearURLsData":
case "log":
json[key] = JSON.stringify(value);
break;
default:
json[key] = value;
}
});
console.log("[ClearURLs]: Save on disk.");
browser.storage.local.set(json);
}
/**
* Save the value under the key on the disk.
* @param {String} key
* @param {Object} value
*/
function saveOnDisk(key, value)
{
browser.storage.local.set({key: value});
}
/**
* Retrieve everything and save on the RAM.
*/
function getDataFromDisk()
{
browser.storage.local.get().then(start, error);
}
/**
* Get the value under the key.
* @param {String} key
* @return {Object}
*/
function getData(key)
{
return storage[key];
}
/**
* Save the value under the key on the RAM.
* @param {String} key
* @param {Object} value
*/
function setData(key, value)
{
switch (key) {
case "ClearURLsData":
case "log":
storage[key] = JSON.parse(value);
break;
default:
storage[key] = value;
}
}
/**
* Write error on console.
*/
function error()
{
console.log("The addon could not started.");
}
/**
* Set default values, if the storage is empty.
* @param {Object} items
*/
function initStorage(items)
{
storage.ClearURLsData = [];
storage.badgedStatus = true;
storage.globalStatus = true;
storage.globalurlcounter = 0;
storage.globalCounter = 0;
storage.hashStatus = "error";
storage.loggingStatus = false;
storage.log = {"log": []};
if(!isEmpty(items)) {
Object.entries(items).forEach(([key, value]) => {
setData(key, value);
});
}
}
/**
* Check if an object is empty.
* @param {Object} obj
* @return {Boolean}
*/
function isEmpty(obj)
{
return (Object.getOwnPropertyNames(obj).length === 0);
}

View File

@ -1,14 +1,21 @@
/** /**
* Get the log and display the data as table. * Get the log and display the data as table.
*/ */
var log = []; var log = {};
var core = function (func) {
return browser.runtime.getBackgroundPage().then(func);
};
/** /**
* Reset the global log * Reset the global log
*/ */
function resetGlobalLog(){ function resetGlobalLog(){
browser.storage.local.remove("log"); core(function (ref){
browser.storage.local.set({"resetLog": true}); obj = {"log": []};
ref.setData('log', JSON.stringify(obj));
});
getLog();
location.reload(); location.reload();
} }
@ -17,15 +24,8 @@ function resetGlobalLog(){
*/ */
function getLog() function getLog()
{ {
browser.storage.local.get('log', function(data) { core(function (ref){
if(data.log) log = ref.getData('log');
{
log = JSON.parse(data.log);
}
else{
//Create foundation for log variable
log = {"log": []};
}
var length = Object.keys(log.log).length; var length = Object.keys(log.log).length;
var row; var row;
@ -42,7 +42,7 @@ function getLog()
} }
} }
$('#logTable').DataTable({ $('#logTable').DataTable({
"pageLength": 5 "pageLength": 10
} ).order([3, 'desc']).draw(); } ).order([3, 'desc']).draw();
}); });
} }

View File

@ -1,3 +1,32 @@
var element = $("#statistics_value");
var elGlobalPercentage = $("#statistics_value_global_percentage");
var elProgressbar_blocked = $('#progress_blocked');
var elProgressbar_non_blocked = $('#progress_non_blocked');
var elTotal = $('#statistics_total_elements');
var globalPercentage = 0;
var globalCounter;
var globalurlcounter;
var globalStatus;
var badgedStatus;
var hashStatus;
var loggingStatus;
var core = function (func) {
return browser.runtime.getBackgroundPage().then(func);
};
function getData()
{
core(function (ref){
globalCounter = ref.getData('globalCounter');
globalurlcounter = ref.getData('globalurlcounter');
globalStatus = ref.getData('globalStatus');
badgedStatus = ref.getData('badgedStatus');
hashStatus = ref.getData('hashStatus');
loggingStatus = ref.getData('loggingStatus');
});
}
/** /**
* Initialize the UI. * Initialize the UI.
* *
@ -16,43 +45,15 @@ function init()
* @param {(data){} Return value form browser.storage.local.get * @param {(data){} Return value form browser.storage.local.get
*/ */
function changeStatistics(){ function changeStatistics(){
var element = $("#statistics_value");
var elGlobalPercentage = $("#statistics_value_global_percentage");
var elProgressbar_blocked = $('#progress_blocked');
var elProgressbar_non_blocked = $('#progress_non_blocked');
var elTotal = $('#statistics_total_elements');
var globalPercentage = 0;
var globalCounter;
var globalurlcounter;
browser.storage.local.get('globalCounter', function(data){
if(data.globalCounter){
globalCounter = data.globalCounter;
}
else {
globalCounter = 0;
}
element.text(globalCounter.toLocaleString());
});
browser.storage.local.get('globalurlcounter', function(data){
if(data.globalurlcounter){
globalurlcounter = data.globalurlcounter;
}
else {
globalurlcounter = 0;
}
globalPercentage = ((globalCounter/globalurlcounter)*100).toFixed(3); globalPercentage = ((globalCounter/globalurlcounter)*100).toFixed(3);
if(isNaN(Number(globalPercentage))) globalPercentage = 0; if(isNaN(Number(globalPercentage))) globalPercentage = 0;
element.text(globalCounter.toLocaleString());
elGlobalPercentage.text(globalPercentage+"%"); elGlobalPercentage.text(globalPercentage+"%");
elProgressbar_blocked.css('width', globalPercentage+'%'); elProgressbar_blocked.css('width', globalPercentage+'%');
elProgressbar_non_blocked.css('width', (100-globalPercentage)+'%'); elProgressbar_non_blocked.css('width', (100-globalPercentage)+'%');
elTotal.text(globalurlcounter.toLocaleString()); elTotal.text(globalurlcounter.toLocaleString());
});
} }
/** /**
@ -63,7 +64,9 @@ function changeStatistics(){
function changeGlobalStatus() { function changeGlobalStatus() {
var element = $('#globalStatus').is(':checked'); var element = $('#globalStatus').is(':checked');
browser.storage.local.set({'globalStatus': element}); core(function (ref){
ref.setData('globalStatus', element);
});
} }
/** /**
@ -71,19 +74,7 @@ function changeGlobalStatus() {
*/ */
function setGlobalStatus() { function setGlobalStatus() {
var element = $('#globalStatus'); var element = $('#globalStatus');
element.prop('checked', globalStatus);
browser.storage.local.get('globalStatus', function(data) {
if(data.globalStatus) {
element.prop('checked', true);
}
else if(data.globalStatus === null || typeof(data.globalStatus) == "undefined"){
element.prop('checked', true);
browser.storage.local.set({'globalStatus': true});
}
else {
element.prop('checked', false);
}
});
} }
/** /**
@ -94,7 +85,9 @@ function setGlobalStatus() {
function changeTabcounter() { function changeTabcounter() {
var element = $('#tabcounter').is(':checked'); var element = $('#tabcounter').is(':checked');
browser.storage.local.set({'badgedStatus': element}); core(function (ref){
ref.setData('badgedStatus', element);
});
} }
/** /**
@ -102,20 +95,7 @@ function changeTabcounter() {
*/ */
function setTabcounter() { function setTabcounter() {
var element = $('#tabcounter'); var element = $('#tabcounter');
element.prop('checked', badgedStatus);
browser.storage.local.get('badgedStatus', function(data) {
if(data.badgedStatus)
{
element.prop('checked', true);
}
else if(data.badgedStatus === null || typeof(data.badgedStatus) == "undefined"){
element.prop('checked', true);
browser.storage.local.set({'badgedStatus': true});
}
else {
element.prop('checked', false);
}
});
} }
/** /**
@ -124,8 +104,9 @@ function setTabcounter() {
function changeLogging() function changeLogging()
{ {
var element = $('#logging').is(':checked'); var element = $('#logging').is(':checked');
core(function (ref){
browser.storage.local.set({'loggingStatus': element}); ref.setData('loggingStatus', element);
});
} }
/** /**
@ -135,15 +116,14 @@ function setHashStatus()
{ {
var element = $('#hashStatus'); var element = $('#hashStatus');
browser.storage.local.get('hashStatus', function(data) { if(hashStatus)
if(data.hashStatus)
{ {
element.text(data.hashStatus); element.text(hashStatus);
} }
else { else {
element.text('Oops something went wrong!'); element.text('Oops something went wrong!');
} }
});
} }
/** /**
@ -152,31 +132,22 @@ function setHashStatus()
function setLogging() function setLogging()
{ {
var element = $('#logging'); var element = $('#logging');
element.prop('checked', loggingStatus);
browser.storage.local.get('loggingStatus', function(data) {
if(data.loggingStatus)
{
element.prop('checked', true);
}
else if(data.loggingStatus === null || typeof(data.loggingStatus) == "undefined"){
element.prop('checked', false);
browser.storage.local.set({'loggingStatus': false});
}
else {
element.prop('checked', false);
}
});
} }
/** /**
* Reset the global statistic * Reset the global statistic
*
*/ */
function resetGlobalCounter(){ function resetGlobalCounter(){
browser.storage.local.set({"globalCounter": 0}); core(function (ref){
browser.storage.local.set({"globalurlcounter": 0}); globalurlcounter = 0;
globalCounter = 0;
ref.setData('globalCounter', 0);
ref.setData('globalurlcounter', 0);
});
} }
getData();
$(document).ready(function(){ $(document).ready(function(){
init(); init();
$("#globalStatus").on("change", changeGlobalStatus); $("#globalStatus").on("change", changeGlobalStatus);

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "ClearURLs", "name": "ClearURLs",
"version": "1.2.1.4", "version": "1.2.2.0",
"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",