uBlock/src/js/whitelist.js

195 lines
6.1 KiB
JavaScript
Raw Normal View History

2014-07-17 16:52:43 +02:00
/*******************************************************************************
2016-03-06 16:51:06 +01:00
uBlock Origin - a browser extension to block requests.
Copyright (C) 2014-2016 Raymond Hill
2014-07-17 16:52:43 +02:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/
2016-03-06 16:51:06 +01:00
/* global uDom, uBlockDashboard */
2014-07-17 16:52:43 +02:00
/******************************************************************************/
(function() {
'use strict';
2014-07-17 16:52:43 +02:00
/******************************************************************************/
2016-12-26 17:35:37 +01:00
var messaging = vAPI.messaging,
cachedWhitelist = '';
2014-07-17 16:52:43 +02:00
/******************************************************************************/
2016-12-26 17:35:37 +01:00
var getTextareaNode = function() {
var me = getTextareaNode,
node = me.theNode;
if ( node === undefined ) {
node = me.theNode = uDom.nodeFromSelector('#whitelist textarea');
}
return node;
};
var setErrorNodeHorizontalOffset = function(px) {
var me = setErrorNodeHorizontalOffset,
offset = me.theOffset || 0;
if ( px === offset ) { return; }
var node = me.theNode;
if ( node === undefined ) {
node = me.theNode = uDom.nodeFromSelector('#whitelist textarea + div');
}
node.style.right = px + 'px';
me.theOffset = px;
2014-07-17 16:52:43 +02:00
};
/******************************************************************************/
2016-12-26 17:35:37 +01:00
var whitelistChanged = (function() {
var changedWhitelist, changed, timer;
var updateUI = function(good) {
uDom.nodeFromId('whitelistApply').disabled = changed || !good;
uDom.nodeFromId('whitelistRevert').disabled = changed;
uDom.nodeFromId('whitelist').classList.toggle('invalid', !good);
};
var validate = function() {
timer = undefined;
messaging.send(
'dashboard',
{ what: 'validateWhitelistString', raw: changedWhitelist },
updateUI
);
};
return function() {
changedWhitelist = getTextareaNode().value.trim();
changed = changedWhitelist === cachedWhitelist;
if ( timer !== undefined ) { clearTimeout(timer); }
timer = vAPI.setTimeout(validate, 251);
var textarea = getTextareaNode();
setErrorNodeHorizontalOffset(textarea.offsetWidth - textarea.clientWidth);
};
})();
/******************************************************************************/
2014-08-02 17:40:27 +02:00
var renderWhitelist = function() {
2014-07-17 16:52:43 +02:00
var onRead = function(whitelist) {
cachedWhitelist = whitelist.trim();
2016-12-26 17:35:37 +01:00
getTextareaNode().value = cachedWhitelist + '\n';
whitelistChanged();
2014-07-17 16:52:43 +02:00
};
2016-03-06 16:51:06 +01:00
messaging.send('dashboard', { what: 'getWhitelist' }, onRead);
2014-07-17 16:52:43 +02:00
};
/******************************************************************************/
2014-10-13 17:01:31 +02:00
var handleImportFilePicker = function() {
2014-07-17 16:52:43 +02:00
var fileReaderOnLoadHandler = function() {
2016-12-26 17:35:37 +01:00
var textarea = getTextareaNode();
textarea.value = [textarea.value.trim(), this.result.trim()].join('\n').trim();
2014-07-17 16:52:43 +02:00
whitelistChanged();
};
2014-10-13 17:01:31 +02:00
var file = this.files[0];
if ( file === undefined || file.name === '' ) {
return;
}
if ( file.type.indexOf('text') !== 0 ) {
return;
}
var fr = new FileReader();
fr.onload = fileReaderOnLoadHandler;
fr.readAsText(file);
};
/******************************************************************************/
var startImportFilePicker = function() {
var input = document.getElementById('importFilePicker');
// Reset to empty string, this will ensure an change event is properly
// triggered if the user pick a file, even if it is the same as the last
// one picked.
input.value = '';
input.click();
2014-07-17 16:52:43 +02:00
};
/******************************************************************************/
var exportWhitelistToFile = function() {
2016-12-26 17:35:37 +01:00
var val = getTextareaNode().value.trim();
if ( val === '' ) { return; }
var filename = vAPI.i18n('whitelistExportFilename')
2016-10-13 19:25:57 +02:00
.replace('{{datetime}}', uBlockDashboard.dateNowToSensibleString())
.replace(/ +/g, '_');
vAPI.download({
'url': 'data:text/plain;charset=utf-8,' + encodeURIComponent(val + '\n'),
'filename': filename
});
2014-07-17 16:52:43 +02:00
};
/******************************************************************************/
var applyChanges = function() {
2016-12-26 17:35:37 +01:00
cachedWhitelist = getTextareaNode().value.trim();
2014-07-17 16:52:43 +02:00
var request = {
what: 'setWhitelist',
2014-08-02 17:40:27 +02:00
whitelist: cachedWhitelist
2014-07-17 16:52:43 +02:00
};
2016-03-06 16:51:06 +01:00
messaging.send('dashboard', request, renderWhitelist);
2014-07-17 16:52:43 +02:00
};
var revertChanges = function() {
2016-12-26 17:35:37 +01:00
getTextareaNode().value = cachedWhitelist + '\n';
whitelistChanged();
};
2014-07-17 16:52:43 +02:00
/******************************************************************************/
2015-08-11 21:29:14 +02:00
var getCloudData = function() {
2016-12-26 17:35:37 +01:00
return getTextareaNode().value;
2015-08-11 21:29:14 +02:00
};
var setCloudData = function(data, append) {
2015-08-11 21:29:14 +02:00
if ( typeof data !== 'string' ) {
return;
}
2016-12-26 17:35:37 +01:00
var textarea = getTextareaNode();
if ( append ) {
data = uBlockDashboard.mergeNewLines(textarea.value.trim(), data);
}
textarea.value = data.trim() + '\n';
2015-08-11 21:29:14 +02:00
whitelistChanged();
};
self.cloud.onPush = getCloudData;
self.cloud.onPull = setCloudData;
/******************************************************************************/
uDom('#importWhitelistFromFile').on('click', startImportFilePicker);
uDom('#importFilePicker').on('change', handleImportFilePicker);
uDom('#exportWhitelistToFile').on('click', exportWhitelistToFile);
2016-12-26 17:35:37 +01:00
uDom('#whitelist textarea').on('input', whitelistChanged);
uDom('#whitelistApply').on('click', applyChanges);
uDom('#whitelistRevert').on('click', revertChanges);
2014-07-17 16:52:43 +02:00
2015-08-11 21:29:14 +02:00
renderWhitelist();
2014-07-17 16:52:43 +02:00
/******************************************************************************/
})();