2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2015-03-07 19:20:18 +01:00
|
|
|
µBlock - a browser extension to block requests.
|
2014-06-24 00:42:43 +02:00
|
|
|
Copyright (C) 2014 Raymond Hill
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2015-08-12 18:17:39 +02:00
|
|
|
/* global vAPI, uDom, uBlockDashboard */
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
2014-11-29 19:04:34 +01:00
|
|
|
'use strict';
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var cachedUserFilters = '';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
var messager = vAPI.messaging.channel('1p-filters.js');
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// This is to give a visual hint that the content of user blacklist has changed.
|
|
|
|
|
2014-10-13 17:01:31 +02:00
|
|
|
function userFiltersChanged() {
|
2015-08-12 18:17:39 +02:00
|
|
|
var changed = uDom.nodeFromId('userFilters').value.trim() !== cachedUserFilters;
|
|
|
|
uDom.nodeFromId('userFiltersApply').disabled = !changed;
|
|
|
|
uDom.nodeFromId('userFiltersRevert').disabled = !changed;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function renderUserFilters() {
|
|
|
|
var onRead = function(details) {
|
|
|
|
if ( details.error ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cachedUserFilters = details.content.trim();
|
2015-08-11 21:29:14 +02:00
|
|
|
uDom.nodeFromId('userFilters').value = details.content;
|
2015-08-12 18:17:39 +02:00
|
|
|
userFiltersChanged();
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
2014-10-17 21:44:19 +02:00
|
|
|
messager.send({ what: 'readUserFilters' }, onRead);
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function allFiltersApplyHandler() {
|
2014-10-17 21:44:19 +02:00
|
|
|
messager.send({ what: 'reloadAllFilters' });
|
2014-07-02 18:02:29 +02:00
|
|
|
uDom('#userFiltersApply').prop('disabled', true );
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-29 19:04:34 +01:00
|
|
|
var handleImportFilePicker = function() {
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1004
|
2015-03-15 19:07:30 +01:00
|
|
|
// Support extraction of filters from ABP backup file
|
|
|
|
var abpImporter = function(s) {
|
2015-05-23 00:03:14 +02:00
|
|
|
var reAbpSubscriptionExtractor = /\n\[Subscription\]\n+url=~[^\n]+([\x08-\x7E]*?)(?:\[Subscription\]|$)/ig;
|
|
|
|
var reAbpFilterExtractor = /\[Subscription filters\]([\x08-\x7E]*?)(?:\[Subscription\]|$)/i;
|
|
|
|
var matches = reAbpSubscriptionExtractor.exec(s);
|
2015-03-15 19:07:30 +01:00
|
|
|
// Not an ABP backup file
|
|
|
|
if ( matches === null ) {
|
|
|
|
return s;
|
|
|
|
}
|
2015-05-23 00:03:14 +02:00
|
|
|
//
|
2015-03-15 19:07:30 +01:00
|
|
|
var out = [];
|
2015-05-23 00:03:14 +02:00
|
|
|
var filterMatch;
|
2015-03-15 19:07:30 +01:00
|
|
|
while ( matches !== null ) {
|
2015-05-23 00:03:14 +02:00
|
|
|
if ( matches.length === 2 ) {
|
|
|
|
filterMatch = reAbpFilterExtractor.exec(matches[1].trim());
|
|
|
|
if ( filterMatch !== null && filterMatch.length === 2 ) {
|
|
|
|
out.push(filterMatch[1].trim().replace(/\\\[/g, '['));
|
|
|
|
}
|
2015-03-15 19:07:30 +01:00
|
|
|
}
|
2015-05-23 00:03:14 +02:00
|
|
|
matches = reAbpSubscriptionExtractor.exec(s);
|
2015-03-15 19:07:30 +01:00
|
|
|
}
|
|
|
|
return out.join('\n');
|
|
|
|
};
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
var fileReaderOnLoadHandler = function() {
|
2015-03-15 19:07:30 +01:00
|
|
|
var sanitized = abpImporter(this.result);
|
2014-07-02 18:02:29 +02:00
|
|
|
var textarea = uDom('#userFilters');
|
2015-03-15 19:07:30 +01:00
|
|
|
textarea.val(textarea.val().trim() + '\n' + sanitized);
|
2014-06-24 00:42:43 +02:00
|
|
|
userFiltersChanged();
|
|
|
|
};
|
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);
|
2014-11-29 19:04:34 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-13 17:01:31 +02:00
|
|
|
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-11-29 19:04:34 +01:00
|
|
|
var exportUserFiltersToFile = function() {
|
2014-10-20 19:05:12 +02:00
|
|
|
var val = uDom('#userFilters').val().trim();
|
2014-11-29 19:04:34 +01:00
|
|
|
if ( val === '' ) {
|
|
|
|
return;
|
2014-10-20 19:05:12 +02:00
|
|
|
}
|
2014-11-29 19:04:34 +01:00
|
|
|
var now = new Date();
|
2014-11-30 02:36:53 +01:00
|
|
|
var filename = vAPI.i18n('1pExportFilename')
|
|
|
|
.replace('{{datetime}}', now.toLocaleString())
|
|
|
|
.replace(/ +/g, '_');
|
2014-11-29 19:04:34 +01:00
|
|
|
vAPI.download({
|
2015-08-12 18:17:39 +02:00
|
|
|
'url': 'data:text/plain;charset=utf-8,' + encodeURIComponent(val + '\n'),
|
2014-11-30 02:36:53 +01:00
|
|
|
'filename': filename
|
2014-11-29 19:04:34 +01:00
|
|
|
});
|
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-08-12 18:17:39 +02:00
|
|
|
var applyChanges = function() {
|
2015-12-07 14:59:22 +01:00
|
|
|
var textarea = uDom.nodeFromId('userFilters');
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
var onWritten = function(details) {
|
|
|
|
if ( details.error ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-07 14:59:22 +01:00
|
|
|
textarea.value = details.content;
|
2014-06-24 00:42:43 +02:00
|
|
|
cachedUserFilters = details.content.trim();
|
|
|
|
userFiltersChanged();
|
|
|
|
allFiltersApplyHandler();
|
2015-12-07 14:59:22 +01:00
|
|
|
textarea.focus();
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
2015-12-07 14:59:22 +01:00
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
var request = {
|
|
|
|
what: 'writeUserFilters',
|
2015-12-07 14:59:22 +01:00
|
|
|
content: textarea.value
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
2014-10-17 21:44:19 +02:00
|
|
|
messager.send(request, onWritten);
|
2014-11-29 19:04:34 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-08-12 18:17:39 +02:00
|
|
|
var revertChanges = function() {
|
|
|
|
uDom.nodeFromId('userFilters').value = cachedUserFilters + '\n';
|
|
|
|
userFiltersChanged();
|
|
|
|
};
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-08-11 21:29:14 +02:00
|
|
|
var getCloudData = function() {
|
|
|
|
return uDom.nodeFromId('userFilters').value;
|
|
|
|
};
|
|
|
|
|
2015-08-12 18:17:39 +02:00
|
|
|
var setCloudData = function(data, append) {
|
2015-08-11 21:29:14 +02:00
|
|
|
if ( typeof data !== 'string' ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-12 18:17:39 +02:00
|
|
|
var textarea = uDom.nodeFromId('userFilters');
|
|
|
|
if ( append ) {
|
|
|
|
data = uBlockDashboard.mergeNewLines(textarea.value, data);
|
|
|
|
}
|
|
|
|
textarea.value = data;
|
2015-08-11 21:29:14 +02:00
|
|
|
userFiltersChanged();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.cloud.onPush = getCloudData;
|
|
|
|
self.cloud.onPull = setCloudData;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Handle user interaction
|
|
|
|
uDom('#importUserFiltersFromFile').on('click', startImportFilePicker);
|
|
|
|
uDom('#importFilePicker').on('change', handleImportFilePicker);
|
|
|
|
uDom('#exportUserFiltersToFile').on('click', exportUserFiltersToFile);
|
|
|
|
uDom('#userFilters').on('input', userFiltersChanged);
|
2015-08-12 18:17:39 +02:00
|
|
|
uDom('#userFiltersApply').on('click', applyChanges);
|
|
|
|
uDom('#userFiltersRevert').on('click', revertChanges);
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-08-11 21:29:14 +02:00
|
|
|
renderUserFilters();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-13 17:01:31 +02:00
|
|
|
// https://www.youtube.com/watch?v=UNilsLf6eW4
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
})();
|