citizen428.blog()

Try to learn something about everything

XPath (Good|mad)ness

Today I started writing a useful little Greasemonkey userscript for a browser-based MMRPG I’m playing. Although I’m no JavaScript guru I feel somewhat confident in the language and am quite fond of XPath ever since I joined the hoodwink.d community. Everything was going well, until I kind of hit a road block: selecting images whose src attribute contains a certain directory (e.g. “funky”) from a frame within a frameset. What sounds trivial at first cost me close to one and a half hours, but luckily enough I finally found a solution. To spare you the hassle, here’s the working result:

1
2
3
4
5
6

1
2
3
4
5
6
<span class='line'>var xpath = "/html/frameset/frame[@name='main']";
</span><span class='line'>var frameResult = document.evaluate(xpath, document, null, 
</span><span class='line'>   XPathResult.ANY_UNORDERED_NODE_TYPE,null).singleNodeValue;
</span><span class='line'>var document2 = frameResult.contentDocument;
</span><span class='line'>var imgResult = document2.evaluate("//img[contains(@src,'funky')]", document2, null, 
</span><span class='line'>  XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);</span>

Of course if any of you know sbout a better way to achieve the same result, please don’t hesitate to post a comment, thanks!

On a completely unrelated note: I’m probably the last person to find out that Slashdot features random Futurama quotes in its HTTP headers, but nonetheless I find it pretty cool to find stuff like “X-Fry: The less fortunate get all the breaks!” in there!

Setting Orange, Chaos 5, 3173 YOLD

Comments

Copyright © 2016 - Michael Kohl - Powered by Octopress