stuart
stuart

Reputation: 2273

JavaScript to highlight XPath matches in webpage?

We need a bit of JavaScript that can be sent to a page (by dumping it in the URL bar) that will highlight all the matches of a given XPath string.

That is, the JavaScript code will contain the hard-coded XPath string.

I know there are developer tools that do this but we need a lightweight api-style solution.

Any help would be appreciated. Functioning code is preferred.

Upvotes: 2

Views: 603

Answers (1)

stuart
stuart

Reputation: 2273

I figured it out. (tested in chrome)

Paste the following in to your omnibox, then delete the first 'j' ("javascript:" gets erased w/out the double j)

Try this at http://www.w3schools.com/jsref/

jjavascript: 

var myxpath = "//a[@class='bigbtn']";

var iterator = document.evaluate(myxpath, document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null );

try {
    var thisNode = iterator.iterateNext();

    while (thisNode) {
        thisNode.style.outline = "5px dashed red";
        thisNode = iterator.iterateNext();
    }   
}
catch (e) {
    dump( 'Error: Document tree modified during iteration ' + e );
}

Upvotes: 2

Related Questions