Skip to content

Commit

Permalink
[mv3] Add ability to handle entity-based CSS and scriptlet injection …
Browse files Browse the repository at this point in the history
…filters

This commit adds the ability to inject entity-based plain CSS
filters and also a set of the most commonly used entity-based
scriptlet injection filters.

Since the scripting API is not compatible with entity patterns,
the entity-related content scripts are injected in all documents
and the entity-matching is done by the content script themselves.

Given this, entity-based content scripts are enabled only when
working in the Complete filtering mode, there won't be any
entity-based filters injected in lower modes.

Also, since there is no way to reasonably have access to the
Public Suffix List in the content scripts, the entity-matching
algorithm is an approximation, though I expect false positives
to be rare (time will tell). In the event of such false
positive, simply falling back to Optimal mode will fix the
issue.

The following issues have been fixed at the same time:

Fixed the no-filtering mode related rules having lower priority
then redirect rules, i.e. redirect rules would still be applied
despite disabling all filtering on a site.

Fixed improper detection of changes to the generic-related CSS
content script, potentially causing undue delays when for example
trying to access the popup panel while working in Complete mode.
The scripting MV3 can be quite slow when registering/updating
large content scripts, so uBOL does its best to call the API only
if really needed, but there had been a regression in the recent
builds preventing uBO from properly detecting unchanged content
script parameters.
  • Loading branch information
