mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +02:00
Improve logic to detect XMLDocument
response in xml-prune
Related discussion: - https://github.com/uBlockOrigin/uBlock-discussions/discussions/792#discussioncomment-6561841 Additionally, added extra parameter `..., logdoc, 1` to allow dumping whole document being worked on to the console.
This commit is contained in:
parent
d3ef9ccfd1
commit
6d7674e69f
@ -2303,7 +2303,8 @@ function xmlPrune(
|
|||||||
if ( selector === '' ) { return; }
|
if ( selector === '' ) { return; }
|
||||||
const reUrl = patternToRegex(urlPattern);
|
const reUrl = patternToRegex(urlPattern);
|
||||||
const extraArgs = getExtraArgs(Array.from(arguments), 3);
|
const extraArgs = getExtraArgs(Array.from(arguments), 3);
|
||||||
const log = shouldLog(extraArgs);
|
const safe = safeSelf();
|
||||||
|
const log = shouldLog(extraArgs) ? ((...args) => { safe.uboLog(...args); }) : (( ) => { });
|
||||||
const queryAll = (xmlDoc, selector) => {
|
const queryAll = (xmlDoc, selector) => {
|
||||||
const isXpath = /^xpath\(.+\)$/.test(selector);
|
const isXpath = /^xpath\(.+\)$/.test(selector);
|
||||||
if ( isXpath === false ) {
|
if ( isXpath === false ) {
|
||||||
@ -2328,19 +2329,20 @@ function xmlPrune(
|
|||||||
if ( selectorCheck !== '' && xmlDoc.querySelector(selectorCheck) === null ) {
|
if ( selectorCheck !== '' && xmlDoc.querySelector(selectorCheck) === null ) {
|
||||||
return xmlDoc;
|
return xmlDoc;
|
||||||
}
|
}
|
||||||
|
if ( extraArgs.logdoc ) {
|
||||||
|
const serializer = new XMLSerializer();
|
||||||
|
log(`xmlPrune: document is\n\t${serializer.serializeToString(xmlDoc)}`);
|
||||||
|
}
|
||||||
const items = queryAll(xmlDoc, selector);
|
const items = queryAll(xmlDoc, selector);
|
||||||
if ( items.length !== 0 ) {
|
if ( items.length === 0 ) { return xmlDoc; }
|
||||||
if ( log ) { safeSelf().uboLog(`xmlPrune: removing ${items.length} items`); }
|
log(`xmlPrune: removing ${items.length} items`);
|
||||||
for ( const item of items ) {
|
for ( const item of items ) {
|
||||||
if ( item.nodeType === 1 ) {
|
if ( item.nodeType === 1 ) {
|
||||||
item.remove();
|
item.remove();
|
||||||
} else if ( item.nodeType === 2 ) {
|
} else if ( item.nodeType === 2 ) {
|
||||||
item.ownerElement.removeAttribute(item.nodeName);
|
item.ownerElement.removeAttribute(item.nodeName);
|
||||||
}
|
}
|
||||||
if ( log ) {
|
log(`xmlPrune: ${item.constructor.name}.${item.nodeName} removed`);
|
||||||
safeSelf().uboLog(`xmlPrune: ${item.constructor.name}.${item.nodeName} removed`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch(ex) {
|
} catch(ex) {
|
||||||
if ( log ) { safeSelf().uboLog(ex); }
|
if ( log ) { safeSelf().uboLog(ex); }
|
||||||
@ -2391,11 +2393,17 @@ function xmlPrune(
|
|||||||
thisArg.addEventListener('readystatechange', function() {
|
thisArg.addEventListener('readystatechange', function() {
|
||||||
if ( thisArg.readyState !== 4 ) { return; }
|
if ( thisArg.readyState !== 4 ) { return; }
|
||||||
const type = thisArg.responseType;
|
const type = thisArg.responseType;
|
||||||
if ( type === 'document' || thisArg.responseXML instanceof XMLDocument ) {
|
if (
|
||||||
|
type === 'document' ||
|
||||||
|
type === '' && thisArg.responseXML instanceof XMLDocument
|
||||||
|
) {
|
||||||
pruneFromDoc(thisArg.responseXML);
|
pruneFromDoc(thisArg.responseXML);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( type === 'text' || typeof thisArg.responseText === 'string' ) {
|
if (
|
||||||
|
type === 'text' ||
|
||||||
|
type === '' && typeof thisArg.responseText === 'string'
|
||||||
|
) {
|
||||||
const textin = thisArg.responseText;
|
const textin = thisArg.responseText;
|
||||||
const textout = pruneFromText(textin);
|
const textout = pruneFromText(textin);
|
||||||
if ( textout === textin ) { return; }
|
if ( textout === textin ) { return; }
|
||||||
|
Loading…
Reference in New Issue
Block a user