1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +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 = ''; }
const needles = [];
for ( const condition of arg1.split(/\s+/) ) {
if ( condition === '' ) { continue; }
const pos = condition.indexOf(':');
let key, value;
if ( pos !== -1 ) {
@ -607,6 +608,7 @@
}
needles.push({ key, re: new RegExp(value) });
}
const log = needles.length === 0 ? console.log.bind(console) : undefined;
self.fetch = new Proxy(self.fetch, {
apply: function(target, thisArg, args) {
let proceed = true;
@ -622,7 +624,13 @@
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 ) {
if (
props.has(key) === false ||
@ -652,10 +660,7 @@
if ( selector === '' || selector === '{{2}}' ) {
selector = `[${tokens.join('],[')}]`;
}
const rmattr = function(ev) {
if ( ev ) {
window.removeEventListener(ev.type, rmattr, true);
}
const rmattr = function() {
try {
const nodes = document.querySelectorAll(selector);
for ( const node of nodes ) {
@ -667,7 +672,7 @@
}
};
if ( document.readyState === 'loading' ) {
window.addEventListener('DOMContentLoaded', rmattr, true);
window.addEventListener('DOMContentLoaded', rmattr, { once: true });
} else {
rmattr();
}