1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-04 16:47:15 +02:00

Add logging ability to new scriptlet no-fetch-if

When no-fetch-if scriptlet is used without argument, the
parameters passed to no-fetch-if will be output to the
console, as `uBO: fetch([...list of arguments...])`.
This commit is contained in:
Raymond Hill 2020-12-11 09:28:29 -05:00
parent 497dc9a58a
commit b6ed83bc5c
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -589,6 +589,7 @@
if ( arg1 === '{{1}}' ) { arg1 = ''; } if ( arg1 === '{{1}}' ) { arg1 = ''; }
const needles = []; const needles = [];
for ( const condition of arg1.split(/\s+/) ) { for ( const condition of arg1.split(/\s+/) ) {
if ( condition === '' ) { continue; }
const pos = condition.indexOf(':'); const pos = condition.indexOf(':');
let key, value; let key, value;
if ( pos !== -1 ) { if ( pos !== -1 ) {
@ -607,6 +608,7 @@
} }
needles.push({ key, re: new RegExp(value) }); needles.push({ key, re: new RegExp(value) });
} }
const log = needles.length === 0 ? console.log.bind(console) : undefined;
self.fetch = new Proxy(self.fetch, { self.fetch = new Proxy(self.fetch, {
apply: function(target, thisArg, args) { apply: function(target, thisArg, args) {
let proceed = true; let proceed = true;
@ -622,7 +624,13 @@
props.set( prop, init[prop]); props.set( prop, init[prop]);
} }
} }
proceed = false; if ( log !== undefined ) {
const out = Array.from(props)
.map(a => `${a[0]}:${a[1]}`)
.join(' ');
log(`uBO: fetch(${out})`);
}
proceed = needles.length === 0;
for ( const { key, re } of needles ) { for ( const { key, re } of needles ) {
if ( if (
props.has(key) === false || props.has(key) === false ||
@ -652,10 +660,7 @@
if ( selector === '' || selector === '{{2}}' ) { if ( selector === '' || selector === '{{2}}' ) {
selector = `[${tokens.join('],[')}]`; selector = `[${tokens.join('],[')}]`;
} }
const rmattr = function(ev) { const rmattr = function() {
if ( ev ) {
window.removeEventListener(ev.type, rmattr, true);
}
try { try {
const nodes = document.querySelectorAll(selector); const nodes = document.querySelectorAll(selector);
for ( const node of nodes ) { for ( const node of nodes ) {
@ -667,7 +672,7 @@
} }
}; };
if ( document.readyState === 'loading' ) { if ( document.readyState === 'loading' ) {
window.addEventListener('DOMContentLoaded', rmattr, true); window.addEventListener('DOMContentLoaded', rmattr, { once: true });
} else { } else {
rmattr(); rmattr();
} }