User:Tollens/longCommenter.js

/*   To use this script, add the following line to your Special:MyPage/common.js:

importScript("User:Tollens/longCommenter.js"); // Backlink: User:Tollens/longCommenter.js

The button will be placed in the same menu as the "Move" button, which depending on the skin used could be called "More" or "Tools". It will only appear on article pages, and will not tag redirects or disambiguation pages (because they are excluded from Special:ShortPages), or any page that is   already 256 bytes or larger.

// if (sessionStorage.getItem("justAddedLongComment") === "true") { mw.notify("Long comment tag added successfully", { type: "success" }); sessionStorage.removeItem("justAddedLongComment"); }

$.when(mw.loader.using(["mediawiki.util"]), $.ready).done(function {	if (mw.config.get("wgNamespaceNumber") !== 0) return;

const api = new mw.Api; const currentPage = mw.config.get("wgPageName");

let button = mw.util.addPortletLink(		"p-cactions",		"#",		"Add long comment tag",		"ca-add-longcomment",		"Add long comment tag to this page"	);

if (button) { button.addEventListener("click", async function {			const apiResponse = ( await api.post({					action: "query",					titles: currentPage,					prop: "revisions|pageprops",					rvprop: "size",					ppprop: "disambiguation",					redirects: true				}) ).query;

const length = Object.values(apiResponse.pages)[0].revisions[0].size; const isRedirect = apiResponse.redirects !== undefined; const isDisambiguation = Object.values(apiResponse.pages)[0].pageprops?.disambiguation === "";

if (isRedirect) { mw.notify("Page is a redirect, will not tag", { type: "warn" }); return; }

if (isDisambiguation) { mw.notify("Page is a disambiguation, will not tag", { type: "warn" }); return; }

if (length >= 255) { mw.notify(`Page too long (${length} bytes), will not tag`, { type: "warn" }); return; }

await api.postWithToken("csrf", {				action: "edit",				title: currentPage,				minor: "yes",				summary:					"Adding long comment tag to prevent legitimately short page from listing at the top of Special:ShortPages (via script)",				appendtext: "\n"			});

sessionStorage.setItem("justAddedLongComment", "true");

window.location.reload; });	} }); //