2015-06-11 18:12:23 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-07-21 18:22:53 +02:00
|
|
|
Copyright (C) 2015-present Raymond Hill
|
2015-06-11 18:12:23 +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
|
|
|
|
*/
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
'use strict';
|
2015-06-11 18:12:23 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
µBlock.staticFilteringReverseLookup = (( ) => {
|
2015-06-11 18:12:23 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const workerTTL = 5 * 60 * 1000;
|
|
|
|
const pendingResponses = new Map();
|
|
|
|
|
|
|
|
let worker = null;
|
|
|
|
let workerTTLTimer;
|
|
|
|
let needLists = true;
|
|
|
|
let messageId = 1;
|
2015-06-11 18:12:23 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const onWorkerMessage = function(e) {
|
|
|
|
const msg = e.data;
|
|
|
|
const callback = pendingResponses.get(msg.id);
|
|
|
|
pendingResponses.delete(msg.id);
|
2015-06-11 18:12:23 +02:00
|
|
|
callback(msg.response);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const stopWorker = function() {
|
|
|
|
if ( workerTTLTimer !== undefined ) {
|
|
|
|
clearTimeout(workerTTLTimer);
|
|
|
|
workerTTLTimer = undefined;
|
2015-06-11 18:12:23 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( worker === null ) { return; }
|
2015-06-11 18:12:23 +02:00
|
|
|
worker.terminate();
|
|
|
|
worker = null;
|
|
|
|
needLists = true;
|
2019-09-15 13:58:28 +02:00
|
|
|
pendingResponses.clear();
|
2015-06-11 18:12:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const initWorker = function() {
|
2015-06-11 18:12:23 +02:00
|
|
|
if ( worker === null ) {
|
|
|
|
worker = new Worker('js/reverselookup-worker.js');
|
|
|
|
worker.onmessage = onWorkerMessage;
|
|
|
|
}
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
// The worker will be shutdown after n minutes without being used.
|
|
|
|
if ( workerTTLTimer !== undefined ) {
|
|
|
|
clearTimeout(workerTTLTimer);
|
2015-06-11 18:12:23 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
workerTTLTimer = vAPI.setTimeout(stopWorker, workerTTL);
|
2015-06-11 18:12:23 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( needLists === false ) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2015-06-11 18:12:23 +02:00
|
|
|
needLists = false;
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const entries = new Map();
|
2015-06-11 18:12:23 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const onListLoaded = function(details) {
|
|
|
|
const entry = entries.get(details.assetKey);
|
2015-07-27 23:55:25 +02:00
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/536
|
2017-01-18 19:17:47 +01:00
|
|
|
// Use assetKey when there is no filter list title.
|
2015-07-27 23:55:25 +02:00
|
|
|
|
2015-06-11 18:12:23 +02:00
|
|
|
worker.postMessage({
|
|
|
|
what: 'setList',
|
|
|
|
details: {
|
2017-01-18 19:17:47 +01:00
|
|
|
assetKey: details.assetKey,
|
|
|
|
title: entry.title || details.assetKey,
|
2015-06-11 18:12:23 +02:00
|
|
|
supportURL: entry.supportURL,
|
|
|
|
content: details.content
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const µb = µBlock;
|
|
|
|
for ( const listKey in µb.availableFilterLists ) {
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( µb.availableFilterLists.hasOwnProperty(listKey) === false ) {
|
2015-06-11 18:12:23 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
const entry = µb.availableFilterLists[listKey];
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( entry.off === true ) { continue; }
|
2019-09-15 13:58:28 +02:00
|
|
|
entries.set(listKey, {
|
2017-01-18 19:17:47 +01:00
|
|
|
title: listKey !== µb.userFiltersPath ?
|
|
|
|
entry.title :
|
|
|
|
vAPI.i18n('1pPageName'),
|
2015-06-11 18:12:23 +02:00
|
|
|
supportURL: entry.supportURL || ''
|
2019-09-15 13:58:28 +02:00
|
|
|
});
|
2015-06-11 18:12:23 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( entries.size === 0 ) {
|
|
|
|
return Promise.resolve();
|
2015-06-11 18:12:23 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const promises = [];
|
|
|
|
for ( const listKey of entries.keys() ) {
|
|
|
|
promises.push(
|
|
|
|
µb.getCompiledFilterList(listKey).then(details => {
|
|
|
|
onListLoaded(details);
|
|
|
|
})
|
|
|
|
);
|
2015-06-11 18:12:23 +02:00
|
|
|
}
|
2019-09-15 13:58:28 +02:00
|
|
|
return Promise.all(promises);
|
2015-06-11 18:12:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const fromNetFilter = async function(compiledFilter, rawFilter, callback) {
|
2015-06-11 18:12:23 +02:00
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-13 17:21:55 +02:00
|
|
|
if ( compiledFilter === '' || rawFilter === '' ) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
await initWorker();
|
2015-06-13 17:21:55 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const id = messageId++;
|
|
|
|
const message = {
|
|
|
|
what: 'fromNetFilter',
|
|
|
|
id: id,
|
|
|
|
compiledFilter: compiledFilter,
|
|
|
|
rawFilter: rawFilter
|
2015-06-13 17:21:55 +02:00
|
|
|
};
|
2019-09-15 13:58:28 +02:00
|
|
|
pendingResponses.set(id, callback);
|
|
|
|
worker.postMessage(message);
|
2015-06-13 17:21:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const fromCosmeticFilter = async function(details, callback) {
|
2018-07-22 16:47:02 +02:00
|
|
|
if ( typeof callback !== 'function' ) { return; }
|
2015-06-13 17:21:55 +02:00
|
|
|
|
2018-07-22 16:47:02 +02:00
|
|
|
if ( details.rawFilter === '' ) {
|
2015-06-13 17:21:55 +02:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
await initWorker();
|
|
|
|
|
|
|
|
const id = messageId++;
|
|
|
|
const hostname = µBlock.URI.hostnameFromURI(details.url);
|
|
|
|
pendingResponses.set(id, callback);
|
|
|
|
worker.postMessage({
|
|
|
|
what: 'fromCosmeticFilter',
|
|
|
|
id: id,
|
|
|
|
domain: µBlock.URI.domainFromHostname(hostname),
|
|
|
|
hostname: hostname,
|
2019-09-21 17:30:38 +02:00
|
|
|
ignoreGeneric:
|
|
|
|
µBlock.staticNetFilteringEngine.matchStringElementHide(
|
|
|
|
'generic',
|
|
|
|
details.url
|
|
|
|
) === 2,
|
|
|
|
ignoreSpecific:
|
|
|
|
µBlock.staticNetFilteringEngine.matchStringElementHide(
|
|
|
|
'specific',
|
|
|
|
details.url
|
|
|
|
) === 2,
|
2019-09-15 13:58:28 +02:00
|
|
|
rawFilter: details.rawFilter
|
|
|
|
});
|
2015-06-11 18:12:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// This tells the worker that filter lists may have changed.
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const resetLists = function() {
|
2015-06-11 18:12:23 +02:00
|
|
|
needLists = true;
|
2019-09-15 13:58:28 +02:00
|
|
|
if ( worker === null ) { return; }
|
2015-06-11 18:12:23 +02:00
|
|
|
worker.postMessage({ what: 'resetLists' });
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
return {
|
2019-09-15 13:58:28 +02:00
|
|
|
fromNetFilter,
|
|
|
|
fromCosmeticFilter,
|
|
|
|
resetLists,
|
2015-06-11 18:12:23 +02:00
|
|
|
shutdown: stopWorker
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|