gorhill committed Oct 20, 2022
1 parent e84b374 commit 433adac
Show file tree
Hide file tree
Showing 15 changed files with 1,546 additions and 61 deletions.
1 change: 1 addition & 0 deletions platform/mv3/extension/js/mode-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ async function setFilteringModeDetails(afterDetails) {
requestDomains: [],
resourceTypes: [ 'main_frame' ],
},
priority: 100,
};
if ( actualDetails.none.size ) {
rule.condition.requestDomains = Array.from(actualDetails.none);
Expand Down
7 changes: 6 additions & 1 deletion platform/mv3/extension/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,17 @@ async function init() {
if ( popupPanelData.hasOmnipotence ) {
ruleCount += rules.removeparam + rules.redirect;
}
let specificCount = 0;
if ( css.specific instanceof Object ) {
specificCount += css.specific.domainBased;
specificCount += css.specific.entityBased;
}
dom.text(
qs$('p', div),
i18n$('perRulesetStats')
.replace('{{ruleCount}}', ruleCount.toLocaleString())
.replace('{{filterCount}}', filters.accepted.toLocaleString())
.replace('{{cssSpecificCount}}', css.specific.toLocaleString())
.replace('{{cssSpecificCount}}', specificCount.toLocaleString())
);
parent.append(div);
}
Expand Down
136 changes: 133 additions & 3 deletions platform/mv3/extension/js/scripting-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ function registerGeneric(context, genericDetails) {
if ( hostnames !== undefined ) {
excludeHostnames.push(...hostnames);
}
if ( details.css.generic instanceof Object === false ) { continue; }
if ( details.css.generic.count === 0 ) { continue; }
js.push(`/rulesets/scripting/generic/${details.id}.generic.js`);
js.push(`/rulesets/scripting/generic/${details.id}.js`);
}

if ( js.length === 0 ) { return; }
Expand Down Expand Up @@ -202,7 +203,7 @@ function registerProcedural(context, proceduralDetails) {
const hostnameMatches = [];
for ( const details of rulesetsDetails ) {
if ( details.css.procedural === 0 ) { continue; }
js.push(`/rulesets/scripting/procedural/${details.id}.procedural.js`);
js.push(`/rulesets/scripting/procedural/${details.id}.js`);
if ( proceduralDetails.has(details.id) ) {
hostnameMatches.push(...proceduralDetails.get(details.id));
}
Expand Down Expand Up @@ -278,7 +279,7 @@ function registerDeclarative(context, declarativeDetails) {
const hostnameMatches = [];
for ( const details of rulesetsDetails ) {
if ( details.css.declarative === 0 ) { continue; }
js.push(`/rulesets/scripting/declarative/${details.id}.declarative.js`);
js.push(`/rulesets/scripting/declarative/${details.id}.js`);
if ( declarativeDetails.has(details.id) ) {
hostnameMatches.push(...declarativeDetails.get(details.id));
}
Expand Down Expand Up @@ -420,6 +421,71 @@ function registerScriptlet(context, scriptletDetails) {

/******************************************************************************/

function registerScriptletEntity(context) {
const { before, filteringModeDetails, rulesetsDetails } = context;

const js = [];
for ( const details of rulesetsDetails ) {
const { scriptlets } = details;
if ( scriptlets instanceof Object === false ) { continue; }
if ( Array.isArray(scriptlets.entityBasedTokens) === false ) { continue; }
if ( scriptlets.entityBasedTokens.length === 0 ) { continue; }
for ( const token of scriptlets.entityBasedTokens ) {
js.push(`/rulesets/scripting/scriptlet-entity/${details.id}.${token}.js`);
}
}

if ( js.length === 0 ) { return; }

const matches = [];
const excludeMatches = [];
if ( filteringModeDetails.extendedGeneric.has('all-urls') ) {
excludeMatches.push(...ut.matchesFromHostnames(filteringModeDetails.none));
excludeMatches.push(...ut.matchesFromHostnames(filteringModeDetails.network));
excludeMatches.push(...ut.matchesFromHostnames(filteringModeDetails.extendedSpecific));
matches.push('<all_urls>');
} else {
matches.push(
...ut.matchesFromHostnames(filteringModeDetails.extendedGeneric)
);
}

if ( matches.length === 0 ) { return; }

const registered = before.get('scriptlet.entity');
before.delete('scriptlet.entity'); // Important!

// register
if ( registered === undefined ) {
context.toAdd.push({
id: 'scriptlet.entity',
js,
matches,
excludeMatches,
runAt: 'document_start',
world: 'MAIN',
});
return;
}

// update
const directive = { id: 'scriptlet.entity' };
if ( arrayEq(registered.js, js, false) === false ) {
directive.js = js;
}
if ( arrayEq(registered.matches, matches) === false ) {
directive.matches = matches;
}
if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) {
directive.excludeMatches = excludeMatches;
}
if ( directive.js || directive.matches || directive.excludeMatches ) {
context.toUpdate.push(directive);
}
}

/******************************************************************************/

function registerSpecific(context, specificDetails) {
const { filteringModeDetails } = context;

Expand Down Expand Up @@ -561,6 +627,68 @@ const toUpdatableScript = (context, fname, hostnames) => {

/******************************************************************************/

function registerSpecificEntity(context) {
const { before, filteringModeDetails, rulesetsDetails } = context;

const js = [];
for ( const details of rulesetsDetails ) {
if ( details.css.specific instanceof Object === false ) { continue; }
if ( details.css.specific.entityBased === 0 ) { continue; }
js.push(`/rulesets/scripting/specific-entity/${details.id}.js`);
}

if ( js.length === 0 ) { return; }

const matches = [];
const excludeMatches = [];
if ( filteringModeDetails.extendedGeneric.has('all-urls') ) {
excludeMatches.push(...ut.matchesFromHostnames(filteringModeDetails.none));
excludeMatches.push(...ut.matchesFromHostnames(filteringModeDetails.network));
excludeMatches.push(...ut.matchesFromHostnames(filteringModeDetails.extendedSpecific));
matches.push('<all_urls>');
} else {
matches.push(
...ut.matchesFromHostnames(filteringModeDetails.extendedGeneric)
);
}

if ( matches.length === 0 ) { return; }

js.push('/js/scripting/css-specific.entity.js');

const registered = before.get('css-specific.entity');
before.delete('css-specific.entity'); // Important!

// register
if ( registered === undefined ) {
context.toAdd.push({
id: 'css-specific.entity',
js,
matches,
excludeMatches,
runAt: 'document_start',
});
return;
}

// update
const directive = { id: 'css-specific.entity' };
if ( arrayEq(registered.js, js, false) === false ) {
directive.js = js;
}
if ( arrayEq(registered.matches, matches) === false ) {
directive.matches = matches;
}
if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) {
directive.excludeMatches = excludeMatches;
}
if ( directive.js || directive.matches || directive.excludeMatches ) {
context.toUpdate.push(directive);
}
}

/******************************************************************************/

async function registerInjectables(origins) {
void origins;

Expand Down Expand Up @@ -604,7 +732,9 @@ async function registerInjectables(origins) {
registerDeclarative(context, declarativeDetails);
registerProcedural(context, proceduralDetails);
registerScriptlet(context, scriptletDetails);
registerScriptletEntity(context);
registerSpecific(context, specificDetails);
registerSpecificEntity(context);
registerGeneric(context, genericDetails);

toRemove.push(...Array.from(before.keys()));
Expand Down
86 changes: 86 additions & 0 deletions platform/mv3/extension/js/scripting/css-specific.entity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2019-present 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
*/

/* jshint esversion:11 */

'use strict';

/******************************************************************************/

// Important!
// Isolate from global scope
(function uBOL_cssSpecificEntity() {

/******************************************************************************/

// $rulesetId$

const specificEntityImports = self.specificEntityImports || [];

/******************************************************************************/

const lookupSelectors = (hn, entity, out) => {
for ( const { argsList, entitiesMap } of specificEntityImports ) {
let argsIndices = entitiesMap.get(entity);
if ( argsIndices === undefined ) { continue; }
if ( typeof argsIndices === 'number' ) { argsIndices = [ argsIndices ]; }
for ( const argsIndex of argsIndices ) {
const details = argsList[argsIndex];
if ( details.n && details.n.includes(hn) ) { continue; }
out.push(details.a);
}
}
};

let hn = '';
try { hn = document.location.hostname; } catch(ex) { }
const selectors = [];
const hnparts = hn.split('.');
const hnpartslen = hnparts.length - 1;
for ( let i = 0; i < hnpartslen; i++ ) {
for ( let j = hnpartslen; j > i; j-- ) {
lookupSelectors(
hnparts.slice(i).join('.'),
hnparts.slice(i,j).join('.'),
selectors
);
}
}

self.specificEntityImports = undefined;

if ( selectors.length === 0 ) { return; }

try {
const sheet = new CSSStyleSheet();
sheet.replace(`@layer{${selectors.join(',')}{display:none!important;}}`);
document.adoptedStyleSheets = [
...document.adoptedStyleSheets,
sheet
];
} catch(ex) {
}

/******************************************************************************/

})();

/******************************************************************************/
Loading

0 comments on commit 433adac

Please sign in to comment.