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

145 lines
4.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. uMatrix - a Chromium browser extension to black/white list requests.
  3. Copyright (C) 2014-2017 Raymond Hill
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see {http://www.gnu.org/licenses/}.
  14. Home: https://github.com/gorhill/uMatrix
  15. */
  16. /* global uDom */
  17. 'use strict';
  18. /******************************************************************************/
  19. uDom.onLoad(function() {
  20. /******************************************************************************/
  21. var backupUserDataToFile = function() {
  22. var userDataReady = function(userData) {
  23. vAPI.download({
  24. 'url': 'data:text/plain,' + encodeURIComponent(JSON.stringify(userData, null, 2)),
  25. 'filename': uDom('[data-i18n="aboutBackupFilename"]').text()
  26. });
  27. };
  28. vAPI.messaging.send('about.js', { what: 'getAllUserData' }, userDataReady);
  29. };
  30. /******************************************************************************/
  31. function restoreUserDataFromFile() {
  32. var validateBackup = function(s) {
  33. var userData = null;
  34. try {
  35. userData = JSON.parse(s);
  36. }
  37. catch (e) {
  38. userData = null;
  39. }
  40. if ( userData === null ) {
  41. return null;
  42. }
  43. if (
  44. typeof userData !== 'object' ||
  45. typeof userData.version !== 'string' ||
  46. typeof userData.when !== 'number' ||
  47. typeof userData.settings !== 'object' ||
  48. typeof userData.rules !== 'string' ||
  49. typeof userData.hostsFiles !== 'object'
  50. ) {
  51. return null;
  52. }
  53. return userData;
  54. };
  55. var fileReaderOnLoadHandler = function() {
  56. var userData = validateBackup(this.result);
  57. if ( !userData ) {
  58. window.alert(uDom('[data-i18n="aboutRestoreError"]').text());
  59. return;
  60. }
  61. var time = new Date(userData.when);
  62. var msg = uDom('[data-i18n="aboutRestoreConfirm"]').text()
  63. .replace('{{time}}', time.toLocaleString());
  64. var proceed = window.confirm(msg);
  65. if ( proceed ) {
  66. vAPI.messaging.send(
  67. 'about.js',
  68. { what: 'restoreAllUserData', userData: userData }
  69. );
  70. }
  71. };
  72. var file = this.files[0];
  73. if ( file === undefined || file.name === '' ) {
  74. return;
  75. }
  76. if ( file.type.indexOf('text') !== 0 ) {
  77. return;
  78. }
  79. var fr = new FileReader();
  80. fr.onload = fileReaderOnLoadHandler;
  81. fr.readAsText(file);
  82. }
  83. /******************************************************************************/
  84. var startRestoreFilePicker = function() {
  85. var input = document.getElementById('restoreFilePicker');
  86. // Reset to empty string, this will ensure an change event is properly
  87. // triggered if the user pick a file, even if it is the same as the last
  88. // one picked.
  89. input.value = '';
  90. input.click();
  91. };
  92. /******************************************************************************/
  93. var resetUserData = function() {
  94. var proceed = window.confirm(uDom('[data-i18n="aboutResetConfirm"]').text());
  95. if ( proceed ) {
  96. vAPI.messaging.send('about.js', { what: 'resetAllUserData' });
  97. }
  98. };
  99. /******************************************************************************/
  100. (function() {
  101. var renderStats = function(details) {
  102. document.getElementById('aboutVersion').textContent = details.version;
  103. var template = uDom('[data-i18n="aboutStorageUsed"]').text();
  104. var storageUsed = '?';
  105. if ( typeof details.storageUsed === 'number' ) {
  106. storageUsed = details.storageUsed.toLocaleString();
  107. }
  108. document.getElementById('aboutStorageUsed').textContent =
  109. template.replace('{{storageUsed}}', storageUsed);
  110. };
  111. vAPI.messaging.send('about.js', { what: 'getSomeStats' }, renderStats);
  112. })();
  113. /******************************************************************************/
  114. uDom('#backupUserDataButton').on('click', backupUserDataToFile);
  115. uDom('#restoreUserDataButton').on('click', startRestoreFilePicker);
  116. uDom('#restoreFilePicker').on('change', restoreUserDataFromFile);
  117. uDom('#resetUserDataButton').on('click', resetUserData);
  118. /******************************************************************************/
  119. });