/*******************************************************************************
µ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
*/
/* global vAPI, uDom */
/******************************************************************************/
(function() {
'use strict';
/******************************************************************************/
var userListName = vAPI.i18n('1pPageName');
var listDetails = {};
var cosmeticSwitch = true;
var externalLists = '';
var cacheWasPurged = false;
var needUpdate = false;
var hasCachedContent = false;
var re3rdPartyExternalAsset = /^https?:\/\/[a-z0-9]+/;
/******************************************************************************/
var onMessage = function(msg) {
switch ( msg.what ) {
case 'loadUbiquitousBlacklistCompleted':
renderBlacklists();
break;
default:
break;
}
};
var messager = vAPI.messaging.channel('3p-filters.js', onMessage);
/******************************************************************************/
var renderNumber = function(value) {
return value.toLocaleString();
};
/******************************************************************************/
// TODO: get rid of background page dependencies
var renderBlacklists = function() {
uDom('body').toggleClass('busy', true);
// Assemble a pretty blacklist name if possible
var listNameFromListKey = function(listKey) {
if ( listKey === listDetails.userFiltersPath ) {
return userListName;
}
var list = listDetails.current[listKey] || listDetails.available[listKey];
var listTitle = list ? list.title : '';
if ( listTitle === '' ) {
return listKey;
}
return listTitle;
};
// Assemble a pretty blacklist name if possible
var htmlFromHomeURL = function(entry) {
if ( !entry.homeDomain ) {
return '';
}
return [
' (',
entry.homeDomain,
')'
].join('');
};
var purgeButtontext = vAPI.i18n('3pExternalListPurge');
var updateButtontext = vAPI.i18n('3pExternalListNew');
var obsoleteButtontext = vAPI.i18n('3pExternalListObsolete');
var liTemplate = [
'
',
'',
' ',
'',
'{{name}}',
'\u200E',
'{{homeURL}}',
': ',
'',
vAPI.i18n('3pListsOfBlockedHostsPerListStats'),
''
].join('');
var htmlFromLeaf = function(listKey) {
var html = [];
var list = listDetails.available[listKey];
var li = liTemplate
.replace('{{checked}}', list.off ? '' : 'checked')
.replace('{{URL}}', encodeURI(listKey))
.replace('{{name}}', listNameFromListKey(listKey))
.replace('{{homeURL}}', htmlFromHomeURL(list))
.replace('{{used}}', !list.off && !isNaN(+list.entryUsedCount) ? renderNumber(list.entryUsedCount) : '0')
.replace('{{total}}', !isNaN(+list.entryCount) ? renderNumber(list.entryCount) : '?');
html.push(li);
// https://github.com/gorhill/uBlock/issues/104
var asset = listDetails.cache[listKey];
if ( asset === undefined ) {
return html.join('\n');
}
// Update status
if ( list.off !== true ) {
var obsolete = asset.repoObsolete ||
asset.cacheObsolete ||
asset.cached !== true && re3rdPartyExternalAsset.test(listKey);
if ( obsolete ) {
html.push(
' ',
'',
asset.repoObsolete ? updateButtontext : obsoleteButtontext,
''
);
needUpdate = true;
}
}
// In cache
if ( asset.cached ) {
html.push(
' ',
'',
purgeButtontext,
''
);
hasCachedContent = true;
}
return html.join('\n');
};
var htmlFromBranch = function(groupKey, listKeys) {
var html = [
'