Slash Boxes
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.
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(
    for (var i = 0; i < result.snapshotLength; i++) {

    • 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 jQuery, the entire DOM manipulation required would look something like this (untested!):

      var link_node = ( function() {
          var dist_url  = canonical_url();
          var dist_name = trim_url(dist_url);

          var link_label = [];
          for ( var k in new_links ) link_label.push( k );

          return link_label.sort(), function() {
                  .attr( 'href', new_links[ this ]( dist_url, dist_name ) )
                  .text( label );
          } );
      } )();

      jQuery( 'tr > td:nth-child(1)' )
          .filter( function() { return $( this ).text() == 'Links' } )
          .find( 'td:nth-child(2)' )
          .each( function() {
              var cell = $( this );
     link_node, function() {
                      .append( "\n[ " )
                      .append( this )
                      .append( " ]" );
              } );
          } );
      • Woops. Replace the remaining $() calls with jQuery(). I haven’t trained myself out of the habit completely yet.