User:Anomie/linkclassifier.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. |
![]() | This user script seems to have a documentation page at User:Anomie/linkclassifier and an accompanying .css page at User:Anomie/linkclassifier.css. |
importScript('User:Anomie/util.js');
var LinkFixer={
/* This array lists "for deletion" categories */
cats:[
'Category:Articles for deletion',
'Category:Categories for deletion',
'Category:Images and media for deletion',
'Category:Miscellaneous pages for deletion',
'Category:Redirects for deletion',
'Category:User categories for discussion',
'Category:Wikipedia templates for deletion'
].sort(),
callback:function(r){
if(!r.query) throw new Error('Bad response');
r=r.query;
var a=document.getElementById('wikiPreview');
if(!a) a=document.getElementById('bodyContent');
if(!a) throw new Error('Huh? No body content?');
a=a.getElementsByTagName('A');
if(a.length==0) return;
var redir={};
if(r.redirects) for(var i=r.redirects.length-1; i>=0; i--){
redir[r.redirects[i].from]=r.redirects[i].to;
}
var cats={};
if(r.pages) for(var i in r.pages){
if(i<0 || typeof(r.pages[i].categories)=='undefined') continue;
cats[r.pages[i].title]=r.pages[i].categories.map(function(a){ return a.title; }).sort();
}
Array.forEach(a, function(a){
if(typeof(redir[a.title])!='undefined'){
addClass(a,'redirect');
a.title=redir[a.title];
}
if(typeof(cats[a.title])!='undefined'){
var c1=cats[a.title];
var i1=c1.length-1;
var c2=LinkFixer.cats;
var i2=c2.length-1;
while(i1>=0 && i2>=0){
if(c1[i1]==c2[i2]){
addClass(a,'deletion');
break;
}
(c1[i1]>c2[i2])?--i1:--i2;
}
}
});
},
onLoad:function(){
var a;
if(wgAction=='view'){
api({action:'query', titles:wgPageName, generator:'links', prop:'categories', redirects:1}, LinkFixer.callback);
} else if(document.getElementById("wikiPreview")){
var a=document.getElementById("wikiPreview").getElementsByTagName('A');
if(a.length==0) return;
a=Array.map(a, function(a){ return a.title; }).sort().filter(function(e,i,a){ return i==0 || a[i-1]!==e; });
api({action:'query', titles:a, prop:'categories', redirects:1}, LinkFixer.callback);
}
}
};
if(doneOnloadHook) LinkFixer.onLoad(); //if imported dynamically
else addOnloadHook(LinkFixer.onLoad);