User:DVRTed/CategoryTree-mode-all.js
Appearance
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.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:DVRTed/CategoryTree-mode-all.
// <nowiki>
/* adds a button in category pages to display
the output of {{Category tree all|mode=all}} for the current category */
/* global mw, $ */
mw.loader.using(["mediawiki.api", "mediawiki.ui.button"], () => {
if (mw.config.get("wgNamespaceNumber") !== 14) return;
const pagename = mw.config.get("wgPageName");
const container = $("<div>").css({ margin: "12px 4px" });
const button = $("<button>")
.addClass("mw-ui-button mw-ui-progressive")
.html("Display {{Category tree all}} (mode=all)")
.on("click", () => {
button.prop("disabled", true);
new mw.Api()
.get({
action: "parse",
prop: "text",
contentmodel: "wikitext",
text: `{{category tree all|${pagename}|mode=all}}`,
})
.done((data) => {
container.html(data?.parse?.text?.["*"] || "<em>(no output)</em>");
mw.hook("wikipage.content").fire(container);
})
.fail(() => {
container.html("<em>Failed to load.</em>");
button.prop("disabled", false);
});
});
$("#mw-content-text").prepend(container).prepend(button);
});
// </nowiki>