1
0
Fork 0
mirror of https://github.com/gorhill/uMatrix.git synced 2024-06-02 02:14:52 +12:00
uMatrix/src/js/about.js

204 lines
6.6 KiB
JavaScript
Raw Normal View History

2014-10-18 08:01:09 +13:00
/*******************************************************************************
µMatrix - a Chromium browser extension to black/white list requests.
Copyright (C) 2014 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uMatrix
*/
2014-10-24 12:40:48 +13:00
/* global chrome, uDom */
2014-10-18 08:01:09 +13:00
/******************************************************************************/
2014-10-24 12:40:48 +13:00
uDom.onLoad(function() {
2014-10-18 08:01:09 +13:00
/******************************************************************************/
var backupUserDataToFile = function() {
var allUserData = {
timeStamp: Date.now(),
version: '',
userSettings: {},
scopes: '',
remoteBlacklists: {},
ubiquitousBlacklist: '',
ubiquitousWhitelist: ''
};
var userWhitelistReady = function(details) {
allUserData.ubiquitousWhitelist = details.content;
chrome.downloads.download({
'url': 'data:text/plain,' + encodeURIComponent(JSON.stringify(allUserData)),
'filename': 'umatrix-alluserdata-backup.txt',
'saveAs': true
});
};
var userBlacklistReady = function(details) {
allUserData.ubiquitousBlacklist = details.content;
messaging.ask({ what: 'readUserUbiquitousAllowRules' }, userWhitelistReady);
};
var ruleDataReady = function(store) {
allUserData.version = store.version;
allUserData.scopes = store.scopes;
allUserData.remoteBlacklists = store.remoteBlacklists;
messaging.ask({ what: 'readUserUbiquitousBlockRules' }, userBlacklistReady);
};
var userSettingsReady = function(store) {
allUserData.userSettings = store;
chrome.storage.local.get(['version', 'scopes', 'remoteBlacklists'], ruleDataReady);
};
messaging.ask({ what: 'readUserSettings' }, userSettingsReady);
};
/******************************************************************************/
2014-10-24 12:40:48 +13:00
function restoreUserDataFromFile() {
2014-10-18 08:01:09 +13:00
var restartCountdown = 4;
var doCountdown = function() {
restartCountdown -= 1;
if ( restartCountdown > 0 ) {
return;
}
chrome.runtime.reload();
};
var restoreBackup = function(data) {
chrome.storage.local.set(data.userSettings, doCountdown);
var store = {
'version': data.version,
'scopes': data.scopes
};
// This case may happen if data was backed up without the user having
// changed default selection of lists.
if ( data.remoteBlacklists !== undefined ) {
store.remoteBlacklists = data.remoteBlacklists;
}
chrome.storage.local.set(store, doCountdown);
messaging.ask({
what: 'writeUserUbiquitousBlockRules',
content: data.ubiquitousBlacklist
},
doCountdown
);
messaging.ask({
what: 'writeUserUbiquitousAllowRules',
content: data.ubiquitousWhitelist
},
doCountdown
);
};
var validateBackup = function(s) {
var data;
try {
data = JSON.parse(s);
}
catch (e) {
data = undefined;
}
if ( typeof data !== 'object' ||
typeof data.timeStamp !== 'number' ||
typeof data.version !== 'string' ||
typeof data.userSettings !== 'object' ||
typeof data.scopes !== 'string' ||
typeof data.ubiquitousBlacklist !== 'string' ||
typeof data.ubiquitousWhitelist !== 'string' ) {
alert('File content is not valid backed up data.');
}
return data;
};
var fileReaderOnLoadHandler = function() {
var data = validateBackup(this.result);
if ( !data ) {
return;
}
var time = new Date(data.timeStamp);
var msg = chrome.i18n
.getMessage('aboutUserDataRestoreConfirm')
.replace('{{time}}', time.toLocaleString());
var proceed = window.confirm(msg);
if ( proceed ) {
restoreBackup(data);
}
};
2014-10-24 12:40:48 +13:00
var file = this.files[0];
if ( file === undefined || file.name === '' ) {
return;
}
if ( file.type.indexOf('text') !== 0 ) {
return;
}
var fr = new FileReader();
fr.onload = fileReaderOnLoadHandler;
fr.readAsText(file);
}
/******************************************************************************/
2014-10-18 08:01:09 +13:00
2014-10-24 12:40:48 +13:00
var startRestoreFilePicker = function() {
var input = document.getElementById('restoreFilePicker');
// Reset to empty string, this will ensure an change event is properly
// triggered if the user pick a file, even if it is the same as the last
// one picked.
input.value = '';
input.click();
2014-10-18 08:01:09 +13:00
};
/******************************************************************************/
var resetUserData = function() {
messaging.tell({
what: 'gotoExtensionURL',
url: 'setup.html'
});
};
/******************************************************************************/
messaging.start('about.js');
/******************************************************************************/
(function() {
2014-10-24 12:40:48 +13:00
uDom('#aboutVersion').html(chrome.runtime.getManifest().version);
2014-10-18 08:01:09 +13:00
var renderStats = function(details) {
var template = chrome.i18n.getMessage('aboutStorageUsed');
var percent = 0;
if ( details.storageQuota ) {
percent = (details.storageUsed / details.storageQuota * 100).toFixed(1);
}
2014-10-24 12:40:48 +13:00
uDom('#aboutStorageUsed').html(template.replace('{{storageUsed}}', percent));
2014-10-18 08:01:09 +13:00
};
messaging.ask({ what: 'getSomeStats' }, renderStats);
})();
/******************************************************************************/
2014-10-24 12:40:48 +13:00
uDom('#backupUserDataButton').on('click', backupUserDataToFile);
uDom('#restoreUserDataButton').on('click', startRestoreFilePicker);
uDom('#restoreFilePicker').on('change', restoreUserDataFromFile);
uDom('#resetUserDataButton').on('click', resetUserData);
2014-10-18 08:01:09 +13:00
/******************************************************************************/
});