uBlock/js/1p-filters.js

145 lines
4.5 KiB
JavaScript
Raw Normal View History

2014-06-23 16:42:43 -06:00
/*******************************************************************************
µBlock - a Chromium browser extension to block requests.
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
*/
2014-07-02 10:02:29 -06:00
/* global chrome, messaging, uDom */
2014-06-23 16:42:43 -06:00
/******************************************************************************/
(function() {
/******************************************************************************/
var cachedUserFilters = '';
/******************************************************************************/
messaging.start('1p-filters.js');
/******************************************************************************/
// This is to give a visual hint that the content of user blacklist has changed.
2014-10-13 09:01:31 -06:00
function userFiltersChanged() {
2014-07-02 10:02:29 -06:00
uDom('#userFiltersApply').prop(
'disabled',
uDom('#userFilters').val().trim() === cachedUserFilters
);
2014-06-23 16:42:43 -06:00
}
/******************************************************************************/
function renderUserFilters() {
var onRead = function(details) {
if ( details.error ) {
return;
}
cachedUserFilters = details.content.trim();
2014-07-02 10:02:29 -06:00
uDom('#userFilters').val(details.content);
2014-06-23 16:42:43 -06:00
};
messaging.ask({ what: 'readUserFilters' }, onRead);
}
/******************************************************************************/
function allFiltersApplyHandler() {
messaging.tell({ what: 'reloadAllFilters' });
2014-07-02 10:02:29 -06:00
uDom('#userFiltersApply').prop('disabled', true );
2014-06-23 16:42:43 -06:00
}
/******************************************************************************/
2014-10-13 09:01:31 -06:00
function handleImportFilePicker() {
2014-06-23 16:42:43 -06:00
var fileReaderOnLoadHandler = function() {
2014-07-02 10:02:29 -06:00
var textarea = uDom('#userFilters');
textarea.val([textarea.val(), this.result].join('\n').trim());
2014-06-23 16:42:43 -06:00
userFiltersChanged();
};
2014-10-13 09:01:31 -06: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-06-23 16:42:43 -06:00
}
/******************************************************************************/
2014-10-13 09:01:31 -06: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-06-23 16:42:43 -06:00
function exportUserFiltersToFile() {
chrome.downloads.download({
2014-07-02 10:02:29 -06:00
'url': 'data:text/plain,' + encodeURIComponent(uDom('#userFilters').val()),
2014-06-23 16:42:43 -06:00
'filename': 'my-ublock-filters.txt',
'saveAs': true
});
}
/******************************************************************************/
function userFiltersApplyHandler() {
var onWritten = function(details) {
if ( details.error ) {
return;
}
cachedUserFilters = details.content.trim();
userFiltersChanged();
allFiltersApplyHandler();
};
var request = {
what: 'writeUserFilters',
2014-07-02 10:02:29 -06:00
content: uDom('#userFilters').val()
2014-06-23 16:42:43 -06:00
};
messaging.ask(request, onWritten);
}
/******************************************************************************/
2014-07-02 10:02:29 -06:00
uDom.onLoad(function() {
2014-06-23 16:42:43 -06:00
// Handle user interaction
2014-10-13 09:01:31 -06:00
uDom('#importUserFiltersFromFile').on('click', startImportFilePicker);
uDom('#importFilePicker').on('change', handleImportFilePicker);
2014-07-02 10:02:29 -06:00
uDom('#exportUserFiltersToFile').on('click', exportUserFiltersToFile);
uDom('#userFilters').on('input', userFiltersChanged);
uDom('#userFiltersApply').on('click', userFiltersApplyHandler);
2014-06-23 16:42:43 -06:00
renderUserFilters();
});
/******************************************************************************/
2014-10-13 09:01:31 -06:00
// https://www.youtube.com/watch?v=UNilsLf6eW4
2014-06-23 16:42:43 -06:00
})();