2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-03-01 00:51:33 +01:00
|
|
|
Copyright (C) 2014-2018 Raymond Hill
|
2014-06-24 00:42: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
|
|
|
|
*/
|
|
|
|
|
2018-03-01 00:51:33 +01:00
|
|
|
/* global CodeMirror, uDom, uBlockDashboard */
|
|
|
|
|
|
|
|
'use strict';
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const messaging = vAPI.messaging;
|
|
|
|
const cmEditor = new CodeMirror(
|
2018-03-01 00:51:33 +01:00
|
|
|
document.getElementById('userFilters'),
|
|
|
|
{
|
2018-03-04 20:07:01 +01:00
|
|
|
autofocus: true,
|
2018-03-01 00:51:33 +01:00
|
|
|
lineNumbers: true,
|
|
|
|
lineWrapping: true,
|
2018-03-05 12:59:03 +01:00
|
|
|
styleActiveLine: true
|
2018-03-01 00:51:33 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-03-28 01:10:31 +02:00
|
|
|
uBlockDashboard.patchCodeMirrorEditor(cmEditor);
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
let cachedUserFilters = '';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/3706
|
|
|
|
// Save/restore cursor position
|
|
|
|
//
|
|
|
|
// CoreMirror reference: https://codemirror.net/doc/manual.html#api_selection
|
|
|
|
|
|
|
|
window.addEventListener('beforeunload', ( ) => {
|
|
|
|
vAPI.localStorage.setItem(
|
|
|
|
'myFiltersCursorPosition',
|
|
|
|
JSON.stringify(cmEditor.getCursor().line)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// This is to give a visual hint that the content of user blacklist has changed.
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const userFiltersChanged = function(changed) {
|
2016-12-07 15:43:11 +01:00
|
|
|
if ( typeof changed !== 'boolean' ) {
|
2018-03-01 00:51:33 +01:00
|
|
|
changed = cmEditor.getValue().trim() !== cachedUserFilters;
|
2016-12-07 15:43:11 +01:00
|
|
|
}
|
2015-08-12 18:17:39 +02:00
|
|
|
uDom.nodeFromId('userFiltersApply').disabled = !changed;
|
|
|
|
uDom.nodeFromId('userFiltersRevert').disabled = !changed;
|
2018-12-22 19:09:56 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const renderUserFilters = function(first) {
|
|
|
|
const onRead = function(details) {
|
2016-12-07 15:43:11 +01:00
|
|
|
if ( details.error ) { return; }
|
2018-12-22 19:09:56 +01:00
|
|
|
let content = details.content.trim();
|
2018-03-01 00:51:33 +01:00
|
|
|
cachedUserFilters = content;
|
|
|
|
if ( content.length !== 0 ) {
|
|
|
|
content += '\n';
|
|
|
|
}
|
|
|
|
cmEditor.setValue(content);
|
2016-12-07 15:43:11 +01:00
|
|
|
if ( first ) {
|
2018-03-01 00:51:33 +01:00
|
|
|
cmEditor.clearHistory();
|
2018-12-22 19:09:56 +01:00
|
|
|
try {
|
|
|
|
const line = JSON.parse(
|
|
|
|
vAPI.localStorage.getItem('myFiltersCursorPosition')
|
|
|
|
);
|
|
|
|
if ( typeof line === 'number' ) {
|
|
|
|
cmEditor.setCursor(line, 0);
|
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
2016-12-07 15:43:11 +01:00
|
|
|
}
|
|
|
|
userFiltersChanged(false);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send('dashboard', { what: 'readUserFilters' }, onRead);
|
2018-12-22 19:09:56 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const allFiltersApplyHandler = function() {
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send('dashboard', { what: 'reloadAllFilters' });
|
2014-07-02 18:02:29 +02:00
|
|
|
uDom('#userFiltersApply').prop('disabled', true );
|
2018-12-22 19:09:56 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const 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
|
2018-12-22 19:09:56 +01:00
|
|
|
const abpImporter = function(s) {
|
|
|
|
const reAbpSubscriptionExtractor = /\n\[Subscription\]\n+url=~[^\n]+([\x08-\x7E]*?)(?:\[Subscription\]|$)/ig;
|
|
|
|
const reAbpFilterExtractor = /\[Subscription filters\]([\x08-\x7E]*?)(?:\[Subscription\]|$)/i;
|
|
|
|
let matches = reAbpSubscriptionExtractor.exec(s);
|
2015-03-15 19:07:30 +01:00
|
|
|
// Not an ABP backup file
|
2018-12-22 19:09:56 +01:00
|
|
|
if ( matches === null ) { return s; }
|
2015-05-23 00:03:14 +02:00
|
|
|
//
|
2018-12-22 19:09:56 +01:00
|
|
|
const out = [];
|
2015-03-15 19:07:30 +01:00
|
|
|
while ( matches !== null ) {
|
2015-05-23 00:03:14 +02:00
|
|
|
if ( matches.length === 2 ) {
|
2018-12-22 19:09:56 +01:00
|
|
|
let filterMatch = reAbpFilterExtractor.exec(matches[1].trim());
|
2015-05-23 00:03:14 +02:00
|
|
|
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');
|
|
|
|
};
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const fileReaderOnLoadHandler = function() {
|
|
|
|
const sanitized = abpImporter(this.result);
|
2018-03-12 17:08:45 +01:00
|
|
|
cmEditor.setValue(cmEditor.getValue().trim() + '\n' + sanitized);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
2018-12-22 19:09:56 +01:00
|
|
|
const file = this.files[0];
|
|
|
|
if ( file === undefined || file.name === '' ) { return; }
|
|
|
|
if ( file.type.indexOf('text') !== 0 ) { return; }
|
|
|
|
const fr = new FileReader();
|
2014-10-13 17:01:31 +02:00
|
|
|
fr.onload = fileReaderOnLoadHandler;
|
|
|
|
fr.readAsText(file);
|
2014-11-29 19:04:34 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const startImportFilePicker = function() {
|
|
|
|
const input = document.getElementById('importFilePicker');
|
2014-10-13 17:01:31 +02:00
|
|
|
// 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();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const exportUserFiltersToFile = function() {
|
|
|
|
const val = cmEditor.getValue().trim();
|
2018-03-12 17:08:45 +01:00
|
|
|
if ( val === '' ) { return; }
|
2018-12-22 19:09:56 +01:00
|
|
|
const filename = vAPI.i18n('1pExportFilename')
|
2016-10-13 19:25:57 +02:00
|
|
|
.replace('{{datetime}}', uBlockDashboard.dateNowToSensibleString())
|
2014-11-30 02:36:53 +01:00
|
|
|
.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
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const applyChanges = function() {
|
2018-03-12 17:08:45 +01:00
|
|
|
messaging.send(
|
|
|
|
'dashboard',
|
|
|
|
{
|
|
|
|
what: 'writeUserFilters',
|
|
|
|
content: cmEditor.getValue()
|
|
|
|
},
|
2018-12-22 19:09:56 +01:00
|
|
|
details => {
|
|
|
|
if ( details.error ) { return; }
|
|
|
|
cachedUserFilters = details.content.trim();
|
|
|
|
allFiltersApplyHandler();
|
|
|
|
}
|
2018-03-12 17:08:45 +01:00
|
|
|
);
|
2014-11-29 19:04:34 +01:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const revertChanges = function() {
|
|
|
|
let content = cachedUserFilters;
|
2018-03-01 00:51:33 +01:00
|
|
|
if ( content.length !== 0 ) {
|
|
|
|
content += '\n';
|
|
|
|
}
|
|
|
|
cmEditor.setValue(content);
|
2015-08-12 18:17:39 +02:00
|
|
|
};
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const getCloudData = function() {
|
2018-03-01 00:51:33 +01:00
|
|
|
return cmEditor.getValue();
|
2015-08-11 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 19:09:56 +01:00
|
|
|
const setCloudData = function(data, append) {
|
2018-03-12 17:08:45 +01:00
|
|
|
if ( typeof data !== 'string' ) { return; }
|
2015-08-12 18:17:39 +02:00
|
|
|
if ( append ) {
|
2018-03-12 17:08:45 +01:00
|
|
|
data = uBlockDashboard.mergeNewLines(cmEditor.getValue(), data);
|
2015-08-12 18:17:39 +02:00
|
|
|
}
|
2018-03-01 00:51:33 +01:00
|
|
|
cmEditor.setValue(data);
|
2015-08-11 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
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
|
|
|
|
2016-12-07 15:43:11 +01:00
|
|
|
renderUserFilters(true);
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2018-03-01 00:51:33 +01:00
|
|
|
cmEditor.on('changes', userFiltersChanged);
|
2018-03-01 19:37:23 +01:00
|
|
|
CodeMirror.commands.save = applyChanges;
|
2018-03-01 00:51:33 +01:00
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|