1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00
uBlock/js/3p-filters.js

231 lines
7.3 KiB
JavaScript
Raw Normal View History

2014-06-24 00:42:43 +02: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 18:02:29 +02:00
/* global chrome, messaging, uDom */
2014-06-24 00:42:43 +02:00
/******************************************************************************/
(function() {
/******************************************************************************/
var userListName = chrome.i18n.getMessage('1pPageName');
var selectedBlacklistsHash = '';
/******************************************************************************/
messaging.start('lists.js');
var onMessage = function(msg) {
switch ( msg.what ) {
case 'loadUbiquitousBlacklistCompleted':
renderBlacklists();
selectedBlacklistsChanged();
break;
default:
break;
}
};
messaging.listen(onMessage);
/******************************************************************************/
function getµb() {
return chrome.extension.getBackgroundPage().µBlock;
}
/******************************************************************************/
function renderNumber(value) {
// TODO: localization
if ( +value > 1000 ) {
value = value.toString();
var i = value.length - 3;
while ( i > 0 ) {
value = value.slice(0, i) + ',' + value.slice(i);
i -= 3;
}
}
return value;
}
/******************************************************************************/
// TODO: get rid of background page dependencies
function renderBlacklists() {
// empty list first
2014-07-02 18:02:29 +02:00
uDom('#blacklists .blacklistDetails').remove();
2014-06-24 00:42:43 +02:00
var µb = getµb();
2014-07-02 18:02:29 +02:00
uDom('#listsOfBlockedHostsPrompt').text(
chrome.i18n.getMessage('3pListsOfBlockedHostsPrompt')
2014-06-24 00:42:43 +02:00
.replace('{{ubiquitousBlacklistCount}}', renderNumber(µb.abpFilters.getFilterCount()))
);
// Assemble a pretty blacklist name if possible
var prettifyListName = function(blacklistTitle, blacklistHref) {
if ( blacklistHref === µb.userFiltersPath ) {
return userListName;
}
if ( !blacklistTitle ) {
return blacklistHref;
}
if ( blacklistHref.indexOf('assets/thirdparties/') !== 0 ) {
return blacklistTitle;
}
var matches = blacklistHref.match(/^assets\/thirdparties\/([^\/]+)/);
if ( matches === null || matches.length !== 2 ) {
return blacklistTitle;
}
var hostname = matches[1];
var domain = µb.URI.domainFromHostname(hostname);
if ( domain === '' ) {
return blacklistTitle;
}
var html = [
blacklistTitle,
' <i>(<a href="http://',
hostname,
'" target="_blank">',
domain,
'</a>)</i>'
];
return html.join('');
};
2014-06-24 03:49:05 +02:00
var listStatsTemplate = chrome.i18n.getMessage('3pListsOfBlockedHostsPerListStats');
2014-06-24 00:42:43 +02:00
var blacklists = µb.remoteBlacklists;
2014-07-02 18:02:29 +02:00
var ul = uDom('#blacklists');
2014-06-24 00:42:43 +02:00
var keys = Object.keys(blacklists);
var i = keys.length;
var blacklist, blacklistHref;
2014-07-02 18:02:29 +02:00
var liTemplate = uDom('#blacklistTemplate .blacklistDetails').first();
var li, text;
2014-06-24 00:42:43 +02:00
while ( i-- ) {
blacklistHref = keys[i];
blacklist = blacklists[blacklistHref];
li = liTemplate.clone();
2014-07-02 18:02:29 +02:00
li.find('input').prop('checked', !blacklist.off);
li.find('a')
.attr('href', encodeURI(blacklistHref))
.html(prettifyListName(blacklist.title, blacklistHref));
2014-06-24 03:49:05 +02:00
text = listStatsTemplate
2014-06-24 00:42:43 +02:00
.replace('{{used}}', !blacklist.off && !isNaN(+blacklist.entryUsedCount) ? renderNumber(blacklist.entryUsedCount) : '0')
.replace('{{total}}', !isNaN(+blacklist.entryCount) ? renderNumber(blacklist.entryCount) : '?')
;
2014-07-02 18:02:29 +02:00
li.find('span').text(text);
2014-06-24 00:42:43 +02:00
ul.prepend(li);
}
2014-07-02 18:02:29 +02:00
uDom('#parseAllABPHideFilters').attr('checked', µb.userSettings.parseAllABPHideFilters === true);
uDom('#ubiquitousParseAllABPHideFiltersPrompt2').text(
2014-06-24 00:42:43 +02:00
chrome.i18n.getMessage("listsParseAllABPHideFiltersPrompt2")
.replace('{{abpHideFilterCount}}', renderNumber(µb.abpHideFilters.getFilterCount()))
);
selectedBlacklistsHash = getSelectedBlacklistsHash();
}
/******************************************************************************/
// Create a hash so that we know whether the selection of preset blacklists
// has changed.
function getSelectedBlacklistsHash() {
var hash = '';
2014-07-02 18:02:29 +02:00
var inputs = uDom('#blacklists .blacklistDetails > input');
var i = inputs.length();
2014-06-24 00:42:43 +02:00
while ( i-- ) {
2014-07-02 18:02:29 +02:00
hash += inputs.subset(i).prop('checked').toString();
2014-06-24 00:42:43 +02:00
}
2014-07-02 18:02:29 +02:00
// Factor in whether cosmetic filters are to be processed
hash += uDom('#parseAllABPHideFilters').prop('checked').toString();
2014-06-24 00:42:43 +02:00
return hash;
}
/******************************************************************************/
// This is to give a visual hint that the selection of blacklists has changed.
function selectedBlacklistsChanged() {
2014-07-02 18:02:29 +02:00
uDom('#blacklistsApply').prop(
2014-06-24 00:42:43 +02:00
'disabled',
getSelectedBlacklistsHash() === selectedBlacklistsHash
);
}
/******************************************************************************/
function blacklistsApplyHandler() {
var newHash = getSelectedBlacklistsHash();
if ( newHash === selectedBlacklistsHash ) {
return;
}
// Reload blacklists
var switches = [];
2014-07-02 18:02:29 +02:00
var lis = uDom('#blacklists .blacklistDetails');
var i = lis.length();
2014-06-24 00:42:43 +02:00
var path;
while ( i-- ) {
2014-07-02 18:02:29 +02:00
path = lis.subset(i).find('a').attr('href');
2014-06-24 00:42:43 +02:00
switches.push({
location: path,
2014-07-02 18:02:29 +02:00
off: lis.subset(i).find('input').prop('checked') === false
2014-06-24 00:42:43 +02:00
});
}
messaging.tell({
what: 'reloadAllFilters',
switches: switches
});
2014-07-02 18:02:29 +02:00
uDom('#blacklistsApply').attr('disabled', true );
2014-06-24 00:42:43 +02:00
}
/******************************************************************************/
function abpHideFiltersCheckboxChanged() {
messaging.tell({
what: 'userSettings',
name: 'parseAllABPHideFilters',
2014-07-02 18:02:29 +02:00
value: this.checked
2014-06-24 00:42:43 +02:00
});
selectedBlacklistsChanged();
}
/******************************************************************************/
2014-07-02 18:02:29 +02:00
uDom.onLoad(function() {
2014-06-24 00:42:43 +02:00
// Handle user interaction
2014-07-02 18:02:29 +02:00
uDom('#blacklists').on('change', '.blacklistDetails', selectedBlacklistsChanged);
uDom('#blacklistsApply').on('click', blacklistsApplyHandler);
uDom('#parseAllABPHideFilters').on('change', abpHideFiltersCheckboxChanged);
2014-06-24 00:42:43 +02:00
renderBlacklists();
});
/******************************************************************************/
})();