User:Splarka/templatesabused.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Splarka/templatesabused. |
/* Templates Used top edit indicator, version [0.0.1a]
Originally from http://en.wikipedia.org/wiki/User:Splarka/templatesabused.js
What?:
* It adds the last editor, timestamp, diff, and comment for the last edit to the Template Used section of an edit window.
Why?:
* Dunno, someone requested it.
Notes:
* Works on preview, so it is necessarily dirty.
** It gets the list from scraping the page and normalizing links.
** It sets the li ID to escapeID(decodeURIComponent(link)).
** On API return, it matches up by matching those IDs with escapeID(title).
*** I think this works for all unicode/characters, needs testing (might break on ampersand, quotes, crap like that)
* Limited to first 50.
* Skips missing/red/ugly.
*/
if(wgAction == 'edit' || wgAction == 'submit') addOnloadHook(templateTopEdits)
function templateTopEdits() {
var docobj = document.getElementById('bodyContent') || document.getElementById('content') || document.getElementById('mw-content') || document.body;
var tu = getElementsByClassName(docobj,'div','templatesUsed');
if(tu.length == 0) return
tu = tu[0];
var ul = tu.getElementsByTagName('ul');
if(ul.length == 0) return
ul = ul[ul.length-1]; //always get the last one
li = ul.getElementsByTagName('li');
if(li.length == 0) return
//turn wgArticlePath into a regex to extract the link
var app = RegExp(wgArticlePath.replace(/([^a-zA-Z0-9])/g,'\\$1').replace(/\\\$1/g,'(.*)'),'i');
var tlinks = [];
for(var i=0;i<li.length;i++) {
a = li[i].getElementsByTagName('a')[0];
if(!a || !a.href) continue
var link = a.getAttribute('href',2);
if(!app.test(link) || link.indexOf('redlink') != -1) continue
link = link.replace(app,'$1');
li[i].setAttribute('id',escapeID(decodeURIComponent(link)));
link = link.replace(/_/g,'%20');
tlinks.push(link);
}
var url = wgScriptPath + '/api.php?action=query&format=json&callback=templateTopEditsCB&maxage=3600&smaxage=3600&prop=revisions&indexpageids&titles=' + tlinks.join('|')
mw.loader.load(url);
}
function templateTopEditsCB(obj) {
if(!obj['query'] || !obj['query']['pages'] || !obj['query']['pageids']) return
var ids = obj['query']['pageids'];
for(var i=0;i<ids.length;i++) {
if(parseInt([ids[i]]) < 0) continue
var page = obj['query']['pages'][ids[i]];
var title = page['title'];
if(!page['revisions']) continue
var rev = page['revisions'][0];
var li = document.getElementById(escapeID(title));
if(!li) continue
li.appendChild(document.createElement('br'));
li.appendChild(document.createTextNode('Last edited: ' + rev['timestamp'].replace(/[A-Z]/g,' ') + ' by '));
addLinkChild(li, wgScript + '?title=Special:Contributions/' + encodeURIComponent(rev['user']),rev['user']);
li.appendChild(document.createTextNode(' ('));
addLinkChild(li, wgScript + '?diff=' + rev['revid'],'diff',false,false,'diff=' + rev['revid']);
li.appendChild(document.createTextNode(') '));
if(rev['comment']) li.appendChild(document.createTextNode(rev['comment']))
}
}
function addLinkChild(obj,href,text,id,classes,title) {
if(!obj || !href || !text) return false;
var a = document.createElement('a');
a.setAttribute('href',href);
a.appendChild(document.createTextNode(text));
if(id) a.setAttribute('id',id);
if(classes) a.setAttribute('class',classes);
if(title) a.setAttribute('title',title);
obj.appendChild(a);
return a;
}
function escapeID(txt) {
var id = txt;
id = id.replace(/ /g,'_');
id = encodeURIComponent(id);
id = id.replace(/\%3A/g,':');
id = id.replace(/\%/g,'.');
return id;
}