about.js 4.2 KB

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