2014-10-18 08:01:09 +13:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2017-12-07 03:47:20 +13:00
|
|
|
uMatrix - a Chromium browser extension to black/white list requests.
|
2019-12-21 06:24:18 +13:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-10-18 08:01:09 +13:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2017-12-07 03:47:20 +13:00
|
|
|
/* global uDom */
|
2014-10-18 08:01:09 +13:00
|
|
|
|
2017-10-06 05:49:26 +13:00
|
|
|
'use strict';
|
|
|
|
|
2014-10-18 08:01:09 +13:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-12-21 06:24:18 +13:00
|
|
|
{
|
|
|
|
// >>>>> start of local scope
|
2014-10-18 08:01:09 +13:00
|
|
|
|
2015-04-12 09:15:57 +12:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-12-21 06:24:18 +13:00
|
|
|
const backupUserDataToFile = function() {
|
|
|
|
vAPI.messaging.send('dashboard', {
|
|
|
|
what: 'getAllUserData',
|
|
|
|
}).then(userData => {
|
2015-04-12 09:15:57 +12:00
|
|
|
vAPI.download({
|
2019-12-21 06:24:18 +13:00
|
|
|
url: 'data:text/plain,' + encodeURIComponent(
|
|
|
|
JSON.stringify(userData, null, 2)
|
|
|
|
),
|
|
|
|
filename:
|
|
|
|
uDom.nodeFromSelector('[data-i18n="aboutBackupFilename"]')
|
|
|
|
.textContent
|
2014-10-18 08:01:09 +13:00
|
|
|
});
|
2019-12-21 06:24:18 +13:00
|
|
|
});
|
2014-10-18 08:01:09 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-12-21 06:24:18 +13:00
|
|
|
const restoreUserDataFromFile = function() {
|
|
|
|
const validateBackup = function(s) {
|
|
|
|
let userData;
|
2014-10-18 08:01:09 +13:00
|
|
|
try {
|
2014-10-25 04:13:46 +13:00
|
|
|
userData = JSON.parse(s);
|
2014-10-18 08:01:09 +13:00
|
|
|
}
|
2019-12-21 06:24:18 +13:00
|
|
|
catch (ex) {
|
2014-10-18 08:01:09 +13:00
|
|
|
}
|
2019-12-21 06:24:18 +13:00
|
|
|
if ( userData === undefined ) { return; }
|
2017-12-07 03:47:20 +13:00
|
|
|
if (
|
|
|
|
typeof userData !== 'object' ||
|
2018-02-04 02:46:34 +13:00
|
|
|
typeof userData.app !== 'string' ||
|
2017-12-07 03:47:20 +13:00
|
|
|
typeof userData.version !== 'string' ||
|
|
|
|
typeof userData.when !== 'number' ||
|
|
|
|
typeof userData.settings !== 'object' ||
|
2019-12-21 06:24:18 +13:00
|
|
|
typeof userData.rules !== 'string' &&
|
|
|
|
Array.isArray(userData.rules) === false
|
2017-12-07 03:47:20 +13:00
|
|
|
) {
|
2019-12-21 06:24:18 +13:00
|
|
|
return;
|
2014-10-25 04:13:46 +13:00
|
|
|
}
|
|
|
|
return userData;
|
2014-10-18 08:01:09 +13:00
|
|
|
};
|
|
|
|
|
2019-12-21 06:24:18 +13:00
|
|
|
const fileReaderOnLoadHandler = function() {
|
|
|
|
const userData = validateBackup(this.result);
|
|
|
|
if ( userData instanceof Object === false ) {
|
2014-10-25 04:13:46 +13:00
|
|
|
window.alert(uDom('[data-i18n="aboutRestoreError"]').text());
|
2014-10-18 08:01:09 +13:00
|
|
|
return;
|
|
|
|
}
|
2019-12-21 06:24:18 +13:00
|
|
|
const time = new Date(userData.when);
|
|
|
|
const msg = uDom.nodeFromSelector('[data-i18n="aboutRestoreConfirm"]')
|
|
|
|
.textContent
|
|
|
|
.replace('{{time}}', time.toLocaleString());
|
|
|
|
const proceed = window.confirm(msg);
|
2014-10-18 08:01:09 +13:00
|
|
|
if ( proceed ) {
|
2019-12-21 06:24:18 +13:00
|
|
|
vAPI.messaging.send('dashboard', {
|
|
|
|
what: 'restoreAllUserData',
|
|
|
|
userData
|
|
|
|
});
|
2014-10-18 08:01:09 +13:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-21 06:24:18 +13:00
|
|
|
const file = this.files[0];
|
|
|
|
if ( file === undefined || file.name === '' ) { return; }
|
|
|
|
if ( file.type.indexOf('text') !== 0 ) { return; }
|
|
|
|
const fr = new FileReader();
|
2014-10-24 12:40:48 +13:00
|
|
|
fr.onload = fileReaderOnLoadHandler;
|
|
|
|
fr.readAsText(file);
|
2019-12-21 06:24:18 +13:00
|
|
|
};
|
2014-10-24 12:40:48 +13:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-10-18 08:01:09 +13:00
|
|
|
|
2019-12-21 06:24:18 +13:00
|
|
|
const startRestoreFilePicker = function() {
|
|
|
|
const input = document.getElementById('restoreFilePicker');
|
2014-10-24 12:40:48 +13:00
|
|
|
// 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
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-12-21 06:24:18 +13:00
|
|
|
const resetUserData = function() {
|
|
|
|
const msg = uDom.nodeFromSelector('[data-i18n="aboutResetConfirm"]')
|
|
|
|
.textContent;
|
|
|
|
const proceed = window.confirm(msg);
|
|
|
|
if ( proceed !== true ) { return; }
|
|
|
|
vAPI.messaging.send('dashboard', {
|
|
|
|
what: 'resetAllUserData',
|
|
|
|
});
|
2014-10-18 08:01:09 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-12-21 06:24:18 +13:00
|
|
|
vAPI.messaging.send('dashboard', {
|
|
|
|
what: 'getSomeStats',
|
|
|
|
}).then(details => {
|
|
|
|
document.getElementById('aboutVersion').textContent = details.version;
|
|
|
|
const template = uDom('[data-i18n="aboutStorageUsed"]').text();
|
|
|
|
let storageUsed = '?';
|
|
|
|
if ( typeof details.storageUsed === 'number' ) {
|
|
|
|
storageUsed = details.storageUsed.toLocaleString();
|
|
|
|
}
|
|
|
|
document.getElementById('aboutStorageUsed').textContent =
|
|
|
|
template.replace('{{storageUsed}}', storageUsed);
|
|
|
|
});
|
2014-10-18 08:01:09 +13:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
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
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-12-21 06:24:18 +13:00
|
|
|
// <<<<< end of local scope
|
|
|
|
}
|