|
| 1 | +/******************************************************************************* |
| 2 | +
|
| 3 | + uBlock Origin - a browser extension to block requests. |
| 4 | + Copyright (C) 2016 Raymond Hill |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see {http://www.gnu.org/licenses/}. |
| 18 | +
|
| 19 | + Home: https://github.com/gorhill/uBlock |
| 20 | +*/ |
| 21 | + |
| 22 | +/* global uDom */ |
| 23 | + |
| 24 | +'use strict'; |
| 25 | + |
| 26 | +/******************************************************************************/ |
| 27 | + |
| 28 | +(function() { |
| 29 | + |
| 30 | +/******************************************************************************/ |
| 31 | + |
| 32 | +var messaging = vAPI.messaging; |
| 33 | +var cachedData = ''; |
| 34 | +var rawAdvancedSettings = uDom.nodeFromId('advancedSettings'); |
| 35 | + |
| 36 | +/******************************************************************************/ |
| 37 | + |
| 38 | +var hashFromAdvancedSettings = function(raw) { |
| 39 | + return raw.trim().replace(/\s+/g, '|'); |
| 40 | +}; |
| 41 | + |
| 42 | +/******************************************************************************/ |
| 43 | + |
| 44 | +// This is to give a visual hint that the content of user blacklist has changed. |
| 45 | + |
| 46 | +var advancedSettingsChanged = (function () { |
| 47 | + var timer = null; |
| 48 | + |
| 49 | + var handler = function() { |
| 50 | + timer = null; |
| 51 | + var changed = hashFromAdvancedSettings(rawAdvancedSettings.value) !== cachedData; |
| 52 | + uDom.nodeFromId('advancedSettingsApply').disabled = !changed; |
| 53 | + }; |
| 54 | + |
| 55 | + return function() { |
| 56 | + if ( timer !== null ) { |
| 57 | + clearTimeout(timer); |
| 58 | + } |
| 59 | + timer = vAPI.setTimeout(handler, 100); |
| 60 | + }; |
| 61 | +})(); |
| 62 | + |
| 63 | +/******************************************************************************/ |
| 64 | + |
| 65 | +function renderAdvancedSettings() { |
| 66 | + var onRead = function(raw) { |
| 67 | + cachedData = hashFromAdvancedSettings(raw); |
| 68 | + var pretty = [], |
| 69 | + whitespaces = ' ', |
| 70 | + lines = raw.split('\n'), |
| 71 | + max = 0, |
| 72 | + pos, |
| 73 | + i, n = lines.length; |
| 74 | + for ( i = 0; i < n; i++ ) { |
| 75 | + pos = lines[i].indexOf(' '); |
| 76 | + if ( pos > max ) { |
| 77 | + max = pos; |
| 78 | + } |
| 79 | + } |
| 80 | + for ( i = 0; i < n; i++ ) { |
| 81 | + pos = lines[i].indexOf(' '); |
| 82 | + pretty.push(whitespaces.slice(0, max - pos) + lines[i]); |
| 83 | + } |
| 84 | + rawAdvancedSettings.value = pretty.join('\n') + '\n'; |
| 85 | + advancedSettingsChanged(); |
| 86 | + rawAdvancedSettings.focus(); |
| 87 | + }; |
| 88 | + messaging.send('dashboard', { what: 'readHiddenSettings' }, onRead); |
| 89 | +} |
| 90 | + |
| 91 | +/******************************************************************************/ |
| 92 | + |
| 93 | +var applyChanges = function() { |
| 94 | + messaging.send( |
| 95 | + 'dashboard', |
| 96 | + { |
| 97 | + what: 'writeHiddenSettings', |
| 98 | + content: rawAdvancedSettings.value |
| 99 | + }, |
| 100 | + renderAdvancedSettings |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +/******************************************************************************/ |
| 105 | + |
| 106 | +// Handle user interaction |
| 107 | +uDom('#advancedSettings').on('input', advancedSettingsChanged); |
| 108 | +uDom('#advancedSettingsApply').on('click', applyChanges); |
| 109 | + |
| 110 | +renderAdvancedSettings(); |
| 111 | + |
| 112 | +/******************************************************************************/ |
| 113 | + |
| 114 | +})(); |
0 commit comments