[Greasemonkey] XPath n00b help

Jim Roberts jimmyroberts at gmail.com
Sat Feb 11 22:16:53 EST 2006


On 2/11/06, Mitchell Mebane wrote:
> Hi there,
>
> I'm trying to create a Greasemonkey script to fix some annoyances with a
> company web app. Basically, I have some image gallery pages I'd like to
> extract info from.
>
> I've been able to get the links to the galleries pretty easy with
> //a[@href[contains(.,'viewgallery')]]
>
> However, I want to get the names of the galleries and image count, but
> this has been eluding me.
>
> The info is stored in blocks like so:
> <span class="galheader">
>     <strong>Item:</strong> MG-SP40<br>
>     <strong>Image count:</strong> 24<br>
> </span>
>
> There are 16 of these blocks per page. I'd like to end up with one
> snapshot containing the item numbers and one with the image counts.
>
> I've tried
> //strong[text()='Item'][following-sibling::text()]
> but I think I'm not understanding the syntax. How would I go about this?
>
> Thanks,
> Mitchell Mebane

The following should work based on your code snippet:

var

itemName = new Array(),

imgCount = new Array(),

itemNameNodes =
document.evaluate('//span[@class="galheader"]/strong[1]/following-sibling::text()[1]',
               document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null),

imgCountNodes =
document.evaluate('//span[@class="galheader"]/strong[2]/following-sibling::text()[1]',
               document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for (var i = 0; i < itemNameNodes.snapshotLength; i++) {

 var itemNameNode = itemNameNodes.snapshotItem(i);
 var imgCountNode = imgCountNodes.snapshotItem(i);

  // arrays containing the actual content of the text nodes.
   itemName[i] = itemNameNode.textContent;
   imgCount[i] = imgCountNode.textContent;

}

http://www.zvon.org/xxl/XPathTutorial/General/examples.html
http://www.w3schools.com/xpath/default.asp


More information about the Greasemonkey mailing list