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.4; alpha release
importScript("User:R'n'B/wrappi.js");
// create a private namespace for this script
if (! mw.RnB) {
mw.RnB = {};
}
// Utility functions
mw.RnB.titleUrl = function (title) {
var normalized = mw.util.wikiUrlencode(title);
normalized = normalized.slice(0,1).toUpperCase() + normalized.slice(1);
return normalized.replace(/%21/g, "!").replace(/%24/g, "$").
// .replace(/'/g, "%27")
replace(/%28/g, "(").replace(/%29/g, ")").
replace(/%2A/g, "*").replace(/%2C/g, ",").
replace(/%3B/g, ";").replace(/%40/g, "@");
};
mw.RnB.log = function (arg) {
if (typeof console !== "undefined") {
console.log(arg);
} else {
mw.log(arg); // does nothing unless page is loaded in debug mode
}
};
// dabtag script
mw.RnB.dabtag = {
initialize: function () {
var app = mw.RnB.dabtag; //shortcut
if (! mw.RnB.Wiki) {
setTimeout(app.initialize, 100);
return;
}
app.api = new mw.RnB.Wiki(),
// get the list of disambig templates
app.api.request(
// query parameters
{ 'action': 'query',
'prop': 'links',
'titles': 'MediaWiki:Disambiguationspage',
'plnamespace': '10',
'pllimit': 'max'
},
// callback upon response
function (data) {
var link, pageid, thispage;
app.dabTemplates = [];
for (pageid in data.query.pages) {
if (data.query.pages.hasOwnProperty(pageid)) {
thispage = data.query.pages[pageid];
for (link in thispage.links) {
if (thispage.links.hasOwnProperty(link)) {
app.dabTemplates.push(
thispage.links[link].title);
}
}
}
}
// set document ready function
jQuery(function() {
var myDiv = document.createElement('div');
myDiv.appendChild(
document.createTextNode('dabtag: loading'));
myDiv.id = "colorizerStatus";
jQuery('#siteSub').after(myDiv);
mw.RnB.statusDiv = jQuery('#colorizerStatus');
mw.RnB.statusDiv.css({ 'display': 'block',
'font-size': '92%',
'font-weight': 'normal'
});
app.api.request(app.params, app.botCallback);
mw.RnB.statusDiv.text("dabcolorizer: waiting for data");
});
}
);
},
doHighlighting: function (pagetitle) {
// finds all links on the dabpage to the target page, and
// changes their CSS class
var urltitle = 'a[href="/wiki/' + mw.RnB.titleUrl(pagetitle) + '"]',
urlsection = 'a[href^="/wiki/' + mw.RnB.titleUrl(pagetitle) + '#"]';
mw.RnB.log("Looking for " + urltitle);
jQuery(urltitle + "," + urlsection).addClass(
pagetitle.indexOf("(disambiguation)") === -1 ?
"dabpagelink" : "intdablink");
},
params: { // API query parameters for page queries
'action': 'query',
'generator': 'links',
'prop': 'info|revisions|templates',
'gplnamespace': '0',
'gpllimit': 'max',
'titles': mw.config.get("wgPageName"),
'tllimit': 'max',
'redirects': ''
},
pagelinkdata: {}, // holds results from API response
ready: false,
redirectsFromTo: {}, // maps redirects to targets, from API response
redirectsToFrom: {}, // maps targets to redirects, from API response
botCallback: function (linkdata) {
// process results of the last API query, continue if necessary,
// and change CSS class of found links to disambig pages
var app = mw.RnB.dabtag, //shortcut
isDisambig,
page,
pageid,
pqr,
qc,
r,
tmpl;
if (! linkdata.query) {
mw.RnB.log("Page link query failed!");
return;
}
qc = linkdata["query-continue"];
app.queryContinue = qc; /* for debugging */
if (qc) {
// query-continue was set, so launch another query
if (qc.templates) {
app.params.tlcontinue = qc.templates.tlcontinue;
delete app.params.gplcontinue;
} else if (qc.links) {
delete app.params.tlcontinue;
app.params.gplcontinue = qc.links.tlcontinue;
}
app.api.request(app.params, app.botCallback);
}
mw.RnB.statusDiv.text("dabcolorizer: working");
// save any redirects for future reference
pqr = linkdata.query.redirects;
if (pqr !== undefined) {
for (r=0; r < pqr.length; r++) {
app.redirectsFromTo[ pqr[r].from ] = pqr[r].to;
app.redirectsToFrom[ pqr[r].to ] = pqr[r].from;
}
}
for (pageid in linkdata.query.pages) {
if (linkdata.query.pages.hasOwnProperty(pageid)) {
page = linkdata.query.pages[pageid];
if (app.pagelinkdata.hasOwnProperty(pageid)) {
// this link already loaded, so see if there are
// any new templates
if (page.hasOwnProperty("templates")) {
if (app.pagelinkdata[pageid]
.hasOwnProperty("templates")) {
jQuery.merge(app.pagelinkdata[pageid].templates,
page.templates);
} else {
app.pagelinkdata[pageid].templates = page.templates;
}
}
} else {
// first time we've seen this link
app.pagelinkdata[pageid] = page;
mw.RnB.log("Saving data for page " + page.title);
}
// skip red links
if (pageid < 0) {
mw.RnB.log("Skipping redlink " + page.title);
continue;
}
// skip links we have already identified as disambigs
if (page.hasOwnProperty("known_dab")) {
mw.RnB.log("Skipping known dab " + page.title);
continue;
}
// see if this link contains any templates
if (page.hasOwnProperty("templates")) {
mw.RnB.log("Checking templates on page " + page.title);
// check templates to see if this is a disambig page
isDisambig = false;
for (tmpl=0; tmpl < page.templates.length; tmpl++) {
if (app.dabTemplates
.indexOf(page.templates[tmpl].title) !== -1) {
isDisambig = true;
break;
}
}
if (isDisambig) {
page.known_dab = true;
// find the link element(s) that match the title
app.doHighlighting(page.title);
// if page is target of a redirect, also highlight links
// to the redirect
if (app.redirectsToFrom[page.title]) {
app.doHighlighting(app.redirectsToFrom[page.title]);
}
}
}
}
}
if (qc) { // query is being continued
mw.RnB.statusDiv.text("dabcolorizer: waiting for data");
}
else { // query was not continued, we're done
mw.RnB.statusDiv.text("dabcolorizer: done!");
mw.RnB.statusDiv.fadeOut('slow');
}
}
};
mw.RnB.action = mw.config.get("wgAction");
if (mw.config.get("wgIsArticle") || jQuery("#wikiPreview").length > 0) {
mw.RnB.dabtag.initialize();
}