Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I prefer using XPath expressions when I can, for much the same reason that I prefer using regular expressions to building my own pastern matching code. To get the same results as your DOM code I would use the following.

    var result = document.evaluate(
        '//tr[td[last()=2]][td[1]/text()="Links"]/td[2]',
        document,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null
    );
    for (var i = 0; i < result.snapshotLength; i++) {
        add_links(result.snapshotItem(i).firstChild);
    }
    If I could assume that there is only one desired node and I that would not need the restriction that the number of cells in the row be exactly 2, I would use this.

    var result = document.evaluate(
        '//tr[td[1]/text()="Links"]/td[2]',
        document,
        null,
        XPathResult.FIRST_ORDERED_NODE_TYPE,
        null
    );
    var cell = result.singleNodeValue;
    if (cell) {
        add_links(cell.firstChild);
    }
    document.evaluate is not available in all browsers so I would not use it in general purpose code, I think that using it in a userscript is safe though.
    • I think that using it in a userscript is safe though.

      Yes, it’s Firefox (and maybe Opera) only, which supports XPath.

      so I would not use it in general purpose code

      Indeed; in general purpose code you would use jQuery (or some other DOM query library of your preference) to write the same thing with CSS3 selectors. This particular case is not as concise as the XPath version because it needs to check text content, which CSS largely has no means to do.

      For comparison’s sake, if written in jQu

      • Woops. Replace the remaining $() calls with jQuery(). I haven’t trained myself out of the habit completely yet.

    • Thanks for that - much tidier. I'm playing with HTTP::Proxy - in fact working on HTTP::Proxy::GreaseMonkey at the moment so I think I'll leave my version the same until I find out whether Safari does XPath. I'm guessing it doesn't. The current script is now working in Safari for me :)