GreaseMonkey: Ignoring users on use.perl

Ovid on 2008-08-23T09:25:21

Just playing around. It's not overly useful, but it's nice to see it work (and it's fragile). Replace 'some_user_name' below to ignore the user of your choice. No, it doesn't have a lot of features, but it was fun to write :)

// ==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/'+user+'/';

    // I want zero-width positive look ahead assertions in XPath.  That
    // would eliminate the awful parentNode.parentNode.parentNode;
    // Does this feature exist and I just don't know it?

    var divs = document.evaluate(
        "//div[@class='full']/div/div[@class='details']/a[@href='"+href+"']",
        document,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null
    );

    for ( var i=0; i < divs.snapshotLength; i++ ) {
        var node       = divs.snapshotItem(i).parentNode.parentNode.parentNode;
        node.innerHTML = '

Ignoring '+user+' via GreaseMonkey

'; } })();


XPath has it

dstar on 2008-08-23T15:44:35

It's possible, but I'm not sure how to handle multiple predicates. For the simple case, it looks like this:

"div[/*/span[@class='storyicons']]"

which pulls out all the divs which have a grandchild which is a span with a class of 'storyicons'.

Re:XPath has it

Aristotle on 2008-08-24T02:23:38

You can put multiple predicates on a single path step. Something like /foo[@bar='baz'][@quux='qux'] is completely valid (and logically equivalent to /foo[@bar='baz' and @quux='qux']).

Re:XPath has it

dstar on 2008-08-24T03:22:59

ah!

So in the example above, it would be something like

"//div[@class='full'][.//div/div[@class='details']/a[@href='"+href+"']]"

(I think)

Re:XPath has it

Aristotle on 2008-08-24T03:49:29

Yes; or alternatively:

"//div[@class='full' and .//div/div[@class='details']/a[@href='"+href+"']]"

Re:XPath has it

Aristotle on 2008-08-24T03:52:36

Err, wait, no. Where did that .// in your second predicate come from? It would be either

"//div[@class='full'][div/div[@class='details']/a[@href='"+href+"']]"

or

"//div[@class='full' and div/div[@class='details']/a[@href='"+href+"']]"

You want those nested DIVs and As as direct child nodes of each other and of the node in question – you’re not looking for that subtree anywhere further down.

Re:XPath has it

dstar on 2008-08-24T04:42:26

Ah, okay -- I'm not that familiar with XPath yet.