User:Deathlime/Deredirectification

From Wikipedia, the free encyclopedia

/*

[edit] Installation directions

  1. Click the direct link: http://en.wikipedia.org/w/index.php?title=User:Deathlime/Deredirectification/script&action=raw&ctype=text/javascript
  2. Go to Tools>Install This User Script…

[edit] What? Why? Where?

This script was written because it bugged me that the only way to tell what the heck [[WP:|WP:*]] links actually linked to was to click them. Now I can hover my mouse for a second or so and the *real* link target will pop up. It works on any redirect that matches /^[A-Z][A-Za-z]{0,2}:/.

As far as I know, this should work automagically on all mediawikis that have the Query API, but I've only tested on "en.wikipedia.org". The regex for filtering titles would probably have to change. For more information about the Query API, check out http://en.wikipedia.org/wiki/User:Yurik/Query_API and http://meta.wikimedia.org/wiki/Query

Hakuna matata.

[edit] Code

*/
 // ==UserScript==
 // @name         WP:Deredirectification
 // @description  Changes the text of the title of hyperlinks that are redirects to the title of the target of the redirect :
 // @include      http://*.wikipedia.org/wiki/*
 // @include      http://*.wikimedia.org/wiki/*
 // ==/UserScript==
 
 // extracted from json.js (http://www.json.org/json.js )
 //    which is Copyright © 2002 JSON.org (go read http://www.json.org/license.html )
 String.prototype.parseJSON = function () {
     try {
         return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                 this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
             eval('(' + this + ')');
     } catch (e) {
         return false;
     }
 };
 
 var logging = 0; //increment for GM_log diarrhoea!
 
 var titles = [];
 var A = document.getElementsByTagName("A");
 for (var i = 0; i < A.length; i++)
     if ((/^[A-Z][A-Za-z]?:/.test(A[i].title)) && (titles.indexOf(A[i].title) < 0))
         titles.push(A[i].title);
 titles.sort(); //not necessary, but pretty! :
 if (logging > 0) {GM_log(titles.length + "| " + titles);}
 
 // this is the onload callback function for the GM_xmlhttpRequest
 function BANZAI(response)
 {
     if (logging > 0) {GM_log("status: " + response.status);}
     if (logging > 1) {GM_log("status text: " + response.statusText);}
     if (logging > 1) {GM_log("response headers:\n" + response.responseHeaders);}
     if (logging > 1) {GM_log("response text:\n" + response.responseText);}
     if (response.status != 200)
         throw ("You Have Angered The Cabal!\n"
             + "Try Again In " + response.status + " Years.");
     var yurik = response.responseText.parseJSON();
     if (yurik == false)
         throw ("I Smell A Wumpus!\n"
             + "Response Isn't JSON.");
     if (yurik.error)
         throw ("Beware Of Developers Wielding Cluebats!\n"
             + "Your Error Was \"" + yurik.error.* + "\".");
     var redirects = {};
     for (var id in yurik.pages)
     {
         var title = yurik.pages[id].title;
         if (yurik.pages[id].redirect != undefined && yurik.pages[id].redirect.to != undefined)
         {
             var redirect = yurik.pages[id].redirect.to.*;
             redirects[title] = redirect;
             if (logging > 2) {GM_log(title + " ’ " + redirect);}
         }
     }
     var A = document.getElementsByTagName("A");
     for (var i = 0; i < A.length; i++)
         if (redirects[A[i].title] != undefined)
             A[i].title = redirects[A[i].title];
 }
 
 var URL = "http://" + document.domain + "/w/query.php" //query the right server!
 URL += "?noprofile"; //Don't care
 URL += "&format=json"; //JSON FTW
 URL += "&what=redirects"; //important
 URL += "&titles="; //This gets tacked on later ´
 
 if (titles.length > 0) //avoid an empty query
 {
     //OK, so WP:WP has like 432 redirect links on it,
     // which makes shit fly when you stuff all that into one query
     //Soooo& now we split large queries into chunks and make multiple requests
     var chunk = 100;
     for (var k = 0; k < titles.length; k += chunk)
     {
         var request = {method: "GET", onload: BANZAI};
         request.headers = {
             "Accept": "application/json",
             };
         request.url = encodeURI(URL + titles.slice(k, k + chunk).join('|'))
         if (logging > 1) {GM_log(request.url );}
         GM_xmlhttpRequest(request);
     }
 }
 /**/