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.

151 lines
5.4 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
  1. /*******************************************************************************
  2. µMatrix - a Chromium browser extension to black/white list requests.
  3. Copyright (C) 2014 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 vAPI, uDom */
  17. /******************************************************************************/
  18. (function() {
  19. 'use strict';
  20. /******************************************************************************/
  21. var messager = vAPI.messaging.channel('privacy.js');
  22. var cachedPrivacySettings = {};
  23. /******************************************************************************/
  24. function changeUserSettings(name, value) {
  25. messager.send({
  26. what: 'userSettings',
  27. name: name,
  28. value: value
  29. });
  30. }
  31. /******************************************************************************/
  32. function changeMatrixSwitch(name, state) {
  33. messager.send({
  34. what: 'setMatrixSwitch',
  35. switchName: name,
  36. state: state
  37. });
  38. }
  39. /******************************************************************************/
  40. function onChangeValueHandler(uelem, setting, min, max) {
  41. var oldVal = cachedPrivacySettings.userSettings[setting];
  42. var newVal = Math.round(parseFloat(uelem.val()));
  43. if ( typeof newVal !== 'number' ) {
  44. newVal = oldVal;
  45. } else {
  46. newVal = Math.max(newVal, min);
  47. newVal = Math.min(newVal, max);
  48. }
  49. uelem.val(newVal);
  50. if ( newVal !== oldVal ) {
  51. changeUserSettings(setting, newVal);
  52. }
  53. }
  54. /******************************************************************************/
  55. function prepareToDie() {
  56. onChangeValueHandler(uDom('#delete-unused-session-cookies-after'), 'deleteUnusedSessionCookiesAfter', 15, 1440);
  57. onChangeValueHandler(uDom('#clear-browser-cache-after'), 'clearBrowserCacheAfter', 15, 1440);
  58. onChangeValueHandler(uDom('#spoof-user-agent-every'), 'spoofUserAgentEvery', 2, 999);
  59. }
  60. /******************************************************************************/
  61. var installEventHandlers = function() {
  62. uDom('[data-setting-bool]').on('change', function(){
  63. var settingName = this.getAttribute('data-setting-bool');
  64. if ( typeof settingName === 'string' && settingName !== '' ) {
  65. changeUserSettings(settingName, this.checked);
  66. }
  67. });
  68. uDom('[data-matrix-switch]').on('change', function(){
  69. var switchName = this.getAttribute('data-matrix-switch');
  70. if ( typeof switchName === 'string' && switchName !== '' ) {
  71. changeMatrixSwitch(switchName, this.checked);
  72. }
  73. });
  74. uDom('#delete-unused-session-cookies-after').on('change', function(){
  75. onChangeValueHandler(uDom(this), 'deleteUnusedSessionCookiesAfter', 15, 1440);
  76. });
  77. uDom('#clear-browser-cache-after').on('change', function(){
  78. onChangeValueHandler(uDom(this), 'clearBrowserCacheAfter', 15, 1440);
  79. });
  80. uDom('#spoof-user-agent-every').on('change', function(){
  81. onChangeValueHandler(uDom(this), 'spoofUserAgentEvery', 2, 999);
  82. });
  83. uDom('#spoof-user-agent-with').on('change', function(){
  84. changeUserSettings('spoofUserAgentWith', uDom(this).val());
  85. });
  86. // https://github.com/gorhill/httpswitchboard/issues/197
  87. uDom(window).on('beforeunload', prepareToDie);
  88. };
  89. /******************************************************************************/
  90. uDom.onLoad(function() {
  91. var onSettingsReceived = function(privacySettings) {
  92. // Cache copy
  93. cachedPrivacySettings = privacySettings;
  94. var userSettings = privacySettings.userSettings;
  95. var matrixSwitches = privacySettings.matrixSwitches;
  96. uDom('[data-setting-bool]').forEach(function(elem){
  97. var settingName = elem.attr('data-setting-bool');
  98. if ( typeof settingName === 'string' && settingName !== '' ) {
  99. elem.prop('checked', userSettings[settingName] === true);
  100. }
  101. });
  102. uDom('[data-matrix-switch]').forEach(function(elem){
  103. var switchName = elem.attr('data-matrix-switch');
  104. if ( typeof switchName === 'string' && switchName !== '' ) {
  105. elem.prop('checked', matrixSwitches[switchName] === true);
  106. }
  107. });
  108. uDom('#delete-unused-session-cookies-after').val(userSettings.deleteUnusedSessionCookiesAfter);
  109. uDom('#clear-browser-cache-after').val(userSettings.clearBrowserCacheAfter);
  110. uDom('#spoof-user-agent-every').val(userSettings.spoofUserAgentEvery);
  111. uDom('#spoof-user-agent-with').val(userSettings.spoofUserAgentWith);
  112. installEventHandlers();
  113. };
  114. messager.send({ what: 'getPrivacySettings' }, onSettingsReceived);
  115. });
  116. /******************************************************************************/
  117. })();