Ovid (publius_ovidius) wrote,

Dear Lazyweb Techie Types

Got an annoying little problem. I wrote the following Greasemonkey script. What this does, when I go out to the Web site use.perl.org is ignore one particularly obnoxious user by simply erasing his comments.

// ==UserScript==
// @name           ignore.use.perl
// @namespace      http://publius-ovidius.livejournal.com/
// @description    Hide Annoying Users
// @include        http://use.perl.org/*
// ==/UserScript==

(function() {
    var user = 'some_user_name';
    var href = '//use.perl.org/~'+user+'/';

    var divs = document.evaluate(

    for ( var i=0; i < divs.snapshotLength; i++ ) {
        var node       = divs.snapshotItem(i).parentNode.parentNode.parentNode;
        node.innerHTML = '<p><strong>Ignoring '+user+' via GreaseMonkey</strong></p>';

In other words, until they restructure use.perl, I will never have to see that user's comments again. However, the code is annoying the heck out of me. I had to write:

        var node       = divs.snapshotItem(i).parentNode.parentNode.parentNode;

That's because of how the XPath is written:


Because I have to descend several levels deep into the node structure, I have to keep calling parentNode to walk back up the tree. With Perl regular expressions, we have 'positive look ahead assertions'. These allow me to match text which is followed by some other text, but ignore that other text for purposes of capturing data. If I had that with XPath, I could say "match any top level node followed by other nodes, but ignore those other nodes. Then I wouldn't have that awful series of parentNode calls. Is this possible in XPath?

Tags: perl, programming, technology
  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded