mirror of
https://github.com/gorhill/uBlock.git
synced 2026-03-11 09:04:36 +00:00
To minimize delay to First Contentful Paint. The idea is to avoid complex data structures such as Map in order to speed up first content script execution. Use stringified arrays where practical and instanciate those arrays from their stringified representation only when there are actual cosmetic filters to apply. Related commit: https://github.com/gorhill/uBlock/commit/6039ef2b6d
169 lines
5 KiB
JavaScript
169 lines
5 KiB
JavaScript
/*******************************************************************************
|
|
|
|
uBlock Origin Lite - a comprehensive, MV3-compliant content blocker
|
|
Copyright (C) 2014-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
|
|
|
|
*/
|
|
|
|
// ruleset: $rulesetId$
|
|
|
|
// Important!
|
|
// Isolate from global scope
|
|
|
|
// Start of local scope
|
|
(function uBOL_scriptlets() {
|
|
|
|
/******************************************************************************/
|
|
|
|
self.$scriptletCode$
|
|
|
|
/******************************************************************************/
|
|
|
|
const scriptletGlobals = {}; // eslint-disable-line
|
|
|
|
const $scriptletFunctions$ = self.$scriptletFunctions$;
|
|
|
|
const $scriptletArgs$ = self.$scriptletArgs$;
|
|
|
|
const $scriptletArglists$ = self.$scriptletArglists$;
|
|
|
|
const $scriptletArglistRefs$ = self.$scriptletArglistRefs$;
|
|
|
|
const $scriptletHostnames$ = self.$scriptletHostnames$;
|
|
|
|
const $hasEntities$ = self.$hasEntities$;
|
|
const $hasAncestors$ = self.$hasAncestors$;
|
|
|
|
/******************************************************************************/
|
|
|
|
const entries = (( ) => {
|
|
const docloc = document.location;
|
|
const origins = [ docloc.origin ];
|
|
if ( docloc.ancestorOrigins ) {
|
|
origins.push(...docloc.ancestorOrigins);
|
|
}
|
|
return origins.map((origin, i) => {
|
|
const beg = origin.indexOf('://');
|
|
if ( beg === -1 ) { return; }
|
|
const hn1 = origin.slice(beg+3)
|
|
const end = hn1.indexOf(':');
|
|
const hn2 = end === -1 ? hn1 : hn1.slice(0, end);
|
|
const hnParts = hn2.split('.');
|
|
if ( hn2.length === 0 ) { return; }
|
|
const hns = [];
|
|
for ( let i = 0; i < hnParts.length; i++ ) {
|
|
hns.push(`${hnParts.slice(i).join('.')}`);
|
|
}
|
|
const ens = [];
|
|
if ( $hasEntities$ ) {
|
|
const n = hnParts.length - 1;
|
|
for ( let i = 0; i < n; i++ ) {
|
|
for ( let j = n; j > i; j-- ) {
|
|
ens.push(`${hnParts.slice(i,j).join('.')}.*`);
|
|
}
|
|
}
|
|
ens.sort((a, b) => {
|
|
const d = b.length - a.length;
|
|
if ( d !== 0 ) { return d; }
|
|
return a > b ? -1 : 1;
|
|
});
|
|
}
|
|
return { hns, ens, i };
|
|
}).filter(a => a !== undefined);
|
|
})();
|
|
if ( entries.length === 0 ) { return; }
|
|
|
|
const collectArglistRefIndices = (out, hn, r) => {
|
|
let l = 0, i = 0, d = 0;
|
|
let candidate = '';
|
|
while ( l < r ) {
|
|
i = l + r >>> 1;
|
|
candidate = $scriptletHostnames$[i];
|
|
d = hn.length - candidate.length;
|
|
if ( d === 0 ) {
|
|
if ( hn === candidate ) {
|
|
out.add(i); break;
|
|
}
|
|
d = hn < candidate ? -1 : 1;
|
|
}
|
|
if ( d < 0 ) {
|
|
r = i;
|
|
} else {
|
|
l = i + 1;
|
|
}
|
|
}
|
|
return i;
|
|
};
|
|
|
|
const indicesFromHostname = (out, hnDetails, suffix = '') => {
|
|
if ( hnDetails.hns.length === 0 ) { return; }
|
|
let r = $scriptletHostnames$.length;
|
|
for ( const hn of hnDetails.hns ) {
|
|
r = collectArglistRefIndices(out, `${hn}${suffix}`, r);
|
|
}
|
|
if ( $hasEntities$ ) {
|
|
let r = $scriptletHostnames$.length;
|
|
for ( const en of hnDetails.ens ) {
|
|
r = collectArglistRefIndices(out, `${en}${suffix}`, r);
|
|
}
|
|
}
|
|
};
|
|
|
|
const todoIndices = new Set();
|
|
indicesFromHostname(todoIndices, entries[0]);
|
|
if ( $hasAncestors$ ) {
|
|
for ( const entry of entries ) {
|
|
if ( entry.i === 0 ) { continue; }
|
|
indicesFromHostname(todoIndices, entry, '>>');
|
|
}
|
|
}
|
|
$scriptletHostnames$.length = 0;
|
|
|
|
if ( todoIndices.size === 0 ) { return; }
|
|
|
|
// Collect arglist references
|
|
const todo = new Set();
|
|
{
|
|
const arglistRefs = $scriptletArglistRefs$.split(';');
|
|
for ( const i of todoIndices ) {
|
|
for ( const ref of JSON.parse(`[${arglistRefs[i]}]`) ) {
|
|
todo.add(ref);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Execute scriplets
|
|
{
|
|
const arglists = $scriptletArglists$.split(';');
|
|
const args = $scriptletArgs$;
|
|
for ( const ref of todo ) {
|
|
if ( ref < 0 ) { continue; }
|
|
if ( todo.has(~ref) ) { continue; }
|
|
const arglist = JSON.parse(`[${arglists[ref]}]`);
|
|
const fn = $scriptletFunctions$[arglist[0]];
|
|
try { fn(...arglist.slice(1).map(a => args[a])); }
|
|
catch { }
|
|
}
|
|
}
|
|
|
|
/******************************************************************************/
|
|
|
|
// End of local scope
|
|
})();
|
|
|
|
void 0;
|