raw-settings.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2018-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/uBlock
  17. */
  18. /* global uDom */
  19. 'use strict';
  20. /******************************************************************************/
  21. (function() {
  22. /******************************************************************************/
  23. var messaging = vAPI.messaging;
  24. var cachedData = '';
  25. var rawSettingsInput = uDom.nodeFromId('rawSettings');
  26. /******************************************************************************/
  27. var hashFromRawSettings = function(raw) {
  28. return raw.trim().replace(/\s+/g, '|');
  29. };
  30. /******************************************************************************/
  31. // This is to give a visual hint that the content of user blacklist has changed.
  32. var rawSettingsChanged = (function () {
  33. var timer = null;
  34. var handler = function() {
  35. timer = null;
  36. var changed =
  37. hashFromRawSettings(rawSettingsInput.value) !== cachedData;
  38. uDom.nodeFromId('rawSettingsApply').disabled = !changed;
  39. };
  40. return function() {
  41. if ( timer !== null ) {
  42. clearTimeout(timer);
  43. }
  44. timer = vAPI.setTimeout(handler, 100);
  45. };
  46. })();
  47. /******************************************************************************/
  48. function renderRawSettings() {
  49. var onRead = function(raw) {
  50. cachedData = hashFromRawSettings(raw);
  51. var pretty = [],
  52. whitespaces = ' ',
  53. lines = raw.split('\n'),
  54. max = 0,
  55. pos,
  56. i, n = lines.length;
  57. for ( i = 0; i < n; i++ ) {
  58. pos = lines[i].indexOf(' ');
  59. if ( pos > max ) {
  60. max = pos;
  61. }
  62. }
  63. for ( i = 0; i < n; i++ ) {
  64. pos = lines[i].indexOf(' ');
  65. pretty.push(whitespaces.slice(0, max - pos) + lines[i]);
  66. }
  67. rawSettingsInput.value = pretty.join('\n') + '\n';
  68. rawSettingsChanged();
  69. rawSettingsInput.focus();
  70. };
  71. messaging.send('dashboard', { what: 'readRawSettings' }, onRead);
  72. }
  73. /******************************************************************************/
  74. var applyChanges = function() {
  75. messaging.send(
  76. 'dashboard',
  77. {
  78. what: 'writeRawSettings',
  79. content: rawSettingsInput.value
  80. },
  81. renderRawSettings
  82. );
  83. };
  84. /******************************************************************************/
  85. // Handle user interaction
  86. uDom('#rawSettings').on('input', rawSettingsChanged);
  87. uDom('#rawSettingsApply').on('click', applyChanges);
  88. renderRawSettings();
  89. /******************************************************************************/
  90. })();