Combining XPathResults from document.evaluate
I am doing a bunch of document.evaluate, then iterating over each result with a for loop on result.snapshotLength
.
Since I am doing the same inside each loop (a thisDiv.parentNode.removeChild
), I would like to do only one loop.
I read that:
The fifth parameter can be used to combine the results of two XPath queries. Passing the previous call to document.evaluate as a result, and it will return the combined results of both queries
So, I tried:
comDivs = document.evaluate(
"//div[@class='class name 1']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
ggDivs = document.evaluate(
"//div[@class='class name 2']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
comDivs);
But that doesn't work (although I don't have an error log, it just doesn't work).
What's the correct way to do this? Can I run different XPath queries and combine the results? Or is there a way to pass regular expressions or some kind of interleaving of the query itself?
The code I have now is: http://userscripts.org/scripts/review/58939
Thank you for your help!
You can simplify your XPath in usercript and combine individual expressions with " |
":
"//div[@class='reactionsCom reactionsNiv1 first'] | " +
"//div[@class='googleBanner'] | " +
"//div[@class='blocE1B-16b'] | " +
"//div[@class='blocE1B-16b clear']"
or even better in one expression with severel conditions:
"//div[@class='reactionsCom reactionsNiv1 first' or " +
"@class='googleBanner' or " +
"@class='blocE1B-16b' or " +
"@class='blocE1B-16b clear']"
As in your example above, iirc the second query will find matches if the result of f contains the first query, i.e. if //div[@class='class name 2']
, where are the child result nodes from the first query //div[@class='class name 2']
.
source to share