User:Nux/CategorySortKeys.js
Appearance
< User:Nux
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:Nux/CategorySortKeys. |
/**
* CategorySortKeys.
*
* Show sort keys on category pages on Wikipedia (or other MediaWiki).
*
* Repository:
* https://github.com/Eccenux/wikiCategorySortKeys
*
* Authors:
* Maciej Nux Jaros
*
* <nowiki>
*/
var CategorySortKeys = class {
constructor() {
this.conf = {
missing: '__NN__', // TODO? move to CSS csk:empty{content:x}
};
}
/**
* Enhance current category page.
*/
async enhance() {
// Object { ns: 0, title: "Gordon Allan", sortkeyprefix: "Allan, Gordon" }
const data = await this.loadPage();
// mapit
const keys = {};
data.forEach(d => {
keys[d.title] = d.sortkeyprefix;
});
// append
document.querySelectorAll('.mw-category-group a').forEach((a)=>{
let title = a.textContent.trim();
let sortkey = (title in keys) ? keys[title] : '';
if (!sortkey.length) {
sortkey = this.conf.missing;
}
a.insertAdjacentHTML('afterend', ` <csk>(${sortkey})</csk>`);
});
}
/** @private Load keys from start to end letters. */
async loadPage() {
let {start, end} = this.findBounds();
console.log('loadPage: ', {start, end});
let data = await this.loadKeys(start, end);
return data;
}
/** @private Find start/end letter. */
findBounds() {
let letters = Array.from(document.querySelectorAll('.mw-category-group h3')).map(el => el.textContent);
if (!letters.length) {
return false;
}
let start = letters[0];
let end = letters.pop();
return {start, end};
}
/** @private Load keys from start to end letters. */
async loadKeys(start, end) {
return new Promise((resolve, reject) => {
const api = new mw.Api();
let props = {
action: 'query',
list: 'categorymembers',
format: 'json',
cmtitle: mw.config.get('wgPageName'),
cmsort: 'sortkey',
cmdir: 'asc',
cmprop: ['sortkeyprefix', 'title'],
cmlimit: 'max',
};
if (start && end) {
props['cmstartsortkeyprefix'] = `${start}`;
props['cmendsortkeyprefix'] = `${end}ZZZ`;
}
api.get(props).done(function (data) {
let members = data?.query?.categorymembers;
if (Array.isArray(members)) {
resolve(members)
} else {
reject({
e: 'invalid data',
data,
});
}
});
});
}
}
// init
if ( mw.config.get('wgCanonicalNamespace') === 'Category' ) {
mw.loader.using(['mediawiki.util'], function () {
// link
const title = 'Sort keys';
const desc = 'Show sort keys for pages in this category';
const link = mw.util.addPortletLink( 'p-tb', '', title, 'pt-sortkeys', desc, null, '#t-info' );
// action
$( link ).click( function ( e ) {
e.preventDefault();
var cats = new CategorySortKeys();
cats.enhance();
} );
} );
}
//</nowiki>