Jump to content

User:Nux/CategorySortKeys.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nux (talk | contribs) at 00:41, 29 June 2023 (#Wikiploy v0.0.1: test version). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * Autocorrection features in Visual Editor.
 * 
 * Version history and technical docs:
 *		https://github.com/Eccenux/veAutocorrect
 * 
 * Authors: Maciej Nux Jaros.
 * 
 * <nowiki>
 */
var CategorySortKeys = class {
	constructor() {}

	/** @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['cmstartsortkeyprefix'] = `${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
					});
				}
			});
		});
	}
}

// temp
async function test() {
	var cats = new CategorySortKeys();

	var data = await cats.loadKeys();
	console.log( data );

	var data = await cats.loadPage();
	console.log(data);

	return cats;
}

if (mw.config.get('wgCanonicalNamespace') === 'Category') {
	$(function(){
		test();
	});
}

//</nowiki>