My first grease monkey script [citation needed]

For those of you that have looked up/browsed/etc. anything on Wikipedia lately, you've probably noticed the "citation Nazis"--I hate citation Nazis (movie?)--have taken over. There are some pages that have the "[citation needed]" tag on almost every sentence. With that in mind, I wrote my first Greasemonkey script this morning.

With the power of Greasemonkey (Firefoxonly), you no longer have to be annoyed by all of the clutter. The tags are completely removed from the page. First install Greasemonkey, and then click the link below to install the script. The source and an explanation are in the extended entry.

Download script

Here's the source of the script if you are interested:

// ==UserScript==
// @name            No Wikipedia Citation Needed
// @namespace       http://marcolson.net
// @description     Gets rid of [citation needed] in wikipedia
// @include         http://*.wikipedia.org/wiki/*
// ==/UserScript==

var anodes, node, supnode, parentnode;

anodes = document.evaluate("//a[@title='Wikipedia:Citing sources']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for (var i = 0; i < anodes.snapshotLength; i++) {
node = anodes.snapshotItem(i);
supnode = node.parentNode;
parentnode = supnode.parentNode;
parentnode.removeChild(supnode);
}

document.normalize();


Basically, we do 3 things:


  1. Get a list of all of the nodes that have a citation title
  2. Traverse that list and remove the parents (the superscript node) from the document
  3. "Normalize" the document to combine the fragmented text nodes

The first step is to get a list of all of the anchor nodes that contain a "Citation Needed" tag. If we look at the source of the page, we notice that they all have a common title ("Wikipedia:Citing sources") so we look for all anchor nodes with that title with our nifty xpath expression: //a[@title='Wikipedia:Citing sources']

anodes = document.evaluate("//a[@title='Wikipedia:Citing sources']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

The next step is to traverse that list, get the parent of the parent. The firefox dom works in such a way that to remove a node, you need to know the parent and do a removeChild() operation. We also don't want to leave the superscript node in place, so that's why we get the parent of the parent--it's cleaner this way.

for (var i = 0; i < anodes.snapshotLength; i++) {
    node = anodes.snapshotItem(i);
    supnode = node.parentNode;
    parentnode = supnode.parentNode;
    parentnode.removeChild(supnode);
}

The final step is just a bit of housekeeping. We want to clean up the dom, so we combine any fragmented text sections.

document.normalize();

And that's it! [citation needed]

March 2013

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            

About this Entry

This page contains a single entry by Marc published on June 27, 2007 11:15 AM.

Why are you protecting me ... from me? was the previous entry in this blog.

Links for 01 Jul 2007 [del.icio.us] is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.21-en