settings.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2014-2019 Raymond Hill
  4. Copyright (C) 2019 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://libregit.org/heckyel/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. changeUserSettings(target.id, target.checked);
  74. break;
  75. case 'collapseBlocked':
  76. changeUserSettings(target.id, target.checked);
  77. synchronizeWidgets();
  78. break;
  79. case 'noMixedContent':
  80. case 'noscriptTagsSpoofed':
  81. case 'processReferer':
  82. changeMatrixSwitch(target.getAttribute('data-matrix-switch'),
  83. target.checked);
  84. break;
  85. case 'deleteUnusedSessionCookiesAfter':
  86. onChangeValueHandler(target, 'deleteUnusedSessionCookiesAfter',
  87. 15, 1440);
  88. break;
  89. case 'clearBrowserCacheAfter':
  90. onChangeValueHandler(target, 'clearBrowserCacheAfter', 15, 1440);
  91. break;
  92. case 'popupScopeLevel':
  93. changeUserSettings('popupScopeLevel', target.value);
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. function synchronizeWidgets() {
  100. let e1, e2;
  101. e1 = uDom.nodeFromId('collapseBlocked');
  102. e2 = uDom.nodeFromId('collapseBlacklisted');
  103. if (e1.checked) {
  104. e2.setAttribute('disabled', '');
  105. } else {
  106. e2.removeAttribute('disabled');
  107. }
  108. }
  109. let onSettingsReceived = function (settings) {
  110. // Cache copy
  111. cachedSettings = settings;
  112. let userSettings = settings.userSettings;
  113. let matrixSwitches = settings.matrixSwitches;
  114. uDom('[data-setting-bool]').forEach(function (elem) {
  115. elem.prop('checked', userSettings[elem.prop('id')] === true);
  116. });
  117. uDom('[data-matrix-switch]').forEach(function (elem) {
  118. let switchName = elem.attr('data-matrix-switch');
  119. if (typeof switchName === 'string' && switchName !== '') {
  120. elem.prop('checked', matrixSwitches[switchName] === true);
  121. }
  122. });
  123. uDom.nodeFromId('displayTextSize').value =
  124. parseInt(userSettings.displayTextSize, 10) || 14;
  125. uDom.nodeFromId('popupScopeLevel').value = userSettings.popupScopeLevel;
  126. uDom.nodeFromId('deleteUnusedSessionCookiesAfter').value =
  127. userSettings.deleteUnusedSessionCookiesAfter;
  128. uDom.nodeFromId('clearBrowserCacheAfter').value =
  129. userSettings.clearBrowserCacheAfter;
  130. synchronizeWidgets();
  131. document.addEventListener('change', onInputChanged);
  132. // https://github.com/gorhill/httpswitchboard/issues/197
  133. uDom(window).on('beforeunload', prepareToDie);
  134. }
  135. vAPI.messaging.send('settings.js', {
  136. what: 'getUserSettings'
  137. }, onSettingsReceived);
  138. })();