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

591 lines
18 KiB
JavaScript
Raw Normal View History

2014-06-24 00:42:43 +02:00
/*******************************************************************************
2015-03-07 19:20:18 +01:00
µBlock - a browser extension to block requests.
2014-06-24 00:42:43 +02:00
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-06-24 00:42:43 +02:00
/******************************************************************************/
var onMessage = function(msg) {
switch ( msg.what ) {
2015-04-17 16:20:45 +02:00
case 'allFilterListsReloaded':
renderFilterLists();
break;
case 'forceUpdateAssetsProgress':
renderBusyOverlay(true, msg.progress);
if ( msg.done ) {
messager.send({ what: 'reloadAllFilters' });
}
break;
default:
break;
2014-06-24 00:42:43 +02:00
}
};
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
2015-03-11 04:46:18 +01:00
var renderFilterLists = function() {
2015-03-08 22:54:08 +01:00
var listGroupTemplate = uDom('#templates .groupEntry');
var listEntryTemplate = uDom('#templates .listEntry');
var listStatsTemplate = vAPI.i18n('3pListsOfBlockedHostsPerListStats');
var renderElapsedTimeToString = vAPI.i18n.renderElapsedTimeToString;
2015-03-11 04:46:18 +01:00
var lastUpdateString = vAPI.i18n('3pLastUpdate');
2015-03-08 22:54:08 +01:00
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
};
2015-03-08 22:54:08 +01:00
var liFromListEntry = function(listKey) {
var entry = listDetails.available[listKey];
var li = listEntryTemplate.clone();
if ( entry.off !== true ) {
li.descendants('input').attr('checked', '');
2014-06-24 00:42:43 +02:00
}
var elem = li.descendants('a:nth-of-type(1)');
2015-05-28 22:02:00 +02:00
elem.attr('href', 'asset-viewer.html?url=' + encodeURI(listKey));
elem.attr('type', 'text/html');
elem.attr('data-listkey', listKey);
2015-03-08 22:54:08 +01:00
elem.text(listNameFromListKey(listKey) + '\u200E');
2015-06-10 17:30:57 +02:00
if ( entry.instructionURL ) {
elem = li.descendants('a:nth-of-type(2)');
2015-06-10 17:30:57 +02:00
elem.attr('href', entry.instructionURL);
elem.css('display', '');
}
if ( entry.supportName ) {
elem = li.descendants('a:nth-of-type(3)');
elem.attr('href', entry.supportURL);
elem.text('(' + entry.supportName + ')');
2015-03-08 22:54:08 +01:00
elem.css('display', '');
2014-09-09 16:53:47 +02:00
}
2015-03-08 22:54:08 +01:00
elem = li.descendants('span:nth-of-type(1)');
var text = listStatsTemplate
2015-03-08 22:54:08 +01:00
.replace('{{used}}', renderNumber(!entry.off && !isNaN(+entry.entryUsedCount) ? entry.entryUsedCount : 0))
.replace('{{total}}', !isNaN(+entry.entryCount) ? renderNumber(entry.entryCount) : '?');
elem.text(text);
2015-04-17 16:20:45 +02:00
// https://github.com/gorhill/uBlock/issues/78
// Badge for non-secure connection
var remoteURL = listKey;
if ( remoteURL.lastIndexOf('http:', 0) !== 0 ) {
remoteURL = entry.homeURL || '';
}
if ( remoteURL.lastIndexOf('http:', 0) === 0 ) {
li.descendants('span.status.unsecure').css('display', '');
}
2015-04-07 03:26:05 +02:00
// https://github.com/chrisaljoudi/uBlock/issues/104
2015-03-08 22:54:08 +01:00
var asset = listDetails.cache[listKey] || {};
2015-04-17 16:20:45 +02:00
// Badge for update status
2015-03-08 22:54:08 +01:00
if ( entry.off !== true ) {
if ( asset.repoObsolete ) {
li.descendants('span.status.new').css('display', '');
needUpdate = true;
} else if ( asset.cacheObsolete ) {
li.descendants('span.status.obsolete').css('display', '');
needUpdate = true;
} else if ( entry.external && !asset.cached ) {
li.descendants('span.status.obsolete').css('display', '');
2014-09-09 16:53:47 +02:00
needUpdate = true;
}
}
2015-03-08 22:54:08 +01:00
2014-09-09 16:53:47 +02:00
// In cache
if ( asset.cached ) {
2015-03-10 00:10:04 +01:00
elem = li.descendants('span.status.purge');
elem.css('display', '');
2015-03-11 04:46:18 +01:00
elem.attr('title', lastUpdateString.replace('{{ago}}', renderElapsedTimeToString(asset.lastModified)));
2014-09-09 16:53:47 +02:00
hasCachedContent = true;
}
2015-03-08 22:54:08 +01:00
return li;
2014-09-09 16:53:47 +02:00
};
2015-03-23 15:19:43 +01:00
var listEntryCountFromGroup = function(listKeys) {
if ( Array.isArray(listKeys) === false ) {
return '';
}
var count = 0;
var i = listKeys.length;
while ( i-- ) {
if ( listDetails.available[listKeys[i]].off !== true ) {
count += 1;
}
}
return count === 0 ? '' : '(' + count.toLocaleString() + ')';
};
2015-03-08 22:54:08 +01:00
var liFromListGroup = function(groupKey, listKeys) {
var liGroup = listGroupTemplate.clone();
2015-03-23 15:19:43 +01:00
var groupName = vAPI.i18n('3pGroup' + groupKey.charAt(0).toUpperCase() + groupKey.slice(1));
if ( groupName !== '' ) {
liGroup.descendants('span.geName').text(groupName);
liGroup.descendants('span.geCount').text(listEntryCountFromGroup(listKeys));
}
2015-03-08 22:54:08 +01:00
var ulGroup = liGroup.descendants('ul');
2014-07-25 22:12:20 +02:00
if ( !listKeys ) {
2015-03-08 22:54:08 +01:00
return liGroup;
2014-07-25 22:12:20 +02:00
}
listKeys.sort(function(a, b) {
2015-03-23 15:19:43 +01:00
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++ ) {
2015-03-08 22:54:08 +01:00
ulGroup.append(liFromListEntry(listKeys[i]));
}
2015-03-08 22:54:08 +01:00
return liGroup;
};
// 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
2015-03-14 19:12:05 +01:00
var ulLists = uDom('#lists').empty(), liGroup;
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];
2015-03-14 19:12:05 +01:00
liGroup = liFromListGroup(groupKey, groups[groupKey]);
liGroup.toggleClass(
'collapsed',
vAPI.localStorage.getItem('collapseGroup' + (i + 1)) === 'y'
);
ulLists.append(liGroup);
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];
2015-03-08 22:54:08 +01:00
ulLists.append(liFromListGroup(groupKey, groups[groupKey]));
2014-07-25 22:12:20 +02:00
}
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);
2015-03-11 04:46:18 +01:00
renderWidgets();
renderBusyOverlay(details.manualUpdate, details.manualUpdateProgress);
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
/******************************************************************************/
2015-03-11 04:46:18 +01:00
// Progress must be normalized to [0, 1], or can be undefined.
var renderBusyOverlay = function(state, progress) {
progress = progress || {};
var showProgress = typeof progress.value === 'number';
if ( showProgress ) {
uDom('#busyOverlay > div:nth-of-type(2) > div:first-child').css(
'width',
(progress.value * 100).toFixed(1) + '%'
);
var text = progress.text || '';
if ( text !== '' ) {
uDom('#busyOverlay > div:nth-of-type(2) > div:last-child').text(text);
}
}
uDom('#busyOverlay > div:nth-of-type(2)').css('display', showProgress ? '' : 'none');
uDom('body').toggleClass('busy', !!state);
};
/******************************************************************************/
// This is to give a visual hint that the selection of blacklists has changed.
var renderWidgets = function() {
uDom('#buttonApply').toggleClass('disabled', !listsSelectionChanged());
uDom('#buttonUpdate').toggleClass('disabled', !listsContentChanged());
uDom('#buttonPurgeAll').toggleClass('disabled', !hasCachedContent);
};
/******************************************************************************/
// 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
}
2015-03-11 04:46:18 +01:00
2014-07-26 22:10:20 +02:00
if ( cacheWasPurged ) {
return true;
}
2015-03-11 04:46:18 +01:00
2014-07-25 22:12:20 +02:00
var availableLists = listDetails.available;
var currentLists = listDetails.current;
var location, availableOff, currentOff;
2015-03-11 04:46:18 +01:00
2014-07-25 22:12:20 +02:00
// 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;
}
}
2015-03-11 04:46:18 +01:00
2014-07-25 22:12:20 +02:00
// 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;
}
}
2015-03-11 04:46:18 +01:00
2014-07-25 22:12:20 +02:00
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-07-25 22:12:20 +02:00
var onListCheckboxChanged = function() {
2015-05-28 22:02:00 +02:00
var href = uDom(this).parent().descendants('a').first().attr('data-listkey');
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;
2015-03-11 04:46:18 +01:00
renderWidgets();
2014-07-09 22:03:25 +02:00
};
2014-06-24 00:42:43 +02:00
/******************************************************************************/
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();
2015-05-28 22:02:00 +02:00
var href = li.descendants('a').first().attr('data-listkey');
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;
2015-03-11 04:46:18 +01:00
renderWidgets();
}
2014-07-26 22:10:20 +02:00
};
/******************************************************************************/
2015-03-11 04:46:18 +01:00
var selectFilterLists = function(callback) {
// Cosmetic filtering switch
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
});
2015-03-11 04:46:18 +01:00
// Filter lists
2014-06-24 00:42:43 +02:00
var switches = [];
2015-03-12 12:33:20 +01:00
var lis = uDom('#lists .listEntry'), li;
2014-11-07 16:07:26 +01:00
var i = lis.length;
2014-06-24 00:42:43 +02:00
while ( i-- ) {
2015-03-12 12:33:20 +01:00
li = lis.at(i);
2014-06-24 00:42:43 +02:00
switches.push({
2015-05-28 22:02:00 +02:00
location: li.descendants('a').attr('data-listkey'),
2015-03-12 12:33:20 +01:00
off: li.descendants('input').prop('checked') === false
2014-06-24 00:42:43 +02:00
});
}
2015-03-11 04:46:18 +01:00
messager.send({
2015-03-11 04:46:18 +01:00
what: 'selectFilterLists',
switches: switches
}, callback);
};
/******************************************************************************/
var buttonApplyHandler = function() {
uDom('#buttonApply').removeClass('enabled');
2015-03-11 04:46:18 +01:00
renderBusyOverlay(true);
var onSelectionDone = function() {
2015-05-02 01:07:13 +02:00
messager.send({ what: 'reloadAllFilters' });
2015-03-11 04:46:18 +01:00
};
selectFilterLists(onSelectionDone);
cacheWasPurged = false;
};
/******************************************************************************/
var buttonUpdateHandler = function() {
uDom('#buttonUpdate').removeClass('enabled');
2014-08-20 07:18:53 +02:00
if ( needUpdate ) {
2015-03-11 04:46:18 +01:00
renderBusyOverlay(true);
var onSelectionDone = function() {
messager.send({ what: 'forceUpdateAssets' });
};
selectFilterLists(onSelectionDone);
cacheWasPurged = false;
2014-08-20 07:18:53 +02:00
}
};
/******************************************************************************/
2014-09-09 01:45:22 +02:00
var buttonPurgeAllHandler = function() {
uDom('#buttonPurgeAll').removeClass('enabled');
renderBusyOverlay(true);
2014-09-09 01:45:22 +02:00
var onCompleted = function() {
2015-03-11 04:46:18 +01:00
cacheWasPurged = true;
renderFilterLists();
2014-09-09 01:45:22 +02:00
};
2015-03-11 04:46:18 +01:00
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;
2015-03-11 04:46:18 +01:00
renderWidgets();
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() {
2015-08-11 21:29:14 +02:00
externalLists = uDom.nodeFromId('externalLists').value;
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
});
2015-03-11 04:46:18 +01:00
renderFilterLists();
2014-07-25 22:12:20 +02:00
uDom('#externalListsApply').prop('disabled', true);
2014-07-09 22:03:25 +02:00
};
2014-06-24 00:42:43 +02:00
/******************************************************************************/
2015-03-14 19:12:05 +01:00
var groupEntryClickHandler = function() {
var li = uDom(this).ancestors('.groupEntry');
li.toggleClass('collapsed');
var key = 'collapseGroup' + li.nthOfType();
if ( li.hasClass('collapsed') ) {
vAPI.localStorage.setItem(key, 'y');
} else {
vAPI.localStorage.removeItem(key);
}
};
/******************************************************************************/
2015-08-11 21:29:14 +02:00
var getCloudData = function() {
var bin = {
parseCosmeticFilters: uDom.nodeFromId('parseCosmeticFilters').checked,
selectedLists: [],
externalLists: externalLists
};
2014-06-24 00:42:43 +02:00
2015-08-11 21:29:14 +02:00
var lis = uDom('#lists .listEntry'), li;
var i = lis.length;
while ( i-- ) {
li = lis.at(i);
if ( li.descendants('input').prop('checked') ) {
bin.selectedLists.push(li.descendants('a').attr('data-listkey'));
}
}
return bin;
};
var setCloudData = function(data) {
if ( typeof data !== 'object' || data === null ) {
return;
}
var checked = data.parseCosmeticFilters === true;
uDom.nodeFromId('parseCosmeticFilters').checked = checked;
listDetails.cosmetic = checked;
var lis = uDom('#lists .listEntry'), li, input, listKey;
var i = lis.length;
while ( i-- ) {
li = lis.at(i);
input = li.descendants('input');
listKey = li.descendants('a').attr('data-listkey');
checked = data.selectedLists.indexOf(listKey) !== -1;
input.prop('checked', checked);
listDetails.available[listKey].off = !checked;
}
uDom.nodeFromId('externalLists').value = data.externalLists || '';
renderWidgets();
externalListsChangeHandler();
};
self.cloud.onPush = getCloudData;
self.cloud.onPull = setCloudData;
/******************************************************************************/
uDom('#autoUpdate').on('change', autoUpdateCheckboxChanged);
uDom('#parseCosmeticFilters').on('change', cosmeticSwitchChanged);
uDom('#buttonApply').on('click', buttonApplyHandler);
uDom('#buttonUpdate').on('click', buttonUpdateHandler);
uDom('#buttonPurgeAll').on('click', buttonPurgeAllHandler);
uDom('#lists').on('change', '.listEntry > input', onListCheckboxChanged);
uDom('#lists').on('click', 'span.purge', onPurgeClicked);
uDom('#externalLists').on('input', externalListsChangeHandler);
uDom('#externalListsApply').on('click', externalListsApplyHandler);
uDom('#lists').on('click', '.groupEntry > span', groupEntryClickHandler);
renderFilterLists();
renderExternalLists();
2014-06-24 00:42:43 +02:00
/******************************************************************************/
})();