From 83d028ac7d974ff264e3f8d67c2f1e2237dabcb3 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 25 Apr 2022 09:49:31 -0400 Subject: [PATCH] Report specific filter before generic one Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/2092 Regression from: - https://github.com/gorhill/uBlock/commit/72bb89495ba0928042b8c0fe22ee57f6e955ca90 --- src/js/messaging.js | 2 +- src/js/reverselookup-worker.js | 49 +++++++++++++++------------------- src/js/reverselookup.js | 6 ++--- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/js/messaging.js b/src/js/messaging.js index 6feb12c7c..ce210c9f3 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -118,7 +118,7 @@ const onMessage = function(request, sender, callback) { return; case 'listsFromCosmeticFilter': - staticFilteringReverseLookup.fromCosmeticFilter( + staticFilteringReverseLookup.fromExtendedFilter( request ).then(response => { callback(response); diff --git a/src/js/reverselookup-worker.js b/src/js/reverselookup-worker.js index eb20e3005..48ea97498 100644 --- a/src/js/reverselookup-worker.js +++ b/src/js/reverselookup-worker.js @@ -23,31 +23,28 @@ /******************************************************************************/ -{ -// >>>>> start of local scope +let listEntries = Object.create(null); /******************************************************************************/ -const reBlockStart = /^#block-start-([\w:]+)\n/gm; -let listEntries = Object.create(null); +// https://github.com/uBlockOrigin/uBlock-issues/issues/2092 +// Order of ids matters const extractBlocks = function(content, ...ids) { - reBlockStart.lastIndex = 0; const out = []; - let match = reBlockStart.exec(content); - while ( match !== null ) { - const beg = match.index + match[0].length; - const id = match[1]; - if ( ids.includes(id) ) { - const end = content.indexOf(`#block-end-${id}`, beg); - out.push(content.slice(beg, end)); - reBlockStart.lastIndex = end; - } - match = reBlockStart.exec(content); + for ( const id of ids ) { + const pattern = `#block-start-${id}\n`; + let beg = content.indexOf(pattern); + if ( beg === -1 ) { continue; } + beg += pattern.length; + const end = content.indexOf(`#block-end-${id}`, beg); + out.push(content.slice(beg, end)); } return out.join('\n'); }; +/******************************************************************************/ + // https://github.com/MajkiIT/polish-ads-filter/issues/14768#issuecomment-536006312 // Avoid reporting badfilter-ed filters. @@ -66,8 +63,7 @@ const fromNetFilter = function(details) { // We need an exact match. // https://github.com/gorhill/uBlock/issues/1392 // https://github.com/gorhill/uBlock/issues/835 - const notFound = pos !== 0 && - content.charCodeAt(pos - 1) !== 0x0A; + const notFound = pos !== 0 && content.charCodeAt(pos - 1) !== 0x0A; pos += compiledFilter.length; if ( notFound || @@ -90,6 +86,8 @@ const fromNetFilter = function(details) { self.postMessage({ id: details.id, response }); }; +/******************************************************************************/ + // Looking up filter lists from a cosmetic filter is a bit more complicated // than with network filters: // @@ -110,7 +108,7 @@ const fromNetFilter = function(details) { // FilterContainer.fromCompiledContent() is our reference code to create // the various compiled versions. -const fromCosmeticFilter = function(details) { +const fromExtendedFilter = function(details) { const match = /^#@?#\^?/.exec(details.rawFilter); const prefix = match[0]; const exception = prefix.charAt(1) === '@'; @@ -161,8 +159,8 @@ const fromCosmeticFilter = function(details) { if ( entry === undefined ) { continue; } const content = extractBlocks( entry.content, - 'COSMETIC_FILTERS:GENERIC', 'COSMETIC_FILTERS:SPECIFIC', + 'COSMETIC_FILTERS:GENERIC', 'SCRIPTLET_FILTERS', 'HTML_FILTERS', 'HTTPHEADER_FILTERS' @@ -270,7 +268,9 @@ const fromCosmeticFilter = function(details) { self.postMessage({ id: details.id, response }); }; -self.onmessage = function(e) { // jshint ignore:line +/******************************************************************************/ + +self.onmessage = function(e) { const msg = e.data; switch ( msg.what ) { @@ -286,15 +286,10 @@ self.onmessage = function(e) { // jshint ignore:line fromNetFilter(msg); break; - case 'fromCosmeticFilter': - fromCosmeticFilter(msg); + case 'fromExtendedFilter': + fromExtendedFilter(msg); break; } }; /******************************************************************************/ - -// <<<<< end of local scope -} - -/******************************************************************************/ diff --git a/src/js/reverselookup.js b/src/js/reverselookup.js index ed6b8288c..5ffe41b63 100644 --- a/src/js/reverselookup.js +++ b/src/js/reverselookup.js @@ -155,7 +155,7 @@ const fromNetFilter = async function(rawFilter) { }); }; -const fromCosmeticFilter = async function(details) { +const fromExtendedFilter = async function(details) { if ( typeof details.rawFilter !== 'string' || details.rawFilter === '' @@ -169,7 +169,7 @@ const fromCosmeticFilter = async function(details) { const hostname = hostnameFromURI(details.url); worker.postMessage({ - what: 'fromCosmeticFilter', + what: 'fromExtendedFilter', id: id, domain: domainFromHostname(hostname), hostname: hostname, @@ -203,7 +203,7 @@ const resetLists = function() { const staticFilteringReverseLookup = { fromNetFilter, - fromCosmeticFilter, + fromExtendedFilter, resetLists, shutdown: stopWorker };