You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nuTensor/src/js/about.js

144 lines
4.8 KiB
JavaScript

10 years ago
/*******************************************************************************
µ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
*/
10 years ago
/* global vAPI, uDom */
10 years ago
/******************************************************************************/
uDom.onLoad(function() {
10 years ago
10 years ago
'use strict';
/******************************************************************************/
var messager = vAPI.messaging.channel('about.js');
10 years ago
/******************************************************************************/
var backupUserDataToFile = function() {
var userDataReady = function(userData) {
10 years ago
vAPI.download({
'url': 'data:text/plain,' + encodeURIComponent(JSON.stringify(userData)),
10 years ago
'filename': uDom('[data-i18n="aboutBackupFilename"]').text()
10 years ago
});
};
10 years ago
messager.send({ what: 'getAllUserData' }, userDataReady);
10 years ago
};
/******************************************************************************/
function restoreUserDataFromFile() {
10 years ago
var validateBackup = function(s) {
var userData = null;
10 years ago
try {
userData = JSON.parse(s);
10 years ago
}
catch (e) {
userData = null;
10 years ago
}
if ( userData === null ) {
return null;
10 years ago
}
if ( typeof userData !== 'object' ||
typeof userData.version !== 'string' ||
typeof userData.when !== 'number' ||
typeof userData.settings !== 'object' ||
typeof userData.rules !== 'string' ||
typeof userData.hostsFiles !== 'object' ) {
return null;
}
return userData;
10 years ago
};
var fileReaderOnLoadHandler = function() {
var userData = validateBackup(this.result);
if ( !userData ) {
window.alert(uDom('[data-i18n="aboutRestoreError"]').text());
10 years ago
return;
}
var time = new Date(userData.when);
var msg = uDom('[data-i18n="aboutRestoreConfirm"]').text()
10 years ago
.replace('{{time}}', time.toLocaleString());
var proceed = window.confirm(msg);
if ( proceed ) {
10 years ago
messager.send({ what: 'restoreAllUserData', userData: userData });
10 years ago
}
};
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);
}
/******************************************************************************/
10 years ago
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();
10 years ago
};
/******************************************************************************/
var resetUserData = function() {
var proceed = window.confirm(uDom('[data-i18n="aboutResetConfirm"]').text());
if ( proceed ) {
10 years ago
messager.send({ what: 'resetAllUserData' });
}
10 years ago
};
/******************************************************************************/
(function() {
var renderStats = function(details) {
uDom('#aboutVersion').html(details.version);
var template = uDom('[data-i18n="aboutStorageUsed"]').text();
var storageUsed = '?';
if ( typeof details.storageUsed === 'number' ) {
storageUsed = details.storageUsed.toLocaleString();
10 years ago
}
uDom('#aboutStorageUsed').html(template.replace('{{storageUsed}}', storageUsed));
10 years ago
};
10 years ago
messager.send({ what: 'getSomeStats' }, renderStats);
10 years ago
})();
/******************************************************************************/
uDom('#backupUserDataButton').on('click', backupUserDataToFile);
uDom('#restoreUserDataButton').on('click', startRestoreFilePicker);
uDom('#restoreFilePicker').on('change', restoreUserDataFromFile);
uDom('#resetUserDataButton').on('click', resetUserData);
10 years ago
/******************************************************************************/
});