2015-06-11 18:12:23 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2015-2017 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global onmessage, postMessage */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
var listEntries = Object.create(null),
|
|
|
|
filterClassSeparator = '\n/* end of network - start of cosmetic */\n';
|
2015-06-11 18:12:23 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-13 17:21:55 +02:00
|
|
|
var fromNetFilter = function(details) {
|
2017-05-25 23:46:59 +02:00
|
|
|
var lists = [],
|
|
|
|
compiledFilter = details.compiledFilter,
|
|
|
|
entry, content, pos, notFound;
|
2017-01-18 19:17:47 +01:00
|
|
|
for ( var assetKey in listEntries ) {
|
|
|
|
entry = listEntries[assetKey];
|
2017-05-25 23:46:59 +02:00
|
|
|
if ( entry === undefined ) { continue; }
|
|
|
|
content = entry.content.slice(
|
|
|
|
0,
|
|
|
|
entry.content.indexOf(filterClassSeparator)
|
|
|
|
);
|
2016-07-05 01:42:34 +02:00
|
|
|
pos = 0;
|
|
|
|
for (;;) {
|
|
|
|
pos = content.indexOf(compiledFilter, pos);
|
2017-05-12 16:35:11 +02:00
|
|
|
if ( pos === -1 ) { break; }
|
2016-07-05 01:42:34 +02:00
|
|
|
// We need an exact match.
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1392
|
|
|
|
// https://github.com/gorhill/uBlock/issues/835
|
2017-05-25 23:46:59 +02:00
|
|
|
notFound = pos !== 0 && content.charCodeAt(pos - 1) !== 0x0A;
|
|
|
|
pos += compiledFilter.length;
|
|
|
|
if (
|
|
|
|
notFound ||
|
|
|
|
pos !== content.length && content.charCodeAt(pos) !== 0x0A
|
|
|
|
) {
|
|
|
|
continue;
|
2016-07-05 01:42:34 +02:00
|
|
|
}
|
2017-05-25 23:46:59 +02:00
|
|
|
lists.push({
|
|
|
|
title: entry.title,
|
|
|
|
supportURL: entry.supportURL
|
|
|
|
});
|
|
|
|
break;
|
2015-10-16 17:42:45 +02:00
|
|
|
}
|
2015-06-11 18:12:23 +02:00
|
|
|
}
|
|
|
|
|
2015-06-13 17:21:55 +02:00
|
|
|
var response = {};
|
|
|
|
response[details.rawFilter] = lists;
|
|
|
|
|
2015-06-11 18:12:23 +02:00
|
|
|
postMessage({
|
|
|
|
id: details.id,
|
2015-06-13 17:21:55 +02:00
|
|
|
response: response
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Looking up filter lists from a cosmetic filter is a bit more complicated
|
|
|
|
// than with network filters:
|
|
|
|
//
|
|
|
|
// The filter is its raw representation, not its compiled version. This is
|
|
|
|
// because the cosmetic filtering engine can't translate a live cosmetic
|
|
|
|
// filter into its compiled version. Reason is I do not want to burden
|
|
|
|
// cosmetic filtering with the resource overhead of being able to re-compile
|
|
|
|
// live cosmetic filters. I want the cosmetic filtering code to be left
|
|
|
|
// completely unaffected by reverse lookup requirements.
|
|
|
|
//
|
|
|
|
// Mainly, given a CSS selector and a hostname as context, we will derive
|
|
|
|
// various versions of compiled filters and see if there are matches. This way
|
|
|
|
// the whole CPU cost is incurred by the reverse lookup code -- in a worker
|
2015-06-13 19:32:14 +02:00
|
|
|
// thread, and the cosmetic filtering engine incurs no cost at all.
|
2015-06-13 17:21:55 +02:00
|
|
|
//
|
|
|
|
// For this though, the reverse lookup code here needs some knowledge of
|
|
|
|
// the inners of the cosmetic filtering engine.
|
|
|
|
// FilterContainer.fromCompiledContent() is our reference code to create
|
|
|
|
// the various compiled versions.
|
|
|
|
|
|
|
|
var fromCosmeticFilter = function(details) {
|
2017-05-25 23:46:59 +02:00
|
|
|
var match = /^#@?#/.exec(details.rawFilter),
|
|
|
|
prefix = match[0],
|
2017-10-22 20:20:33 +02:00
|
|
|
needle = details.rawFilter.slice(prefix.length),
|
|
|
|
selector = needle;
|
2017-05-25 23:46:59 +02:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
// With low generic simple cosmetic filters, the class or id prefix
|
|
|
|
// character is not part of the compiled data. So we must be ready to
|
|
|
|
// look-up version of the selector without the prefix character.
|
2017-10-22 20:20:33 +02:00
|
|
|
var idOrClassPrefix = needle.charAt(0),
|
2017-10-21 19:43:46 +02:00
|
|
|
cssPrefixMatcher;
|
|
|
|
if ( idOrClassPrefix === '#' ) {
|
|
|
|
cssPrefixMatcher = '#?';
|
2017-10-22 20:20:33 +02:00
|
|
|
needle = needle.slice(1);
|
2017-10-21 19:43:46 +02:00
|
|
|
} else if ( idOrClassPrefix === '.' ) {
|
|
|
|
cssPrefixMatcher = '\\.?';
|
2017-10-22 20:20:33 +02:00
|
|
|
needle = needle.slice(1);
|
2017-10-21 19:43:46 +02:00
|
|
|
} else {
|
|
|
|
idOrClassPrefix = '';
|
|
|
|
cssPrefixMatcher = '';
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:35:45 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3101
|
|
|
|
// Use `m` flag for efficient regex execution.
|
2017-05-31 17:49:37 +02:00
|
|
|
var reFilter = new RegExp(
|
2017-10-06 19:35:45 +02:00
|
|
|
'^\\[\\d,[^\\n]*\\\\*"' +
|
2017-10-21 19:43:46 +02:00
|
|
|
cssPrefixMatcher +
|
2017-10-22 20:20:33 +02:00
|
|
|
reEscapeCosmetic(needle) +
|
2017-10-06 19:35:45 +02:00
|
|
|
'\\\\*"[^\\n]*\\]$',
|
|
|
|
'gm'
|
2017-05-30 19:04:01 +02:00
|
|
|
);
|
2017-05-25 23:46:59 +02:00
|
|
|
|
|
|
|
var reHostname = new RegExp(
|
|
|
|
'^' +
|
|
|
|
details.hostname.split('.').reduce(
|
|
|
|
function(acc, item) {
|
|
|
|
return acc === ''
|
|
|
|
? item
|
|
|
|
: '(' + acc + '\\.)?' + item;
|
|
|
|
},
|
|
|
|
''
|
|
|
|
) +
|
|
|
|
'$'
|
|
|
|
);
|
|
|
|
|
|
|
|
var reEntity,
|
|
|
|
domain = details.domain,
|
|
|
|
pos = domain.indexOf('.');
|
2015-06-13 17:21:55 +02:00
|
|
|
if ( pos !== -1 ) {
|
2017-05-25 23:46:59 +02:00
|
|
|
reEntity = new RegExp(
|
|
|
|
'^' +
|
|
|
|
domain.slice(0, pos).split('.').reduce(
|
|
|
|
function(acc, item) {
|
|
|
|
return acc === ''
|
|
|
|
? item
|
|
|
|
: '(' + acc + '\\.)?' + item;
|
|
|
|
},
|
|
|
|
''
|
|
|
|
) +
|
|
|
|
'\\.\\*$'
|
2015-06-13 17:21:55 +02:00
|
|
|
);
|
|
|
|
}
|
2017-05-25 23:46:59 +02:00
|
|
|
|
|
|
|
var response = Object.create(null),
|
|
|
|
assetKey, entry, content, found, fargs;
|
2015-06-13 17:21:55 +02:00
|
|
|
|
2017-05-25 23:46:59 +02:00
|
|
|
for ( assetKey in listEntries ) {
|
|
|
|
entry = listEntries[assetKey];
|
|
|
|
if ( entry === undefined ) { continue; }
|
|
|
|
content = entry.content.slice(
|
|
|
|
entry.content.indexOf(filterClassSeparator) +
|
|
|
|
filterClassSeparator.length
|
|
|
|
);
|
|
|
|
found = undefined;
|
|
|
|
while ( (match = reFilter.exec(content)) !== null ) {
|
|
|
|
fargs = JSON.parse(match[0]);
|
|
|
|
switch ( fargs[0] ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
case 0: // id-based
|
|
|
|
case 2: // class-based
|
2017-10-22 18:48:13 +02:00
|
|
|
found = prefix + selector;
|
2017-10-21 19:43:46 +02:00
|
|
|
break;
|
2017-05-25 23:46:59 +02:00
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
case 7:
|
2017-10-22 18:48:13 +02:00
|
|
|
found = prefix + selector;
|
2017-05-25 23:46:59 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 3:
|
2017-10-22 18:48:13 +02:00
|
|
|
if ( fargs[2] === selector ) {
|
|
|
|
found = prefix + selector;
|
2017-05-25 23:46:59 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
case 8:
|
2017-10-21 19:43:46 +02:00
|
|
|
case 9:
|
2017-10-22 18:48:13 +02:00
|
|
|
if ( fargs[3] !== selector ) { break; }
|
2017-05-25 23:46:59 +02:00
|
|
|
if (
|
|
|
|
fargs[2] === '' ||
|
|
|
|
reHostname.test(fargs[2]) === true ||
|
|
|
|
reEntity !== undefined && reEntity.test(fargs[2]) === true
|
|
|
|
) {
|
2017-10-22 18:48:13 +02:00
|
|
|
found = fargs[2] + prefix + selector;
|
2017-05-25 23:46:59 +02:00
|
|
|
}
|
|
|
|
break;
|
2015-06-13 17:21:55 +02:00
|
|
|
}
|
2017-05-25 23:46:59 +02:00
|
|
|
if ( found !== undefined ) {
|
|
|
|
if ( response[found] === undefined ) {
|
|
|
|
response[found] = [];
|
|
|
|
}
|
|
|
|
response[found].push({
|
|
|
|
title: entry.title,
|
|
|
|
supportURL: entry.supportURL
|
|
|
|
});
|
|
|
|
break;
|
2015-06-13 17:21:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
postMessage({
|
|
|
|
id: details.id,
|
|
|
|
response: response
|
2015-06-11 18:12:23 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-05-31 17:49:37 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2666
|
|
|
|
// Raw filters in compiled filter lists may have been JSON-stringified one or
|
|
|
|
// multiple times.
|
|
|
|
|
|
|
|
var reEscapeCosmetic = function(s) {
|
|
|
|
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
|
|
.replace(/"/g, '\\\\*"');
|
|
|
|
};
|
|
|
|
|
2015-06-11 18:12:23 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
onmessage = function(e) { // jshint ignore:line
|
2015-06-11 18:12:23 +02:00
|
|
|
var msg = e.data;
|
|
|
|
|
|
|
|
switch ( msg.what ) {
|
|
|
|
case 'resetLists':
|
|
|
|
listEntries = Object.create(null);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'setList':
|
2017-01-18 19:17:47 +01:00
|
|
|
listEntries[msg.details.assetKey] = msg.details;
|
2015-06-11 18:12:23 +02:00
|
|
|
break;
|
|
|
|
|
2015-06-13 17:21:55 +02:00
|
|
|
case 'fromNetFilter':
|
|
|
|
fromNetFilter(msg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'fromCosmeticFilter':
|
|
|
|
fromCosmeticFilter(msg);
|
2015-06-11 18:12:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|