User:MordantMaenad/scripts/contemplinks.js

// be warned: only works for first 500 revisions of nontalk page

if (wgNamespaceNumber == 1) { var name = wgTitle; } else { var name = new String(wgPageName); name = name.replace(/_talk/i, ""); } name = escape(name);

function genurl(id, list) { if (id == "null") { return wgServer + '/wiki/' + name; } for (var j in list) { if (eval(list[j]) < eval(id)) { return wgServer + wgScriptPath + "/index.php?title=" + name + "&oldid=" + list[j]; } } }

function genlist(data) { var pages = data.query.pages; var rlist = new Array; for (var n = 0; n < 500; n++) { for (var page in pages) { try { var oldid = pages[page].revisions[n].revid; } catch(err) { break; }   }    rlist.push(oldid); } var histlinkies = getElementsByClassName(document.getElementById('bodyContent'),"*","mw-history-histlinks"); var datelinks = getElementsByClassName(document.getElementById('bodyContent'),"*","history-user"); for (var j = 0; j< histlinkies.length; j++) { var addspan = document.createElement('span'); var adda = document.createElement('a'); var d = datelinks[j].parentNode.getElementsByTagName('a'); if (j != 0) { var idurl = new String(d[2].href); var idtarget = idurl.substr(idurl.length-6); } else { var idtarget = "null"; }   var contempurl = genurl(idtarget, rlist); adda.href = contempurl; adda.innerHTML = "contemp"; addspan.appendChild(document.createTextNode("("));   if (contempurl != undefined) {      addspan.appendChild(adda);    } else {      addspan.appendChild(document.createTextNode("contemp"));    }    addspan.appendChild(document.createTextNode(")")); addspan.className = "contemp-link"; histlinkies[j].parentNode.insertBefore(addspan,histlinkies[j].nextSibling); histlinkies[j].parentNode.insertBefore(document.createTextNode(addspan.nextSibling.textContent),addspan); } var last = document.getElementById("pagehistory").lastChild; if (last.getElementsByTagName("span")[1].className != "contemp-link") last.insertBefore(document.createTextNode("(contemp)"), last.getElementsByTagName("span")[0].nextSibling); }

function init_contemp { var t = wgCanonicalNamespace; if (t.search(/talk/i) != -1) { var u = new String(document.URL); var target = u.replace(/_talk/i, ""); var obt = document.createElement('script'); obt.src = wgServer + wgScriptPath + '/api.php?format=json&action=query&titles=' + name + '&prop=revisions&rvprop=ids|timestamp&rvlimit=500&callback=genlist'; document.body.appendChild(obt); } }

addOnloadHook(init_contemp);