Jump to content

User:JJPMaster/Scripts/masscat.js

From Wikipedia, the free encyclopedia
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.
var contentContent = document.getElementById("bodyContent");
if (mw.config.get("wgCanonicalNamespace") == "Category" || mw.config.get("wgCanonicalNamespace") == "Category talk") {
	mw.util.addPortletLink("p-tb", `/wiki/Special:MassCat?cat=${mw.config.get("wgTitle")}`, "Mass categorizer", "p-masscat", "Mass categorizer");
}
// Adapted from User:Ahecht/Scripts/massmove.js
function massCatGetArticles() {
	var articles = document.getElementById("wpMassCatPages").value.split("\n");
	var ret = [];
	var i, len;
	for (i = 0, len = articles.length; i < len; i++) {
		var s = articles[i];
		s = s.trim();
		if (s) {
			ret.push(s);
		}
	}
	return ret;
}

function massCatGetCat() {
	return document.getElementById("wpMassCatName").value;
}

function massCatPrepare(e) {
	e.preventDefault();
	var MCarts = confirm("Your articles are these, right?\n" + massCatGetArticles());
	var MCcat; 
	var MCfinal;
	if (MCarts) MCcat = confirm(`And you're adding them to ${massCatGetCat()}, right?`);
	if (MCcat) MCfinal = confirm("Are you sure you want to do this mass categorization?");
	if (MCfinal) doMassCat();
}

function doMassCat() {
	var params;
	massCatGetArticles().forEach((e) => {
		params = {
			action: "edit",
			title: e,
			appendtext: `\n[[Category:${massCatGetCat()}]]`,
			summary: "Mass categorization with [[User:JJPMaster/masscat.js]]"
		},
		api = new mw.Api();

		api.postWithToken( 'csrf', params ).done( function ( data ) {
			console.log( data );
			location.reload();
		} );

	});
}

$(function() {
	if(mw.config.get("wgPageName").toLowerCase() == "special:masscat") {
		document.title = "Mass categorizer - " + mw.config.get("wgSiteName");
		document.getElementById("firstHeading").innerText = "Mass categorizer";	
		// Adapted from [[User:Ahecht/Scripts/massmove.js]]
		contentContent.innerHTML = `<form id="wpMassCat" name="wpMassCat">
			<b>If you abuse this tool, it\'s <i>your</i> fault, not mine.</b>
			<div id="wpMassCatFailedContainer"></div>
			The name of the category:<br/>
			<input type="text" id="wpMassCatName" name="wpMassCatName" value="${new URLSearchParams(window.location.search).get("cat")}"/>
			<br /><br />
				Pages to add to the category (one on each line, please):<br />
					<textarea tabindex="1" accesskey="," name="wpMassCatPages" id="wpMassCatPages" rows="10" cols="80"></textarea>
			<input type="submit"/>
			</form>`;

	document.getElementById("wpMassCat").addEventListener("submit", massCatPrepare);	
	}
});