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:20, 23 January 2022 (Dreamy Jazz moved page User:Dreamy Jazz/spi-category-checkuser-template-script.js to User:Dreamy Jazz/sockpuppet-category-helper.js: Better name). 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.0.0
*/
$(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')) {
    console.log("test: " + $("div#mw-pages a.userlink").text());
    $("div#mw-pages a.userlink").each(async function (index, element) {
      console.log($(element).hasClass("userlink"));
      let text = $(this).text();
      let value = await getParsedCheckuserTemplateText(text.replace("User:", ""));
      $(this).replaceWith(value);
      console.log("finished");
    });
  }
});
});

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