settings.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. 'use strict';
  19. (function() {
  20. let cachedSettings = {};
  21. function changeUserSettings(name, value) {
  22. vAPI.messaging.send('settings.js', {
  23. what: 'userSettings',
  24. name: name,
  25. value: value
  26. });
  27. }
  28. function changeMatrixSwitch(name, state) {
  29. vAPI.messaging.send('settings.js', {
  30. what: 'setMatrixSwitch',
  31. switchName: name,
  32. state: state
  33. });
  34. }
  35. function onChangeValueHandler(elem, setting, min, max) {
  36. let oldVal = cachedSettings.userSettings[setting];
  37. let newVal = Math.round(parseFloat(elem.value));
  38. if (typeof newVal !== 'number') {
  39. newVal = oldVal;
  40. } else {
  41. newVal = Math.max(newVal, min);
  42. newVal = Math.min(newVal, max);
  43. }
  44. elem.value = newVal;
  45. if (newVal !== oldVal) {
  46. changeUserSettings(setting, newVal);
  47. }
  48. }
  49. function prepareToDie() {
  50. onChangeValueHandler(uDom.nodeFromId('deleteUnusedSessionCookiesAfter'),
  51. 'deleteUnusedSessionCookiesAfter',
  52. 15, 1440);
  53. onChangeValueHandler(uDom.nodeFromId('clearBrowserCacheAfter'),
  54. 'clearBrowserCacheAfter',
  55. 15, 1440);
  56. }
  57. function onInputChanged(ev) {
  58. let target = ev.target;
  59. switch (target.id) {
  60. case 'displayTextSize':
  61. changeUserSettings('displayTextSize', target.value + 'px');
  62. break;
  63. case 'clearBrowserCache':
  64. case 'cloudStorageEnabled':
  65. case 'collapseBlacklisted':
  66. case 'colorBlindFriendly':
  67. case 'deleteCookies':
  68. case 'deleteLocalStorage':
  69. case 'deleteUnusedSessionCookies':
  70. case 'iconBadgeEnabled':
  71. case 'processHyperlinkAuditing':
  72. case 'disableUpdateIcon':
  73. case 'resolveCname':
  74. changeUserSettings(target.id, target.checked);
  75. break;
  76. case 'collapseBlocked':
  77. changeUserSettings(target.id, target.checked);
  78. synchronizeWidgets();
  79. break;
  80. case 'noMixedContent':
  81. case 'noscriptTagsSpoofed':
  82. case 'processReferer':
  83. changeMatrixSwitch(target.getAttribute('data-matrix-switch'),
  84. target.checked);
  85. break;
  86. case 'deleteUnusedSessionCookiesAfter':
  87. onChangeValueHandler(target, 'deleteUnusedSessionCookiesAfter',
  88. 15, 1440);
  89. break;
  90. case 'clearBrowserCacheAfter':
  91. onChangeValueHandler(target, 'clearBrowserCacheAfter', 15, 1440);
  92. break;
  93. case 'popupScopeLevel':
  94. changeUserSettings('popupScopeLevel', target.value);
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. function synchronizeWidgets() {
  101. let e1, e2;
  102. e1 = uDom.nodeFromId('collapseBlocked');
  103. e2 = uDom.nodeFromId('collapseBlacklisted');
  104. if (e1.checked) {
  105. e2.setAttribute('disabled', '');
  106. } else {
  107. e2.removeAttribute('disabled');
  108. }
  109. }
  110. let onSettingsReceived = function (settings) {
  111. // Cache copy
  112. cachedSettings = settings;
  113. let userSettings = settings.userSettings;
  114. let matrixSwitches = settings.matrixSwitches;
  115. uDom('[data-setting-bool]').forEach(function (elem) {
  116. elem.prop('checked', userSettings[elem.prop('id')] === true);
  117. });
  118. uDom('[data-matrix-switch]').forEach(function (elem) {
  119. let switchName = elem.attr('data-matrix-switch');
  120. if (typeof switchName === 'string' && switchName !== '') {
  121. elem.prop('checked', matrixSwitches[switchName] === true);
  122. }
  123. });
  124. uDom.nodeFromId('displayTextSize').value =
  125. parseInt(userSettings.displayTextSize, 10) || 14;
  126. uDom.nodeFromId('popupScopeLevel').value = userSettings.popupScopeLevel;
  127. uDom.nodeFromId('deleteUnusedSessionCookiesAfter').value =
  128. userSettings.deleteUnusedSessionCookiesAfter;
  129. uDom.nodeFromId('clearBrowserCacheAfter').value =
  130. userSettings.clearBrowserCacheAfter;
  131. synchronizeWidgets();
  132. document.addEventListener('change', onInputChanged);
  133. // https://github.com/gorhill/httpswitchboard/issues/197
  134. uDom(window).on('beforeunload', prepareToDie);
  135. }
  136. vAPI.messaging.send('settings.js', {
  137. what: 'getUserSettings'
  138. }, onSettingsReceived);
  139. })();