Jump to content

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

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.3.5
*/
$(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[href^='/wiki/User:']").each(async function (index, element) {
		let text = $(element).text().replace("User:", "");
		if (mw.util.isIPAddress(text)) {
		  text = "{{checkip|" + text + "}}";
		} else {
		  text = "{{checkuser|" + text + "}}";
		}
		let value = await getParsedTemplateText(text);
		let $new = $(value);
		$(element).parent().prepend($new);
		$(element).attr("style", "display:none");
		if ($(element).attr("class") !== undefined) {
			$new.find('.cuEntry .plainlinks:nth-child(1) a').addClass($(element).attr("class"));
		}
    });
  }
});
});

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["*"];
}