/* http://greasemonkey.mozdev.org */

// ==UserScript==
// @name            NYTimes permalinks
// @namespace       http://akkartik.name/greasemonkey
// @exclude         http://nytimes.blogspace.com/*
// @exclude         http://*reddit.com/*
// @description     Run NYTimes links through Aaron Swartz's link generator
// ==/UserScript==

/* Context:
 *   http://reddit.com/info/bkh2/comments
 *   http://features.reddit.com/info/bki0/comments */
function genlink(link){
    var x,t,i,j;
    t=link.replace(/[%]3A/ig,':').replace(/[%]2f/ig,'/');
    i=t.lastIndexOf('http://');
    if(i>0){
        t=t.substring(i);
        j=t.indexOf('&');
        if(j>0)
            t=t.substring(0,j);
    }
    return 'http://nytimes.blogspace.com/genlink?q='+t;
}

(function () {
     var external = document.links;
     for (var k=0; k<external.length; k++) {
          external[k].href = external[k].href.replace(/(^http:\/\/[^\/]*.nytimes.com.*)/, genlink("$1")) ;
          /* disable reddit's link redirection */
          external[k].removeAttribute('onmousedown');
     }
})();
