wicked good xpath example
by brian428
HTML
<script src="https://s3.amazonaws.com/nolme-public/wgxpath.install.js"></script>
<div>
<p>
This is a test text
</p>
<bgcolor="#FFFF00">
<p>
Bogus text.
</p>
</bgcolor>
</div>
<button onClick="selectAll();">Select all</button>
<button onClick="selectP();">Select P1</button>
<button onClick="selectBG();">Select BGCOLOR</button>
JavaScript
e1 = document.evaluate;
wgxpath.install();
console.log("Installed customized Wicked Good XPath.");
e2 = document.evaluate;
if (e1 === e2) {
console.log("I don't see any difference in document.evaluate().");
} else {
console.log("Nice, I see that document.evaluate() implementation has changed.");
}
lookUpNode = function (path) {
return document.evaluate(path, document, null, 0, null).iterateNext();
}
selectNode = function (node) {
r = document.createRange();
r.setStartBefore(node);
r.setEndAfter(node);
sel = getSelection();
sel.removeAllRanges();
sel.addRange(r);
}
testPath = function(path) {
node = lookUpNode(path);
selectNode(node);
}
selectAll = function() { testPath('/HTML/BODY/DIV'); }
selectP = function() { testPath('/HTML/BODY/DIV/P'); }
selectBG = function() { testPath('/HTML/BODY/DIV/BGCOLOR="#FFFF00"'); }