Jump to content

User:Dreamy Jazz/sockpuppet-category-helper.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dreamy Jazz (talk | contribs) at 00:43, 23 January 2022 (test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/*
Script by Dreamy Jazz
Replaces userlinks in sockpuppet categories with uses of the checkuser template.
Version 1.2.1
*/
$(document).ready(function() {
	mw.loader.using( [
	'mediawiki.api',
	'mediawiki.util',
] ).then( async () => {
  if (mw.config.get('wgPageName').startsWith('Category:Wikipedia_sockpuppets_of') || mw.config.get('wgPageName').startsWith('Category:Suspected_Wikipedia_sockpuppets_of')) {
    $("div#mw-pages a.userlink").each(async function (index, element) {
    	let preExistingClasses = $(element).attr("class");
    	console.log(preExistingClasses);
		let text = $(element).text().replace("User:", "");
		if (mw.util.isIPAddress(text)) {
		  text = "{{checkip|" + text + "}}";
		} else {
		  text = "{{checkuser|" + text + "}}";
		}
		let value = await getParsedTemplateText(text);
		$(element).replaceWith(value);
		if (preExistingClasses !== undefined) {
			$(element).find('a').addClass(preExistingClasses);
		}
    });
  }
});
});

async function getParsedTemplateText(wikitext) {
	const api = new mw.Api();
	const response = await api.get({
		action: "parse",
		format: "json",
		prop: "text",
		text: wikitext,
		wrapoutputclass: "",
		disablelimitreport: 1,
		disableeditsection: 1,
		contentmodel: "wikitext"
	});
	return response.parse.text["*"];
}