uBlock/src/js/3p-filters.js

489 lines
15 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-11-26 19:32:10 +01:00
/* global vAPI, uDom */
2014-06-24 00:42:43 +02:00
/******************************************************************************/
(function() {
2014-11-26 19:32:10 +01:00
'use strict';
2014-06-24 00:42:43 +02:00
/******************************************************************************/
var userListName = vAPI.i18n('1pPageName');
2014-07-25 22:12:20 +02:00
var listDetails = {};
2014-08-20 16:26:57 +02:00
var cosmeticSwitch = true;
2014-07-25 22:12:20 +02:00
var externalLists = '';
2014-07-26 22:10:20 +02:00
var cacheWasPurged = false;
var needUpdate = false;
2014-09-09 01:45:22 +02:00
var hasCachedContent = false;
2014-11-26 19:32:10 +01:00
var re3rdPartyExternalAsset = /^https?:\/\/[a-z0-9]+/;
2014-06-24 00:42:43 +02:00
/******************************************************************************/
var onMessage = function(msg) {
switch ( msg.what ) {
2015-02-24 00:31:29 +01:00
case 'allFilterListsReloaded':
2014-06-24 00:42:43 +02:00
renderBlacklists();
break;
default:
break;
}
};
var messager = vAPI.messaging.channel('3p-filters.js', onMessage);
2014-06-24 00:42:43 +02:00
/******************************************************************************/
2014-07-09 22:03:25 +02:00
var renderNumber = function(value) {
return value.toLocaleString();
2014-07-09 22:03:25 +02:00
};
2014-06-24 00:42:43 +02:00
/******************************************************************************/
// TODO: get rid of background page dependencies
2014-07-09 22:03:25 +02:00
var renderBlacklists = function() {
2014-09-09 16:53:47 +02:00
uDom('body').toggleClass('busy', true);
2014-06-24 00:42:43 +02:00
// Assemble a pretty blacklist name if possible
var listNameFromListKey = function(listKey) {
if ( listKey === listDetails.userFiltersPath ) {
2014-06-24 00:42:43 +02:00
return userListName;
}
var list = listDetails.current[listKey] || listDetails.available[listKey];
var listTitle = list ? list.title : '';
if ( listTitle === '' ) {
return listKey;
2014-06-24 00:42:43 +02:00
}
return listTitle;
2014-08-23 18:08:02 +02:00
};
// Assemble a pretty blacklist name if possible
var htmlFromHomeURL = function(entry) {
if ( !entry.homeDomain ) {
2014-08-23 18:08:02 +02:00
return '';
2014-06-24 00:42:43 +02:00
}
2014-11-26 19:32:10 +01:00
return [
2014-08-23 18:08:02 +02:00
' <a href="http://',
entry.homeHostname,
2014-08-23 18:08:02 +02:00
'" target="_blank">(',
entry.homeDomain,
2014-08-23 18:08:02 +02:00
')</a>'
2014-11-26 19:32:10 +01:00
].join('');
2014-06-24 00:42:43 +02:00
};
var purgeButtontext = vAPI.i18n('3pExternalListPurge');
var updateButtontext = vAPI.i18n('3pExternalListNew');
var obsoleteButtontext = vAPI.i18n('3pExternalListObsolete');
2014-09-09 16:53:47 +02:00
var liTemplate = [
'<li class="listDetails">',
'<input type="checkbox" {{checked}}>',
' ',
'<a data-href="{{URL}}" type="text/plain">',
2014-09-09 16:53:47 +02:00
'{{name}}',
'\u200E</a>',
'{{homeURL}}',
': ',
'<span class="dim">',
vAPI.i18n('3pListsOfBlockedHostsPerListStats'),
2014-09-09 16:53:47 +02:00
'</span>'
].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))
2014-09-09 16:53:47 +02:00
.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(
'&ensp;',
'<span class="status obsolete">',
asset.repoObsolete ? updateButtontext : obsoleteButtontext,
'</span>'
);
needUpdate = true;
}
}
// In cache
if ( asset.cached ) {
html.push(
'&ensp;',
'<span class="status purge">',
purgeButtontext,
'</span>'
);
hasCachedContent = true;
}
return html.join('\n');
};
2014-09-09 16:53:47 +02:00
var htmlFromBranch = function(groupKey, listKeys) {
var html = [
'<li>',
vAPI.i18n('3pGroup' + groupKey.charAt(0).toUpperCase() + groupKey.slice(1)),
'<ul>'
];
2014-07-25 22:12:20 +02:00
if ( !listKeys ) {
return html.join('');
}
listKeys.sort(function(a, b) {
return (listDetails.available[a].title || "").localeCompare(listDetails.available[b].title || "");
2014-07-25 22:12:20 +02:00
});
for ( var i = 0; i < listKeys.length; i++ ) {
2014-09-09 16:53:47 +02:00
html.push(htmlFromLeaf(listKeys[i]));
}
html.push('</ul>');
return html.join('');
};
// https://www.youtube.com/watch?v=unCVi4hYRlY#t=30m18s
var groupsFromLists = function(lists) {
var groups = {};
var listKeys = Object.keys(lists);
var i = listKeys.length;
var listKey, list, groupKey;
while ( i-- ) {
listKey = listKeys[i];
list = lists[listKey];
groupKey = list.group || 'nogroup';
if ( groups[groupKey] === undefined ) {
groups[groupKey] = [];
}
groups[groupKey].push(listKey);
}
return groups;
};
2014-07-25 22:12:20 +02:00
var onListsReceived = function(details) {
2014-09-09 16:53:47 +02:00
// Before all, set context vars
2014-07-25 22:12:20 +02:00
listDetails = details;
2014-08-20 16:26:57 +02:00
cosmeticSwitch = details.cosmetic;
needUpdate = false;
2014-09-09 01:45:22 +02:00
hasCachedContent = false;
2014-07-25 22:12:20 +02:00
2014-09-09 16:53:47 +02:00
// Visually split the filter lists in purpose-based groups
2014-07-25 22:12:20 +02:00
var html = [];
2014-09-09 16:53:47 +02:00
var groups = groupsFromLists(details.available);
2014-07-25 22:12:20 +02:00
var groupKey, i;
var groupKeys = [
'default',
'ads',
'privacy',
'malware',
'social',
'multipurpose',
'regions',
'custom'
];
for ( i = 0; i < groupKeys.length; i++ ) {
groupKey = groupKeys[i];
2014-09-09 16:53:47 +02:00
html.push(htmlFromBranch(groupKey, groups[groupKey]));
2014-07-25 22:12:20 +02:00
delete groups[groupKey];
}
// For all groups not covered above (if any left)
groupKeys = Object.keys(groups);
for ( i = 0; i < groupKeys.length; i++ ) {
groupKey = groupKeys[i];
2014-09-09 16:53:47 +02:00
html.push(htmlFromBranch(groupKey, groups[groupKey]));
2014-07-25 22:12:20 +02:00
delete groups[groupKey];
}
2014-08-20 16:26:57 +02:00
uDom('#listsOfBlockedHostsPrompt').text(
vAPI.i18n('3pListsOfBlockedHostsPrompt')
2014-08-20 16:26:57 +02:00
.replace('{{netFilterCount}}', renderNumber(details.netFilterCount))
.replace('{{cosmeticFilterCount}}', renderNumber(details.cosmeticFilterCount))
);
uDom('#autoUpdate').prop('checked', listDetails.autoUpdate === true);
uDom('#parseCosmeticFilters').prop('checked', listDetails.cosmetic === true);
2014-08-21 16:56:36 +02:00
uDom('#lists').html(html.join(''));
2014-07-25 22:12:20 +02:00
uDom('a').attr('target', '_blank');
// Firefox: sanitizer drops those `href` attributes that point to local URLs
var lis = uDom('a[data-href]');
var a;
i = lis.length;
while ( i-- ) {
a = lis.subset(i, 1);
a.attr('href', a.attr('data-href'));
}
updateWidgets();
2014-07-25 22:12:20 +02:00
};
2014-06-24 00:42:43 +02:00
messager.send({ what: 'getLists' }, onListsReceived);
2014-07-09 22:03:25 +02:00
};
2014-06-24 00:42:43 +02:00
/******************************************************************************/
// Return whether selection of lists changed.
2014-06-24 00:42:43 +02:00
var listsSelectionChanged = function() {
2014-08-20 16:26:57 +02:00
if ( listDetails.cosmetic !== cosmeticSwitch ) {
2014-07-25 22:12:20 +02:00
return true;
2014-06-24 00:42:43 +02:00
}
2014-07-26 22:10:20 +02:00
if ( cacheWasPurged ) {
return true;
}
2014-07-25 22:12:20 +02:00
var availableLists = listDetails.available;
var currentLists = listDetails.current;
var location, availableOff, currentOff;
// This check existing entries
for ( location in availableLists ) {
if ( availableLists.hasOwnProperty(location) === false ) {
continue;
}
availableOff = availableLists[location].off === true;
currentOff = currentLists[location] === undefined || currentLists[location].off === true;
if ( availableOff !== currentOff ) {
return true;
}
}
// This check removed entries
for ( location in currentLists ) {
if ( currentLists.hasOwnProperty(location) === false ) {
continue;
}
currentOff = currentLists[location].off === true;
availableOff = availableLists[location] === undefined || availableLists[location].off === true;
if ( availableOff !== currentOff ) {
return true;
}
}
return false;
2014-07-09 22:03:25 +02:00
};
2014-06-24 00:42:43 +02:00
/******************************************************************************/
// Return whether content need update.
var listsContentChanged = function() {
return needUpdate;
};
/******************************************************************************/
2014-06-24 00:42:43 +02:00
// This is to give a visual hint that the selection of blacklists has changed.
var updateWidgets = function() {
2014-09-09 16:53:47 +02:00
uDom('#buttonApply').toggleClass('disabled', !listsSelectionChanged());
uDom('#buttonUpdate').toggleClass('disabled', !listsContentChanged());
uDom('#buttonPurgeAll').toggleClass('disabled', !hasCachedContent);
uDom('body').toggleClass('busy', false);
2014-07-25 22:12:20 +02:00
};
/******************************************************************************/
var onListCheckboxChanged = function() {
2014-11-07 16:07:26 +01:00
var href = uDom(this).parent().descendants('a').first().attr('href');
2014-07-25 22:12:20 +02:00
if ( typeof href !== 'string' ) {
return;
}
if ( listDetails.available[href] === undefined ) {
return;
}
listDetails.available[href].off = !this.checked;
updateWidgets();
2014-07-09 22:03:25 +02:00
};
2014-06-24 00:42:43 +02:00
/******************************************************************************/
2014-07-09 22:03:25 +02:00
var onListLinkClicked = function(ev) {
messager.send({
what: 'gotoURL',
details: {
url: 'asset-viewer.html?url=' + uDom(this).attr('href'),
select: true,
index: -1
}
2014-07-09 22:03:25 +02:00
});
ev.preventDefault();
};
/******************************************************************************/
2014-08-20 05:01:36 +02:00
var onPurgeClicked = function() {
2014-07-26 22:10:20 +02:00
var button = uDom(this);
var li = button.parent();
2014-11-07 16:07:26 +01:00
var href = li.descendants('a').first().attr('href');
2014-07-26 22:10:20 +02:00
if ( !href ) {
return;
}
messager.send({ what: 'purgeCache', path: href });
2014-07-26 22:10:20 +02:00
button.remove();
2014-11-07 16:07:26 +01:00
if ( li.descendants('input').first().prop('checked') ) {
cacheWasPurged = true;
updateWidgets();
}
2014-07-26 22:10:20 +02:00
};
/******************************************************************************/
var reloadAll = function(update) {
2014-09-09 16:53:47 +02:00
// Loading may take a while when resources are fetched from remote
// servers. We do not want the user to force reload while we are reloading.
2014-09-09 16:53:47 +02:00
uDom('body').toggleClass('busy', true);
2014-06-24 00:42:43 +02:00
// Reload blacklists
messager.send({
2014-07-25 22:12:20 +02:00
what: 'userSettings',
name: 'parseAllABPHideFilters',
2014-07-26 19:31:22 +02:00
value: listDetails.cosmetic
2014-07-25 22:12:20 +02:00
});
// Reload blacklists
2014-06-24 00:42:43 +02:00
var switches = [];
var lis = uDom('#lists .listDetails');
2014-11-07 16:07:26 +01:00
var i = lis.length;
2014-06-24 00:42:43 +02:00
var path;
while ( i-- ) {
2014-07-20 21:17:35 +02:00
path = lis
2014-11-07 16:07:26 +01:00
.subset(i, 1)
.descendants('a')
2014-07-25 22:12:20 +02:00
.attr('href');
2014-06-24 00:42:43 +02:00
switches.push({
location: path,
2014-11-07 16:07:26 +01:00
off: lis.subset(i, 1).descendants('input').prop('checked') === false
2014-06-24 00:42:43 +02:00
});
}
messager.send({
2014-06-24 00:42:43 +02:00
what: 'reloadAllFilters',
switches: switches,
update: update
2014-06-24 00:42:43 +02:00
});
2014-07-26 22:10:20 +02:00
cacheWasPurged = false;
};
/******************************************************************************/
var buttonApplyHandler = function() {
2014-09-08 23:46:58 +02:00
reloadAll(false);
uDom('#buttonApply').toggleClass('enabled', false);
};
/******************************************************************************/
var buttonUpdateHandler = function() {
2014-08-20 07:18:53 +02:00
if ( needUpdate ) {
reloadAll(true);
}
};
/******************************************************************************/
2014-09-09 01:45:22 +02:00
var buttonPurgeAllHandler = function() {
var onCompleted = function() {
renderBlacklists();
};
messager.send({ what: 'purgeAllCaches' }, onCompleted);
2014-09-09 01:45:22 +02:00
};
/******************************************************************************/
var autoUpdateCheckboxChanged = function() {
messager.send({
what: 'userSettings',
name: 'autoUpdate',
value: this.checked
});
2014-07-09 22:03:25 +02:00
};
2014-06-24 00:42:43 +02:00
/******************************************************************************/
2014-08-20 16:26:57 +02:00
var cosmeticSwitchChanged = function() {
2014-07-25 22:12:20 +02:00
listDetails.cosmetic = this.checked;
updateWidgets();
2014-07-25 22:12:20 +02:00
};
/******************************************************************************/
var renderExternalLists = function() {
var onReceived = function(details) {
uDom('#externalLists').val(details);
externalLists = details;
};
messager.send({ what: 'userSettings', name: 'externalLists' }, onReceived);
2014-07-25 22:12:20 +02:00
};
/******************************************************************************/
var externalListsChangeHandler = function() {
uDom('#externalListsApply').prop(
'disabled',
this.value.trim() === externalLists
);
};
/******************************************************************************/
var externalListsApplyHandler = function() {
externalLists = uDom('#externalLists').val();
messager.send({
2014-06-24 00:42:43 +02:00
what: 'userSettings',
2014-07-25 22:12:20 +02:00
name: 'externalLists',
value: externalLists
2014-06-24 00:42:43 +02:00
});
2014-07-25 22:12:20 +02:00
renderBlacklists();
uDom('#externalListsApply').prop('disabled', true);
2014-07-09 22:03:25 +02:00
};
2014-06-24 00:42:43 +02:00
/******************************************************************************/
2014-07-02 18:02:29 +02:00
uDom.onLoad(function() {
uDom('#autoUpdate').on('change', autoUpdateCheckboxChanged);
2014-08-20 16:26:57 +02:00
uDom('#parseCosmeticFilters').on('change', cosmeticSwitchChanged);
uDom('#buttonApply').on('click', buttonApplyHandler);
uDom('#buttonUpdate').on('click', buttonUpdateHandler);
2014-09-09 01:45:22 +02:00
uDom('#buttonPurgeAll').on('click', buttonPurgeAllHandler);
2014-07-25 22:12:20 +02:00
uDom('#lists').on('change', '.listDetails > input', onListCheckboxChanged);
uDom('#lists').on('click', '.listDetails > a:nth-of-type(1)', onListLinkClicked);
uDom('#lists').on('click', 'span.purge', onPurgeClicked);
2014-07-25 22:12:20 +02:00
uDom('#externalLists').on('input', externalListsChangeHandler);
uDom('#externalListsApply').on('click', externalListsApplyHandler);
2014-06-24 00:42:43 +02:00
renderBlacklists();
2014-07-25 22:12:20 +02:00
renderExternalLists();
2014-06-24 00:42:43 +02:00
});
/******************************************************************************/
})();