User:R'n'B/dabcolorizer.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:R'n'B/dabcolorizer. |
// Version 0.2; alpha release
importScript("User:Luasóg bot/framework.js");
$.urlifytitle = function ( title ){
var urlified = mw.util.wikiUrlencode(title);
urlified = urlified.slice(0,1).toUpperCase() + urlified.slice(1);
return urlified.replace(/'/, "%27").replace(/%21/, "!").
replace(/%2C/, ",").
replace(/%28/, "(").replace(/%29/, ")");
};
$.startcolorizer = function (){
var pageid,
pageobj,
isdisambig,
template,
linktitle,
urltitle;
if( ! $.botReady ) {
setTimeout($.startcolorizer, 125);
return;
}
mw.log("Starting colorizer.");
for( pageid in $.botPagelinkdata ){
pageobj = $.botPagelinkdata[pageid];
// check templates to see if this is a disambig page
isdisambig = false;
if( pageobj.templates ){
for( template in pageobj.templates ){
if( $.disambigtemplates.indexOf(
pageobj.templates[template].title) !== -1 ){
isdisambig = true;
break;
}
}
}
if( isdisambig ){
// find the link element(s) that match the title
// if pageobj is target of a redirect, use the redirect
// as the link to highlight
linktitle = $.redirectsToFrom[pageobj.title] ?
$.redirectsToFrom[pageobj.title] :
pageobj.title;
urltitle = 'a[href="/wiki/' + $.urlifytitle( linktitle ) + '"]';
mw.log("Looking for " + urltitle);
$(urltitle).addClass(linktitle.indexOf("(disambiguation)") === -1 ?
"dabpagelink" : "intdablink");
}
}
};
$.botInitialize = function (){
// make sure framework has been loaded
if (! Luasog) {
setTimeout($.botInitialize, 125);
return;
}
// wrapper for Luasog bot request method
Luasog.prototype.raw_request = Luasog.prototype.request;
// wraps request with handling of maxlag
Luasog.prototype.request = function (__luas_args, __luas_callback){
__luas_args.maxlag = "5";
var lagpattern = /Waiting for [\d.]+: (\d+) seconds? lagged/;
var __luas_this = this;
this.raw_request(__luas_args, function (response_data){
// callback here once data is received
var lagmatch, lagtime;
if( response_data.error ){
if( response_data.error.code === "maxlag" ){
lagmatch = lagpattern.exec(response_data.error.info);
lagtime = parseInt(lagmatch[1], 10);
// set throttle into the future for lag delay
__luas_this.timeoflastrequest = ((new Date()).getTime()
+ Math.min(Math.max(5000, 500*lagtime), 120000));
// repeat request
__luas_this.request(__luas_args, __luas_callback);
} else {
Luasog.exception({ name: response_data.error.code,
message: response_data.error.info });
}
} else {
// no error code, go to original callback
__luas_callback(response_data);
}
});
};
// initialize the bot
$.bot = new Luasog(mw.config.get("wgServer")
+ mw.config.get("wgScriptPath") + "/api.php");
$.bot.speed = 10;
// get the list of disambig templates
$.bot.request(
{'action':'query', 'prop':'links',
'titles':'MediaWiki:Disambiguationspage', 'plnamespace':'10',
'pllimit':'max'},
function (data){
var link, pageid, thispage;
$.disambigtemplates = [];
for (pageid in data.query.pages) {
thispage = data.query.pages[pageid];
for (link in thispage.links) {
$.disambigtemplates.push( thispage.links[link].title );
}
}
}
);
// get the text of all pages linked from the current page;
// throttle should ensure that previous request will be completed
// before this one starts
$.botParams = {'action':'query', 'generator':'links',
'prop':'info|revisions|templates', 'gplnamespace':'0',
'gpllimit':'max', 'titles':mw.config.get("wgPageName"),
'tllimit':'max', 'redirects':''};
$.botPagelinkdata = {};
$.botReady = false;
$.redirectsFromTo = {};
$.redirectsToFrom = {};
$.botCallback = function (linkdata){
var page,
pageTemplates,
savedTemplates,
ttitle,
newT,
oldT,
knownT,
pqr,
qc,
r;
if ( ! linkdata.query) {
mw.log( "Page link query failed!" );
return;
}
for( page in linkdata.query.pages ){
if( $.botPagelinkdata[page] ){
// this page already loaded, so see if there are
// any new templates
pageTemplates = linkdata.query.pages[page].templates;
savedTemplates = $.botPagelinkdata[page].templates;
if (pageTemplates) {
for( newT in pageTemplates ){
knownT = false;
ttitle = pageTemplates[newT].title;
if( savedTemplates ){
for( oldT in savedTemplates ){
if( savedTemplates[oldT].title === ttitle) {
knownT = true;
break;
}
}
}
if( ! knownT ){
if (savedTemplates) {
savedTemplates.push(pageTemplates[newT]);
} else {
$.botPagelinkdata[page].templates =
[pageTemplates[newT]];
savedTemplates =
$.botPagelinkdata[page].templates;
}
}
}
}
} else {
$.botPagelinkdata[page] = linkdata.query.pages[page];
}
}
pqr = linkdata.query.redirects;
for( r in pqr ){
$.redirectsFromTo[ pqr[r].from ] = pqr[r].to;
$.redirectsToFrom[ pqr[r].to ] = pqr[r].from;
}
qc = linkdata["query-continue"];
if( qc ){
if( qc.templates ) {
$.botParams.tlcontinue = qc.templates.tlcontinue;
delete $.botParams.gplcontinue;
$.bot.request( $.botParams, $.botCallback );
} else if( qc.links ) {
delete $.botParams.tlcontinue;
$.botParams.gplcontinue = qc.links.tlcontinue;
$.bot.request( $.botParams, $.botCallback );
}
} else {
delete $.botParams.tlcontinue;
delete $.botParams.gplcontinue;
$.botReady = true;
}
};
$.bot.request( $.botParams, $.botCallback );
}
if ( mw.config.get("wgIsArticle") ){
$.botInitialize();
// wait until DOM is ready
$('document').ready($.startcolorizer);
}