2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-10-25 01:02:15 +02:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
/* global uDom */
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
'use strict';
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
(( ) => {
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const lastUpdateTemplateString = vAPI.i18n('3pLastUpdate');
|
|
|
|
const reValidExternalList = /[a-z-]+:\/\/\S*\/\S+/;
|
|
|
|
|
|
|
|
let listDetails = {};
|
|
|
|
let filteringSettingsHash = '';
|
|
|
|
let hideUnusedSet = new Set();
|
2014-09-09 01:45:22 +02:00
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-19 14:31:38 +02:00
|
|
|
const messaging = vAPI.messaging;
|
|
|
|
|
|
|
|
vAPI.broadcastListener.add(msg => {
|
2014-06-24 00:42:43 +02:00
|
|
|
switch ( msg.what ) {
|
2017-01-18 19:17:47 +01:00
|
|
|
case 'assetUpdated':
|
|
|
|
updateAssetStatus(msg);
|
|
|
|
break;
|
2017-01-23 15:35:05 +01:00
|
|
|
case 'assetsUpdated':
|
|
|
|
document.body.classList.remove('updating');
|
2017-04-26 18:21:01 +02:00
|
|
|
renderWidgets();
|
2017-01-23 15:35:05 +01:00
|
|
|
break;
|
2016-09-24 20:36:08 +02:00
|
|
|
case 'staticFilteringDataChanged':
|
2015-04-17 16:20:45 +02:00
|
|
|
renderFilterLists();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2019-09-19 14:31:38 +02:00
|
|
|
});
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const renderNumber = function(value) {
|
2014-07-16 02:32:33 +02:00
|
|
|
return value.toLocaleString();
|
2014-07-09 22:03:25 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const renderFilterLists = function(soft) {
|
|
|
|
const listGroupTemplate = uDom('#templates .groupEntry');
|
|
|
|
const listEntryTemplate = uDom('#templates .listEntry');
|
|
|
|
const listStatsTemplate = vAPI.i18n('3pListsOfBlockedHostsPerListStats');
|
|
|
|
const renderElapsedTimeToString = vAPI.i18n.renderElapsedTimeToString;
|
|
|
|
const groupNames = new Map([ [ 'user', '' ] ]);
|
2015-03-08 22:54:08 +01:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// Assemble a pretty list name if possible
|
2019-05-19 21:35:00 +02:00
|
|
|
const listNameFromListKey = function(listKey) {
|
|
|
|
const list = listDetails.current[listKey] || listDetails.available[listKey];
|
|
|
|
const listTitle = list ? list.title : '';
|
2017-01-20 14:40:19 +01:00
|
|
|
if ( listTitle === '' ) { return listKey; }
|
2014-09-26 02:21:21 +02:00
|
|
|
return listTitle;
|
2014-08-23 18:08:02 +02:00
|
|
|
};
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const liFromListEntry = function(listKey, li, hideUnused) {
|
|
|
|
const entry = listDetails.available[listKey];
|
2017-01-20 14:40:19 +01:00
|
|
|
if ( !li ) {
|
|
|
|
li = listEntryTemplate.clone().nodeAt(0);
|
2015-06-10 17:30:57 +02:00
|
|
|
}
|
2019-05-19 21:35:00 +02:00
|
|
|
const on = entry.off !== true;
|
|
|
|
let elem;
|
2017-01-20 14:40:19 +01:00
|
|
|
if ( li.getAttribute('data-listkey') !== listKey ) {
|
|
|
|
li.setAttribute('data-listkey', listKey);
|
|
|
|
elem = li.querySelector('input[type="checkbox"]');
|
2018-04-09 15:01:39 +02:00
|
|
|
elem.checked = on;
|
2017-01-20 14:40:19 +01:00
|
|
|
elem = li.querySelector('a:nth-of-type(1)');
|
|
|
|
elem.setAttribute('href', 'asset-viewer.html?url=' + encodeURI(listKey));
|
|
|
|
elem.setAttribute('type', 'text/html');
|
2017-01-24 19:53:04 +01:00
|
|
|
elem.textContent = listNameFromListKey(listKey);
|
2017-01-24 14:23:52 +01:00
|
|
|
li.classList.remove('toRemove');
|
2017-01-20 14:40:19 +01:00
|
|
|
if ( entry.supportName ) {
|
2017-01-22 22:05:16 +01:00
|
|
|
li.classList.add('support');
|
|
|
|
elem = li.querySelector('a.support');
|
2017-01-20 14:40:19 +01:00
|
|
|
elem.setAttribute('href', entry.supportURL);
|
2017-01-22 22:05:16 +01:00
|
|
|
elem.setAttribute('title', entry.supportName);
|
|
|
|
} else {
|
|
|
|
li.classList.remove('support');
|
|
|
|
}
|
|
|
|
if ( entry.external ) {
|
|
|
|
li.classList.add('external');
|
|
|
|
} else {
|
|
|
|
li.classList.remove('external');
|
|
|
|
}
|
|
|
|
if ( entry.instructionURL ) {
|
|
|
|
li.classList.add('mustread');
|
|
|
|
elem = li.querySelector('a.mustread');
|
|
|
|
elem.setAttribute('href', entry.instructionURL);
|
2017-01-20 14:40:19 +01:00
|
|
|
} else {
|
2017-01-22 22:05:16 +01:00
|
|
|
li.classList.remove('mustread');
|
2017-01-20 14:40:19 +01:00
|
|
|
}
|
2018-04-09 15:01:39 +02:00
|
|
|
li.classList.toggle('unused', hideUnused && !on);
|
2014-09-09 16:53:47 +02:00
|
|
|
}
|
2017-01-22 22:05:16 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1429
|
|
|
|
if ( !soft ) {
|
2018-04-09 15:01:39 +02:00
|
|
|
li.querySelector('input[type="checkbox"]').checked = on;
|
2017-01-22 22:05:16 +01:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
elem = li.querySelector('span.counts');
|
2019-05-19 21:35:00 +02:00
|
|
|
let text = '';
|
2017-01-22 22:05:16 +01:00
|
|
|
if ( !isNaN(+entry.entryUsedCount) && !isNaN(+entry.entryCount) ) {
|
|
|
|
text = listStatsTemplate
|
2018-04-09 15:01:39 +02:00
|
|
|
.replace('{{used}}', renderNumber(on ? entry.entryUsedCount : 0))
|
2017-01-22 22:05:16 +01:00
|
|
|
.replace('{{total}}', renderNumber(entry.entryCount));
|
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
elem.textContent = text;
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/104
|
2019-05-19 21:35:00 +02:00
|
|
|
const asset = listDetails.cache[listKey] || {};
|
|
|
|
const remoteURL = asset.remoteURL;
|
2017-01-18 19:17:47 +01:00
|
|
|
li.classList.toggle(
|
|
|
|
'unsecure',
|
|
|
|
typeof remoteURL === 'string' && remoteURL.lastIndexOf('http:', 0) === 0
|
|
|
|
);
|
2017-01-22 22:05:16 +01:00
|
|
|
li.classList.toggle('failed', asset.error !== undefined);
|
|
|
|
li.classList.toggle('obsolete', asset.obsolete === true);
|
2017-04-26 18:21:01 +02:00
|
|
|
if ( asset.cached === true ) {
|
|
|
|
li.classList.add('cached');
|
2017-01-22 22:05:16 +01:00
|
|
|
li.querySelector('.status.cache').setAttribute(
|
2017-01-18 19:17:47 +01:00
|
|
|
'title',
|
2017-04-26 18:21:01 +02:00
|
|
|
lastUpdateTemplateString.replace(
|
|
|
|
'{{ago}}',
|
|
|
|
renderElapsedTimeToString(asset.writeTime)
|
|
|
|
)
|
2017-01-18 19:17:47 +01:00
|
|
|
);
|
2017-04-26 18:21:01 +02:00
|
|
|
} else {
|
|
|
|
li.classList.remove('cached');
|
2014-09-09 16:53:47 +02:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
li.classList.remove('discard');
|
2015-03-08 22:54:08 +01:00
|
|
|
return li;
|
2014-09-09 16:53:47 +02:00
|
|
|
};
|
2014-07-16 02:32:33 +02:00
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const listEntryCountFromGroup = function(listKeys) {
|
2017-01-20 14:40:19 +01:00
|
|
|
if ( Array.isArray(listKeys) === false ) { return ''; }
|
2019-05-19 21:35:00 +02:00
|
|
|
let count = 0,
|
2018-04-09 15:01:39 +02:00
|
|
|
total = 0;
|
2019-05-19 21:35:00 +02:00
|
|
|
for ( const listKey of listKeys ) {
|
|
|
|
if ( listDetails.available[listKey].off !== true ) {
|
2015-03-23 15:19:43 +01:00
|
|
|
count += 1;
|
|
|
|
}
|
2018-04-09 15:01:39 +02:00
|
|
|
total += 1;
|
2015-03-23 15:19:43 +01:00
|
|
|
}
|
2018-04-09 15:01:39 +02:00
|
|
|
return total !== 0 ?
|
2019-05-19 21:35:00 +02:00
|
|
|
`(${count.toLocaleString()}/${total.toLocaleString()})` :
|
2018-04-09 15:01:39 +02:00
|
|
|
'';
|
2015-03-23 15:19:43 +01:00
|
|
|
};
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const liFromListGroup = function(groupKey, listKeys) {
|
|
|
|
let liGroup = document.querySelector(`#lists > .groupEntry[data-groupkey="${groupKey}"]`);
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( liGroup === null ) {
|
|
|
|
liGroup = listGroupTemplate.clone().nodeAt(0);
|
2018-08-31 16:50:19 +02:00
|
|
|
let groupName = groupNames.get(groupKey);
|
2017-09-10 19:02:04 +02:00
|
|
|
if ( groupName === undefined ) {
|
|
|
|
groupName = vAPI.i18n('3pGroup' + groupKey.charAt(0).toUpperCase() + groupKey.slice(1));
|
|
|
|
groupNames.set(groupKey, groupName);
|
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( groupName !== '' ) {
|
|
|
|
liGroup.querySelector('.geName').textContent = groupName;
|
|
|
|
}
|
2015-03-23 15:19:43 +01:00
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( liGroup.querySelector('.geName:empty') === null ) {
|
|
|
|
liGroup.querySelector('.geCount').textContent = listEntryCountFromGroup(listKeys);
|
2014-07-25 22:12:20 +02:00
|
|
|
}
|
2018-08-31 16:50:19 +02:00
|
|
|
let hideUnused = mustHideUnusedLists(groupKey);
|
2018-04-09 15:01:39 +02:00
|
|
|
liGroup.classList.toggle('hideUnused', hideUnused);
|
2018-08-31 16:50:19 +02:00
|
|
|
let ulGroup = liGroup.querySelector('.listEntries');
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( !listKeys ) { 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
|
|
|
});
|
2018-08-31 16:50:19 +02:00
|
|
|
for ( let i = 0; i < listKeys.length; i++ ) {
|
|
|
|
let liEntry = liFromListEntry(
|
2018-04-09 15:01:39 +02:00
|
|
|
listKeys[i],
|
|
|
|
ulGroup.children[i],
|
|
|
|
hideUnused
|
|
|
|
);
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( liEntry.parentElement === null ) {
|
|
|
|
ulGroup.appendChild(liEntry);
|
|
|
|
}
|
2014-07-16 02:32:33 +02:00
|
|
|
}
|
2015-03-08 22:54:08 +01:00
|
|
|
return liGroup;
|
2014-07-16 02:32:33 +02:00
|
|
|
};
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const groupsFromLists = function(lists) {
|
2018-08-31 16:50:19 +02:00
|
|
|
let groups = new Map();
|
|
|
|
let listKeys = Object.keys(lists);
|
|
|
|
for ( let listKey of listKeys ) {
|
|
|
|
let list = lists[listKey];
|
|
|
|
let groupKey = list.group || 'nogroup';
|
|
|
|
if ( groupKey === 'social' ) {
|
|
|
|
groupKey = 'annoyances';
|
|
|
|
}
|
|
|
|
let memberKeys = groups.get(groupKey);
|
|
|
|
if ( memberKeys === undefined ) {
|
|
|
|
groups.set(groupKey, (memberKeys = []));
|
2014-07-16 02:32:33 +02:00
|
|
|
}
|
2018-08-31 16:50:19 +02:00
|
|
|
memberKeys.push(listKey);
|
2014-07-16 02:32:33 +02:00
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
};
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const 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;
|
2017-01-18 19:17:47 +01:00
|
|
|
|
2018-04-09 21:45:25 +02:00
|
|
|
// "My filters" will now sit in its own group. The following code
|
|
|
|
// ensures smooth transition.
|
|
|
|
listDetails.available['user-filters'].group = 'user';
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
// Incremental rendering: this will allow us to easily discard unused
|
|
|
|
// DOM list entries.
|
2018-04-09 15:01:39 +02:00
|
|
|
uDom('#lists .listEntries .listEntry[data-listkey]').addClass('discard');
|
|
|
|
|
|
|
|
// Remove import widget while we recreate list of lists.
|
2019-05-19 21:35:00 +02:00
|
|
|
const importWidget = uDom('.listEntry.toImport').detach();
|
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
|
2019-05-19 21:35:00 +02:00
|
|
|
const ulLists = document.querySelector('#lists');
|
|
|
|
const groups = groupsFromLists(details.available);
|
|
|
|
const groupKeys = [
|
|
|
|
'user',
|
|
|
|
'default',
|
|
|
|
'ads',
|
|
|
|
'privacy',
|
|
|
|
'malware',
|
|
|
|
'annoyances',
|
|
|
|
'multipurpose',
|
|
|
|
'regions',
|
|
|
|
'custom'
|
|
|
|
];
|
2018-04-09 15:01:39 +02:00
|
|
|
document.body.classList.toggle('hideUnused', mustHideUnusedLists('*'));
|
2018-08-31 16:50:19 +02:00
|
|
|
for ( let i = 0; i < groupKeys.length; i++ ) {
|
|
|
|
let groupKey = groupKeys[i];
|
|
|
|
let liGroup = liFromListGroup(groupKey, groups.get(groupKey));
|
2017-01-18 19:17:47 +01:00
|
|
|
liGroup.setAttribute('data-groupkey', groupKey);
|
|
|
|
liGroup.classList.toggle(
|
2015-03-14 19:12:05 +01:00
|
|
|
'collapsed',
|
|
|
|
vAPI.localStorage.getItem('collapseGroup' + (i + 1)) === 'y'
|
|
|
|
);
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( liGroup.parentElement === null ) {
|
|
|
|
ulLists.appendChild(liGroup);
|
|
|
|
}
|
2018-08-31 16:50:19 +02:00
|
|
|
groups.delete(groupKey);
|
2014-07-25 22:12:20 +02:00
|
|
|
}
|
|
|
|
// For all groups not covered above (if any left)
|
2019-05-19 21:35:00 +02:00
|
|
|
for ( const groupKey of Object.keys(groups) ) {
|
2018-08-31 16:50:19 +02:00
|
|
|
ulLists.appendChild(liFromListGroup(groupKey, groupKey));
|
2014-07-25 22:12:20 +02:00
|
|
|
}
|
2014-07-16 02:32:33 +02:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
uDom('#lists .listEntries .listEntry.discard').remove();
|
2018-04-09 15:01:39 +02:00
|
|
|
|
|
|
|
// Re-insert import widget.
|
|
|
|
uDom('[data-groupkey="custom"] .listEntries').append(importWidget);
|
|
|
|
|
|
|
|
uDom.nodeFromId('autoUpdate').checked = listDetails.autoUpdate === true;
|
|
|
|
uDom.nodeFromId('listsOfBlockedHostsPrompt').textContent =
|
2014-10-17 21:44:19 +02:00
|
|
|
vAPI.i18n('3pListsOfBlockedHostsPrompt')
|
2018-04-09 15:01:39 +02:00
|
|
|
.replace(
|
|
|
|
'{{netFilterCount}}',
|
|
|
|
renderNumber(details.netFilterCount)
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
'{{cosmeticFilterCount}}',
|
|
|
|
renderNumber(details.cosmeticFilterCount)
|
|
|
|
);
|
|
|
|
uDom.nodeFromId('parseCosmeticFilters').checked =
|
|
|
|
listDetails.parseCosmeticFilters === true;
|
|
|
|
uDom.nodeFromId('ignoreGenericCosmeticFilters').checked =
|
|
|
|
listDetails.ignoreGenericCosmeticFilters === true;
|
2017-01-18 19:17:47 +01:00
|
|
|
|
2017-01-20 14:40:19 +01:00
|
|
|
// Compute a hash of the settings so that we can keep track of changes
|
|
|
|
// affecting the loading of filter lists.
|
2017-01-22 22:05:16 +01:00
|
|
|
if ( !soft ) {
|
2017-01-20 14:40:19 +01:00
|
|
|
filteringSettingsHash = hashFromCurrentFromSettings();
|
2017-01-18 19:17:47 +01:00
|
|
|
}
|
2019-05-20 00:31:12 +02:00
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2394
|
|
|
|
document.body.classList.toggle('updating', listDetails.isUpdating);
|
|
|
|
|
2015-03-11 04:46:18 +01:00
|
|
|
renderWidgets();
|
2014-07-25 22:12:20 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
messaging.send('dashboard', {
|
|
|
|
what: 'getLists',
|
|
|
|
}).then(details => {
|
|
|
|
onListsReceived(details);
|
|
|
|
});
|
2014-07-09 22:03:25 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const renderWidgets = function() {
|
2018-04-09 15:01:39 +02:00
|
|
|
uDom('#buttonApply').toggleClass(
|
|
|
|
'disabled',
|
|
|
|
filteringSettingsHash === hashFromCurrentFromSettings()
|
|
|
|
);
|
2017-05-26 14:31:19 +02:00
|
|
|
uDom('#buttonPurgeAll').toggleClass(
|
|
|
|
'disabled',
|
|
|
|
document.querySelector('#lists .listEntry.cached:not(.obsolete)') === null
|
|
|
|
);
|
2018-04-09 15:01:39 +02:00
|
|
|
uDom('#buttonUpdate').toggleClass(
|
|
|
|
'disabled',
|
2019-09-18 12:52:10 +02:00
|
|
|
document.querySelector('body:not(.updating) #lists .listEntry.obsolete:not(.toRemove) > input[type="checkbox"]:checked') === null
|
2018-04-09 15:01:39 +02:00
|
|
|
);
|
2015-03-11 04:46:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const updateAssetStatus = function(details) {
|
|
|
|
const li = document.querySelector(
|
2018-10-25 01:02:15 +02:00
|
|
|
'#lists .listEntry[data-listkey="' + details.key + '"]'
|
|
|
|
);
|
2017-01-22 22:05:16 +01:00
|
|
|
if ( li === null ) { return; }
|
|
|
|
li.classList.toggle('failed', !!details.failed);
|
|
|
|
li.classList.toggle('obsolete', !details.cached);
|
|
|
|
li.classList.toggle('cached', !!details.cached);
|
|
|
|
if ( details.cached ) {
|
|
|
|
li.querySelector('.status.cache').setAttribute(
|
|
|
|
'title',
|
|
|
|
lastUpdateTemplateString.replace(
|
|
|
|
'{{ago}}',
|
|
|
|
vAPI.i18n.renderElapsedTimeToString(Date.now())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-01-18 19:17:47 +01:00
|
|
|
renderWidgets();
|
2015-03-11 04:46:18 +01:00
|
|
|
};
|
|
|
|
|
2017-01-20 14:40:19 +01:00
|
|
|
/*******************************************************************************
|
2015-03-11 04:46:18 +01:00
|
|
|
|
2017-01-20 14:40:19 +01:00
|
|
|
Compute a hash from all the settings affecting how filter lists are loaded
|
|
|
|
in memory.
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2017-01-20 14:40:19 +01:00
|
|
|
**/
|
2015-03-11 04:46:18 +01:00
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const hashFromCurrentFromSettings = function() {
|
|
|
|
const hash = [
|
2018-04-09 15:01:39 +02:00
|
|
|
uDom.nodeFromId('parseCosmeticFilters').checked,
|
|
|
|
uDom.nodeFromId('ignoreGenericCosmeticFilters').checked
|
2017-01-20 14:40:19 +01:00
|
|
|
];
|
2019-05-19 21:35:00 +02:00
|
|
|
const listHash = [];
|
|
|
|
const listEntries = document.querySelectorAll('#lists .listEntry[data-listkey]:not(.toRemove)');
|
|
|
|
for ( const liEntry of listEntries ) {
|
2017-01-20 14:40:19 +01:00
|
|
|
if ( liEntry.querySelector('input[type="checkbox"]:checked') !== null ) {
|
|
|
|
listHash.push(liEntry.getAttribute('data-listkey'));
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 23:16:37 +01:00
|
|
|
hash.push(
|
|
|
|
listHash.sort().join(),
|
2018-04-09 15:01:39 +02:00
|
|
|
uDom.nodeFromId('importLists').checked &&
|
|
|
|
reValidExternalList.test(uDom.nodeFromId('externalLists').value),
|
2017-01-23 23:16:37 +01:00
|
|
|
document.querySelector('#lists .listEntry.toRemove') !== null
|
|
|
|
);
|
2017-01-22 22:05:16 +01:00
|
|
|
return hash.join();
|
2014-08-20 02:41:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const onFilteringSettingsChanged = function() {
|
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
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const onRemoveExternalList = function(ev) {
|
|
|
|
const liEntry = uDom(this).ancestors('[data-listkey]');
|
|
|
|
const listKey = liEntry.attr('data-listkey');
|
2017-01-22 22:05:16 +01:00
|
|
|
if ( listKey ) {
|
|
|
|
liEntry.toggleClass('toRemove');
|
|
|
|
renderWidgets();
|
|
|
|
}
|
|
|
|
ev.preventDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const onPurgeClicked = function(ev) {
|
|
|
|
const button = uDom(ev.target);
|
|
|
|
const liEntry = button.ancestors('[data-listkey]');
|
|
|
|
const listKey = liEntry.attr('data-listkey');
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( !listKey ) { return; }
|
2015-10-14 20:16:43 +02:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
messaging.send('dashboard', {
|
|
|
|
what: 'purgeCache',
|
|
|
|
assetKey: listKey,
|
|
|
|
});
|
2015-10-14 20:16:43 +02:00
|
|
|
|
|
|
|
// If the cached version is purged, the installed version must be assumed
|
|
|
|
// to be obsolete.
|
2016-06-19 19:26:02 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1733
|
2017-01-22 22:05:16 +01:00
|
|
|
// An external filter list must not be marked as obsolete, they will
|
|
|
|
// always be fetched anyways if there is no cached copy.
|
|
|
|
liEntry.addClass('obsolete');
|
2017-01-18 19:17:47 +01:00
|
|
|
liEntry.removeClass('cached');
|
2015-10-14 20:16:43 +02:00
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( liEntry.descendants('input').first().prop('checked') ) {
|
2015-03-11 04:46:18 +01:00
|
|
|
renderWidgets();
|
2014-08-20 02:41:52 +02:00
|
|
|
}
|
2014-07-26 22:10:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const selectFilterLists = async function() {
|
2015-03-11 04:46:18 +01:00
|
|
|
// Cosmetic filtering switch
|
2016-08-13 22:42:58 +02:00
|
|
|
messaging.send('dashboard', {
|
|
|
|
what: 'userSettings',
|
|
|
|
name: 'parseAllABPHideFilters',
|
2019-09-17 21:15:01 +02:00
|
|
|
value: document.getElementById('parseCosmeticFilters').checked,
|
2016-08-13 22:42:58 +02:00
|
|
|
});
|
|
|
|
messaging.send('dashboard', {
|
|
|
|
what: 'userSettings',
|
|
|
|
name: 'ignoreGenericCosmeticFilters',
|
2019-09-17 21:15:01 +02:00
|
|
|
value: document.getElementById('ignoreGenericCosmeticFilters').checked,
|
2016-08-13 22:42:58 +02:00
|
|
|
});
|
2015-03-11 04:46:18 +01:00
|
|
|
|
2017-01-22 22:05:16 +01:00
|
|
|
// Filter lists to select
|
2019-05-19 21:35:00 +02:00
|
|
|
const toSelect = [];
|
|
|
|
let liEntries = document.querySelectorAll('#lists .listEntry[data-listkey]:not(.toRemove)');
|
|
|
|
for ( const liEntry of liEntries ) {
|
2017-01-20 14:40:19 +01:00
|
|
|
if ( liEntry.querySelector('input[type="checkbox"]:checked') !== null ) {
|
2017-01-22 22:05:16 +01:00
|
|
|
toSelect.push(liEntry.getAttribute('data-listkey'));
|
2017-01-18 19:17:47 +01:00
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2015-03-11 04:46:18 +01:00
|
|
|
|
2017-01-22 22:05:16 +01:00
|
|
|
// External filter lists to remove
|
2019-05-19 21:35:00 +02:00
|
|
|
const toRemove = [];
|
2017-01-22 22:05:16 +01:00
|
|
|
liEntries = document.querySelectorAll('#lists .listEntry.toRemove[data-listkey]');
|
2019-05-19 21:35:00 +02:00
|
|
|
for ( const liEntry of liEntries ) {
|
|
|
|
toRemove.push(liEntry.getAttribute('data-listkey'));
|
2017-01-22 22:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// External filter lists to import
|
2019-05-19 21:35:00 +02:00
|
|
|
const externalListsElem = document.getElementById('externalLists');
|
|
|
|
const toImport = externalListsElem.value.trim();
|
2017-01-22 22:05:16 +01:00
|
|
|
externalListsElem.value = '';
|
2018-04-09 15:01:39 +02:00
|
|
|
uDom.nodeFromId('importLists').checked = false;
|
2017-01-22 22:05:16 +01:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
await messaging.send('dashboard', {
|
|
|
|
what: 'applyFilterListSelection',
|
|
|
|
toSelect: toSelect,
|
|
|
|
toImport: toImport,
|
|
|
|
toRemove: toRemove,
|
|
|
|
});
|
|
|
|
|
2017-01-20 14:40:19 +01:00
|
|
|
filteringSettingsHash = hashFromCurrentFromSettings();
|
2014-08-20 02:41:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const buttonApplyHandler = async function() {
|
2015-03-12 22:25:51 +01:00
|
|
|
uDom('#buttonApply').removeClass('enabled');
|
2019-09-17 21:15:01 +02:00
|
|
|
await selectFilterLists();
|
2019-09-18 12:52:10 +02:00
|
|
|
renderWidgets();
|
2019-09-17 21:15:01 +02:00
|
|
|
messaging.send('dashboard', { what: 'reloadAllFilters' });
|
2014-08-20 02:41:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const buttonUpdateHandler = async function() {
|
|
|
|
await selectFilterLists();
|
|
|
|
document.body.classList.add('updating');
|
2017-01-20 14:40:19 +01:00
|
|
|
renderWidgets();
|
2019-09-17 21:15:01 +02:00
|
|
|
messaging.send('dashboard', { what: 'forceUpdateAssets' });
|
2014-08-20 02:41:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const buttonPurgeAllHandler = async function(hard) {
|
2015-03-12 22:25:51 +01:00
|
|
|
uDom('#buttonPurgeAll').removeClass('enabled');
|
2019-09-17 21:15:01 +02:00
|
|
|
await messaging.send('dashboard', {
|
|
|
|
what: 'purgeAllCaches',
|
|
|
|
hard,
|
|
|
|
});
|
|
|
|
renderFilterLists(true);
|
2014-09-09 01:45:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const autoUpdateCheckboxChanged = function() {
|
2019-09-17 21:15:01 +02:00
|
|
|
messaging.send('dashboard', {
|
|
|
|
what: 'userSettings',
|
|
|
|
name: 'autoUpdate',
|
|
|
|
value: this.checked,
|
|
|
|
});
|
2014-07-09 22:03:25 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-04-09 15:01:39 +02:00
|
|
|
// Collapsing of unused lists.
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const mustHideUnusedLists = function(which) {
|
|
|
|
const hideAll = hideUnusedSet.has('*');
|
2018-04-09 15:01:39 +02:00
|
|
|
if ( which === '*' ) { return hideAll; }
|
|
|
|
return hideUnusedSet.has(which) !== hideAll;
|
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const toggleHideUnusedLists = function(which) {
|
|
|
|
const doesHideAll = hideUnusedSet.has('*');
|
|
|
|
let groupSelector;
|
|
|
|
let mustHide;
|
2018-04-09 15:01:39 +02:00
|
|
|
if ( which === '*' ) {
|
|
|
|
mustHide = doesHideAll === false;
|
|
|
|
groupSelector = '';
|
|
|
|
hideUnusedSet.clear();
|
|
|
|
if ( mustHide ) {
|
|
|
|
hideUnusedSet.add(which);
|
|
|
|
}
|
|
|
|
document.body.classList.toggle('hideUnused', mustHide);
|
|
|
|
uDom('.groupEntry[data-groupkey]').toggleClass('hideUnused', mustHide);
|
2015-03-14 19:12:05 +01:00
|
|
|
} else {
|
2019-05-19 21:35:00 +02:00
|
|
|
const doesHide = hideUnusedSet.has(which);
|
2018-04-09 15:01:39 +02:00
|
|
|
if ( doesHide ) {
|
|
|
|
hideUnusedSet.delete(which);
|
|
|
|
} else {
|
|
|
|
hideUnusedSet.add(which);
|
|
|
|
}
|
|
|
|
mustHide = doesHide === doesHideAll;
|
|
|
|
groupSelector = '.groupEntry[data-groupkey="' + which + '"] ';
|
|
|
|
uDom(groupSelector).toggleClass('hideUnused', mustHide);
|
2015-03-14 19:12:05 +01:00
|
|
|
}
|
2018-04-09 15:01:39 +02:00
|
|
|
uDom(groupSelector + '.listEntry > input[type="checkbox"]:not(:checked)')
|
|
|
|
.ancestors('.listEntry[data-listkey]')
|
|
|
|
.toggleClass('unused', mustHide);
|
|
|
|
vAPI.localStorage.setItem(
|
|
|
|
'hideUnusedFilterLists',
|
|
|
|
JSON.stringify(Array.from(hideUnusedSet))
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const revealHiddenUsedLists = function() {
|
2018-04-09 15:01:39 +02:00
|
|
|
uDom('#lists .listEntry.unused > input[type="checkbox"]:checked')
|
|
|
|
.ancestors('.listEntry[data-listkey]')
|
|
|
|
.removeClass('unused');
|
2015-03-14 19:12:05 +01:00
|
|
|
};
|
|
|
|
|
2018-04-09 15:01:39 +02:00
|
|
|
uDom('#listsOfBlockedHostsPrompt').on('click', function() {
|
|
|
|
toggleHideUnusedLists('*');
|
|
|
|
});
|
|
|
|
|
|
|
|
uDom('#lists').on('click', '.groupEntry[data-groupkey] > .geDetails', function(ev) {
|
|
|
|
toggleHideUnusedLists(
|
|
|
|
uDom(ev.target)
|
|
|
|
.ancestors('.groupEntry[data-groupkey]')
|
|
|
|
.attr('data-groupkey')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
// Initialize from saved state.
|
|
|
|
{
|
|
|
|
let aa;
|
2018-04-09 15:01:39 +02:00
|
|
|
try {
|
2019-05-19 21:35:00 +02:00
|
|
|
const json = vAPI.localStorage.getItem('hideUnusedFilterLists');
|
2018-04-09 15:01:39 +02:00
|
|
|
if ( json !== null ) {
|
|
|
|
aa = JSON.parse(json);
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
}
|
|
|
|
if ( Array.isArray(aa) === false ) {
|
|
|
|
aa = [ '*' ];
|
|
|
|
}
|
|
|
|
hideUnusedSet = new Set(aa);
|
2019-05-19 21:35:00 +02:00
|
|
|
}
|
2018-04-09 15:01:39 +02:00
|
|
|
|
2015-03-14 19:12:05 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-04-09 15:01:39 +02:00
|
|
|
// Cloud-related.
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const toCloudData = function() {
|
|
|
|
const bin = {
|
2015-08-11 21:29:14 +02:00
|
|
|
parseCosmeticFilters: uDom.nodeFromId('parseCosmeticFilters').checked,
|
2016-08-13 22:42:58 +02:00
|
|
|
ignoreGenericCosmeticFilters: uDom.nodeFromId('ignoreGenericCosmeticFilters').checked,
|
2018-04-09 15:01:39 +02:00
|
|
|
selectedLists: []
|
2015-08-11 21:29:14 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const liEntries = document.querySelectorAll('#lists .listEntry');
|
|
|
|
for ( const liEntry of liEntries ) {
|
|
|
|
if ( liEntry.querySelector('input').checked ) {
|
|
|
|
bin.selectedLists.push(liEntry.getAttribute('data-listkey'));
|
2015-08-11 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bin;
|
|
|
|
};
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const fromCloudData = function(data, append) {
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( typeof data !== 'object' || data === null ) { return; }
|
2015-08-11 21:29:14 +02:00
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
let elem, checked;
|
2015-08-11 21:29:14 +02:00
|
|
|
|
2016-01-08 17:08:53 +01:00
|
|
|
elem = uDom.nodeFromId('parseCosmeticFilters');
|
2016-08-13 22:42:58 +02:00
|
|
|
checked = data.parseCosmeticFilters === true || append && elem.checked;
|
|
|
|
elem.checked = listDetails.parseCosmeticFilters = checked;
|
|
|
|
|
|
|
|
elem = uDom.nodeFromId('ignoreGenericCosmeticFilters');
|
|
|
|
checked = data.ignoreGenericCosmeticFilters === true || append && elem.checked;
|
|
|
|
elem.checked = listDetails.ignoreGenericCosmeticFilters = checked;
|
2016-01-08 17:08:53 +01:00
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
const selectedSet = new Set(data.selectedLists);
|
|
|
|
const listEntries = uDom('#lists .listEntry');
|
|
|
|
for ( let i = 0, n = listEntries.length; i < n; i++ ) {
|
|
|
|
const listEntry = listEntries.at(i);
|
|
|
|
const listKey = listEntry.attr('data-listkey');
|
|
|
|
const hasListKey = selectedSet.has(listKey);
|
2018-04-09 15:01:39 +02:00
|
|
|
selectedSet.delete(listKey);
|
2019-05-19 21:35:00 +02:00
|
|
|
const input = listEntry.descendants('input').first();
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( append && input.prop('checked') ) { continue; }
|
2018-04-09 15:01:39 +02:00
|
|
|
input.prop('checked', hasListKey);
|
2015-08-11 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2018-04-09 15:01:39 +02:00
|
|
|
// If there are URL-like list keys left in the selected set, import them.
|
2019-05-19 21:35:00 +02:00
|
|
|
for ( const listKey of selectedSet ) {
|
2018-04-09 15:01:39 +02:00
|
|
|
if ( reValidExternalList.test(listKey) === false ) {
|
|
|
|
selectedSet.delete(listKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( selectedSet.size !== 0 ) {
|
|
|
|
elem = uDom.nodeFromId('externalLists');
|
|
|
|
if ( append ) {
|
|
|
|
if ( elem.value.trim() !== '' ) { elem.value += '\n'; }
|
|
|
|
} else {
|
|
|
|
elem.value = '';
|
|
|
|
}
|
|
|
|
elem.value += Array.from(selectedSet).join('\n');
|
|
|
|
uDom.nodeFromId('importLists').checked = true;
|
|
|
|
}
|
2015-08-11 21:29:14 +02:00
|
|
|
|
2018-04-09 15:01:39 +02:00
|
|
|
revealHiddenUsedLists();
|
2015-08-11 21:29:14 +02:00
|
|
|
renderWidgets();
|
|
|
|
};
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
self.cloud.onPush = toCloudData;
|
|
|
|
self.cloud.onPull = fromCloudData;
|
2015-08-11 21:29:14 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-19 21:35:00 +02:00
|
|
|
self.hasUnsavedData = function() {
|
|
|
|
return hashFromCurrentFromSettings() !== filteringSettingsHash;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-08-11 21:29:14 +02:00
|
|
|
uDom('#autoUpdate').on('change', autoUpdateCheckboxChanged);
|
2017-01-20 14:40:19 +01:00
|
|
|
uDom('#parseCosmeticFilters').on('change', onFilteringSettingsChanged);
|
|
|
|
uDom('#ignoreGenericCosmeticFilters').on('change', onFilteringSettingsChanged);
|
2019-09-17 21:15:01 +02:00
|
|
|
uDom('#buttonApply').on('click', ( ) => { buttonApplyHandler(); });
|
|
|
|
uDom('#buttonUpdate').on('click', ( ) => { buttonUpdateHandler(); });
|
|
|
|
uDom('#buttonPurgeAll').on('click', ev => {
|
|
|
|
buttonPurgeAllHandler(ev.ctrlKey && ev.shiftKey);
|
|
|
|
});
|
2017-01-22 22:05:16 +01:00
|
|
|
uDom('#lists').on('change', '.listEntry > input', onFilteringSettingsChanged);
|
|
|
|
uDom('#lists').on('click', '.listEntry > a.remove', onRemoveExternalList);
|
|
|
|
uDom('#lists').on('click', 'span.cache', onPurgeClicked);
|
|
|
|
uDom('#externalLists').on('input', onFilteringSettingsChanged);
|
2015-08-11 21:29:14 +02:00
|
|
|
|
2018-04-09 15:01:39 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-01-22 22:05:16 +01:00
|
|
|
renderFilterLists();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|