1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00
This commit is contained in:
gorhill 2015-03-06 23:36:09 -05:00
parent e861ff00ca
commit d8b949ecbe
7 changed files with 177 additions and 59 deletions

View File

@ -235,6 +235,18 @@
"message":"Enable experimental features (<a href='https:\/\/github.com\/gorhill\/uBlock\/wiki\/Experimental-features'>About<\/a>)", "message":"Enable experimental features (<a href='https:\/\/github.com\/gorhill\/uBlock\/wiki\/Experimental-features'>About<\/a>)",
"description":"English: Enable experimental features" "description":"English: Enable experimental features"
}, },
"settingsStorageUsed":{
"message":"Storage used: {{value}} bytes",
"description":"English: Storage used: {{}} bytes"
},
"settingsLastRestorePrompt":{
"message":"Last restore:",
"description":"English: Last restore:"
},
"settingsLastBackupPrompt":{
"message":"Last backup:",
"description":"English: Last backup:"
},
"3pListsOfBlockedHostsPrompt":{ "3pListsOfBlockedHostsPrompt":{
"message":"{{netFilterCount}} network filters {{cosmeticFilterCount}} cosmetic filters from:", "message":"{{netFilterCount}} network filters {{cosmeticFilterCount}} cosmetic filters from:",
"description":"English: {{netFilterCount}} network filters {{cosmeticFilterCount}} cosmetic filters from:" "description":"English: {{netFilterCount}} network filters {{cosmeticFilterCount}} cosmetic filters from:"

View File

@ -92,6 +92,13 @@ return {
selfieMagic: 'spqmeuaftfra' selfieMagic: 'spqmeuaftfra'
}, },
restoreBackupSettings: {
lastRestoreFile: '',
lastRestoreTime: 0,
lastBackupFile: '',
lastBackupTime: 0
},
// EasyList, EasyPrivacy and many others have an 4-day update period, // EasyList, EasyPrivacy and many others have an 4-day update period,
// as per list headers. // as per list headers.
updateAssetsEvery: 97 * oneHour, updateAssetsEvery: 97 * oneHour,

View File

@ -954,24 +954,58 @@ var µb = µBlock;
/******************************************************************************/ /******************************************************************************/
var getUserData = function(callback) { var getLocalData = function(callback) {
var onUserFiltersReady = function(details) { var onStorageInfoReady = function(bytesInUse) {
var o = µb.restoreBackupSettings;
callback({ callback({
'timeStamp': Date.now(), storageUsed: bytesInUse,
'version': vAPI.app.version, lastRestoreFile: o.lastRestoreFile,
'userSettings': µb.userSettings, lastRestoreTime: o.lastRestoreTime,
'filterLists': µb.remoteBlacklists, lastBackupFile: o.lastBackupFile,
'netWhitelist': µb.stringFromWhitelist(µb.netWhitelist), lastBackupTime: o.lastBackupTime
'userFilters': details.content
}); });
}; };
µb.getBytesInUse(onStorageInfoReady);
};
/******************************************************************************/
var backupUserData = function(callback) {
var onUserFiltersReady = function(details) {
var userData = {
timeStamp: Date.now(),
version: vAPI.app.version,
userSettings: µb.userSettings,
filterLists: µb.remoteBlacklists,
netWhitelist: µb.stringFromWhitelist(µb.netWhitelist),
userFilters: details.content
};
var now = new Date();
var filename = vAPI.i18n('aboutBackupFilename')
.replace('{{datetime}}', now.toLocaleString())
.replace(/ +/g, '_');
vAPI.download({
'url': 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(userData, null, ' ')),
'filename': filename
});
µb.restoreBackupSettings.lastBackupFile = filename;
µb.restoreBackupSettings.lastBackupTime = Date.now();
µb.XAL.keyvalSetMany(µb.restoreBackupSettings);
getLocalData(callback);
};
µb.assets.get('assets/user/filters.txt', onUserFiltersReady); µb.assets.get('assets/user/filters.txt', onUserFiltersReady);
}; };
/******************************************************************************/ /******************************************************************************/
var restoreUserData = function(userData) { var restoreUserData = function(request) {
var countdown = 5; var userData = request.userData;
var countdown = 6;
var onCountdown = function() { var onCountdown = function() {
countdown -= 1; countdown -= 1;
if ( countdown === 0 ) { if ( countdown === 0 ) {
@ -987,6 +1021,13 @@ var restoreUserData = function(userData) {
µb.XAL.keyvalSetOne('remoteBlacklists', userData.filterLists, onCountdown); µb.XAL.keyvalSetOne('remoteBlacklists', userData.filterLists, onCountdown);
µb.XAL.keyvalSetOne('netWhitelist', userData.netWhitelist, onCountdown); µb.XAL.keyvalSetOne('netWhitelist', userData.netWhitelist, onCountdown);
µb.assets.put('assets/user/filters.txt', userData.userFilters, onCountdown); µb.assets.put('assets/user/filters.txt', userData.userFilters, onCountdown);
µb.XAL.keyvalSetMany({
lastRestoreFile: request.file || '',
lastRestoreTime: Date.now(),
lastBackupFile: '',
lastBackupTime: 0
}, onCountdown);
}; };
// If we are going to restore all, might as well wipe out clean local // If we are going to restore all, might as well wipe out clean local
@ -999,7 +1040,7 @@ var restoreUserData = function(userData) {
var resetUserData = function() { var resetUserData = function() {
µb.XAL.keyvalRemoveAll(); µb.XAL.keyvalRemoveAll();
// Keep global counts, people can become quite attached to numbers // Keep global counts, people can become quite attached to numbers
µBlock.saveLocalSettings(); µb.saveLocalSettings();
vAPI.app.restart(); vAPI.app.restart();
}; };
@ -1008,8 +1049,11 @@ var resetUserData = function() {
var onMessage = function(request, sender, callback) { var onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
case 'getUserData': case 'backupUserData':
return getUserData(callback); return backupUserData(callback);
case 'getLocalData':
return getLocalData(callback);
default: default:
break; break;
@ -1020,7 +1064,7 @@ var onMessage = function(request, sender, callback) {
switch ( request.what ) { switch ( request.what ) {
case 'restoreUserData': case 'restoreUserData':
restoreUserData(request.userData); restoreUserData(request);
break; break;
case 'resetUserData': case 'resetUserData':

View File

@ -33,27 +33,16 @@ var messager = vAPI.messaging.channel('settings.js');
/******************************************************************************/ /******************************************************************************/
var exportToFile = function() {
var onUserDataReady = function(userData) {
if (!userData) {
return;
}
var now = new Date();
var filename = vAPI.i18n('aboutBackupFilename')
.replace('{{datetime}}', now.toLocaleString())
.replace(/ +/g, '_');
vAPI.download({
'url': 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(userData, null, ' ')),
'filename': filename
});
};
messager.send({ what: 'getUserData' }, onUserDataReady);
};
/******************************************************************************/
var handleImportFilePicker = function() { var handleImportFilePicker = function() {
var file = this.files[0];
if ( file === undefined || file.name === '' ) {
return;
}
if ( file.type.indexOf('text') !== 0 ) {
return;
}
var filename = file.name;
var fileReaderOnLoadHandler = function() { var fileReaderOnLoadHandler = function() {
var userData; var userData;
try { try {
@ -80,20 +69,17 @@ var handleImportFilePicker = function() {
} }
var time = new Date(userData.timeStamp); var time = new Date(userData.timeStamp);
var msg = vAPI.i18n('aboutRestoreDataConfirm') var msg = vAPI.i18n('aboutRestoreDataConfirm')
.replace('{{time}}', time.toLocaleString()); .replace('{{time}}', time.toLocaleString());
var proceed = window.confirm(msg); var proceed = window.confirm(msg);
if ( proceed ) { if ( proceed ) {
messager.send({ what: 'restoreUserData', userData: userData }); messager.send({
what: 'restoreUserData',
userData: userData,
file: filename
});
} }
}; };
var file = this.files[0];
if ( file === undefined || file.name === '' ) {
return;
}
if ( file.type.indexOf('text') !== 0 ) {
return;
}
var fr = new FileReader(); var fr = new FileReader();
fr.onload = fileReaderOnLoadHandler; fr.onload = fileReaderOnLoadHandler;
fr.readAsText(file); fr.readAsText(file);
@ -112,6 +98,47 @@ var startImportFilePicker = function() {
/******************************************************************************/ /******************************************************************************/
var exportToFile = function() {
messager.send({ what: 'backupUserData' }, onLocalDataReceived);
};
/******************************************************************************/
var onLocalDataReceived = function(details) {
uDom('#localData > ul > li:nth-of-type(1)').text(
vAPI.i18n('settingsStorageUsed').replace('{{value}}', details.storageUsed.toLocaleString())
);
var elem, dt;
var timeOptions = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
timeZoneName: 'short'
};
var lastBackupFile = details.lastBackupFile || '';
if ( lastBackupFile !== '' ) {
dt = new Date(details.lastBackupTime);
uDom('#localData > ul > li:nth-of-type(2) > ul > li:nth-of-type(1)').text(dt.toLocaleString('fullwide', timeOptions));
uDom('#localData > ul > li:nth-of-type(2) > ul > li:nth-of-type(2)').text(lastBackupFile);
uDom('#localData > ul > li:nth-of-type(2)').css('display', '');
}
var lastRestoreFile = details.lastRestoreFile || '';
elem = uDom('#localData > p:nth-of-type(3)');
if ( lastRestoreFile !== '' ) {
dt = new Date(details.lastRestoreTime);
uDom('#localData > ul > li:nth-of-type(3) > ul > li:nth-of-type(1)').text(dt.toLocaleString('fullwide', timeOptions));
uDom('#localData > ul > li:nth-of-type(3) > ul > li:nth-of-type(2)').text(lastRestoreFile);
uDom('#localData > ul > li:nth-of-type(3)').css('display', '');
}
};
/******************************************************************************/
var resetUserData = function() { var resetUserData = function() {
var msg = vAPI.i18n('aboutResetDataConfirm'); var msg = vAPI.i18n('aboutResetDataConfirm');
var proceed = window.confirm(msg); var proceed = window.confirm(msg);
@ -175,6 +202,7 @@ var onUserSettingsReceived = function(details) {
uDom.onLoad(function() { uDom.onLoad(function() {
messager.send({ what: 'userSettings' }, onUserSettingsReceived); messager.send({ what: 'userSettings' }, onUserSettingsReceived);
messager.send({ what: 'getLocalData' }, onLocalDataReceived);
}); });
/******************************************************************************/ /******************************************************************************/

View File

@ -143,12 +143,6 @@ var onUserSettingsReady = function(fetched) {
/******************************************************************************/ /******************************************************************************/
var onLocalSettingsReady = function(fetched) {
fromFetch(µb.localSettings, fetched);
};
/******************************************************************************/
// Housekeeping, as per system setting changes // Housekeeping, as per system setting changes
var onSystemSettingsReady = function(fetched) { var onSystemSettingsReady = function(fetched) {
@ -170,11 +164,11 @@ var onSystemSettingsReady = function(fetched) {
/******************************************************************************/ /******************************************************************************/
var onFirstFetchReady = function(fetched) { var onFirstFetchReady = function(fetched) {
// Order is important -- do not change: // Order is important -- do not change:
onSystemSettingsReady(fetched); onSystemSettingsReady(fetched);
onLocalSettingsReady(fetched); fromFetch(µb.localSettings, fetched);
onUserSettingsReady(fetched); onUserSettingsReady(fetched);
fromFetch(µb.restoreBackupSettings, fetched);
onNetWhitelistReady(fetched.netWhitelist); onNetWhitelistReady(fetched.netWhitelist);
onVersionReady(fetched.version); onVersionReady(fetched.version);
@ -191,6 +185,10 @@ var onFirstFetchReady = function(fetched) {
var fetchableProps = { var fetchableProps = {
'compiledMagic': '', 'compiledMagic': '',
'lastRestoreFile': '',
'lastRestoreTime': 0,
'lastBackupFile': '',
'lastBackupTime': 0,
'netWhitelist': '', 'netWhitelist': '',
'selfie': null, 'selfie': null,
'selfieMagic': '', 'selfieMagic': '',
@ -222,6 +220,7 @@ var fromFetch = function(to, fetched) {
toFetch(µb.localSettings, fetchableProps); toFetch(µb.localSettings, fetchableProps);
toFetch(µb.userSettings, fetchableProps); toFetch(µb.userSettings, fetchableProps);
toFetch(µb.restoreBackupSettings, fetchableProps);
vAPI.storage.get(fetchableProps, onFirstFetchReady); vAPI.storage.get(fetchableProps, onFirstFetchReady);

View File

@ -25,9 +25,13 @@
/******************************************************************************/ /******************************************************************************/
µBlock.getBytesInUse = function() { µBlock.getBytesInUse = function(callback) {
if ( typeof callback !== 'function' ) {
callback = this.noopFunc;
}
var getBytesInUseHandler = function(bytesInUse) { var getBytesInUseHandler = function(bytesInUse) {
µBlock.storageUsed = bytesInUse; µBlock.storageUsed = bytesInUse;
callback(bytesInUse);
}; };
vAPI.storage.getBytesInUse(null, getBytesInUseHandler); vAPI.storage.getBytesInUse(null, getBytesInUseHandler);
}; };

View File

@ -7,9 +7,21 @@
<link rel="stylesheet" type="text/css" href="css/dashboard-common.css"> <link rel="stylesheet" type="text/css" href="css/dashboard-common.css">
<style> <style>
ul { ul {
list-style-type: none;
padding-left: 1em; padding-left: 1em;
} }
li {
margin-top: 0.25em;
}
ul#userSettings {
list-style-type: none;
}
#localData > ul > li {
margin-top: 1em;
}
#localData > ul > li > ul > li:nth-of-type(2) {
font-family: monospace;
font-size: small;
}
#experimental-enabled { #experimental-enabled {
margin-top: 1em; margin-top: 1em;
} }
@ -18,24 +30,36 @@ ul {
<body> <body>
<ul> <ul id="userSettings">
<li><input id="collapse-blocked" type="checkbox"><label data-i18n="settingsCollapseBlockedPrompt" for="collapse-blocked"></label> <li><input id="collapse-blocked" type="checkbox"><label data-i18n="settingsCollapseBlockedPrompt" for="collapse-blocked"></label>
<li><input id="icon-badge" type="checkbox"><label data-i18n="settingsIconBadgePrompt" for="icon-badge"></label> <li><input id="icon-badge" type="checkbox"><label data-i18n="settingsIconBadgePrompt" for="icon-badge"></label>
<li><input id="context-menu-enabled" type="checkbox"><label data-i18n="settingsContextMenuPrompt" for="context-menu-enabled"></label> <li><input id="context-menu-enabled" type="checkbox"><label data-i18n="settingsContextMenuPrompt" for="context-menu-enabled"></label>
<li><input id="advanced-user-enabled" type="checkbox"><label data-i18n="settingsAdvancedUserPrompt" for="advanced-user-enabled"></label> <li><input id="advanced-user-enabled" type="checkbox"><label data-i18n="settingsAdvancedUserPrompt" for="advanced-user-enabled"></label>
<!-- <li><input id="experimental-enabled" type="checkbox" disabled><label data-i18n="settingsExperimentalPrompt" for="experimental-enabled"></label> --> <!-- <li><input id="experimental-enabled" type="checkbox" disabled><label data-i18n="settingsExperimentalPrompt" for="experimental-enabled"></label> -->
</ul> </ul>
<div style="margin:3em 0;border-top:1px solid #ccc;"></div> <div id="localData" style="margin: 0 1em;">
<div style="margin: 2.5em 0; border-top: 1px solid #ccc;"></div>
<ul>
<li>
<li style="display: none;"><span data-i18n="settingsLastBackupPrompt"></span><ul>
<li>
<li>
</ul>
<li style="display: none;"><span data-i18n="settingsLastRestorePrompt"></span><ul>
<li>
<li>
</ul>
</ul>
</div>
<div style="margin:1em 0 0 1em;"> <div style="margin: 2.5em 1em;">
<p><button type="button" id="export" data-i18n="aboutBackupDataButton"></button>&ensp; <p><button type="button" id="export" data-i18n="aboutBackupDataButton"></button>&ensp;
<button type="button" id="import" data-i18n="aboutRestoreDataButton"></button> <button type="button" id="import" data-i18n="aboutRestoreDataButton"></button>
<input id="restoreFilePicker" type="file" accept="text/plain" class="hiddenFileInput"> <input id="restoreFilePicker" type="file" accept="text/plain" class="hiddenFileInput">
<p> <p>
<p><button type="button" id="reset" data-i18n="aboutResetDataButton"></button> <p><button type="button" id="reset" data-i18n="aboutResetDataButton"></button>
</div> </div>
<script src="js/vapi-common.js"></script> <script src="js/vapi-common.js"></script>
<script src="js/vapi-client.js"></script> <script src="js/vapi-client.js"></script>