User:Erutuon/scripts/addAutoCat.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

Creates links immediately below the header that you can click to add {{autocat}} to empty category pages. Modified from User:Dixtosa/fillAutoCat.js.

To use this script, place this in your Special:MyPage/common.js:

importScript("User:Erutuon/scripts/addAutoCat.js") // Linkback: [[User:Erutuon/scripts/addAutoCat.js]]

By default, doesn't add an edit summary. To use the edit summary that links back to the function, place this in your common.js:

window.autoCatEditSummary = true;

/* jshint esversion: 6, undef: true, unused: true, varstmt: true */
/* globals $, mw, OO */
// <nowiki>

(function addAutoCatIIFE () {
"use strict";

if (mw.config.get("wgNamespaceNumber") !== 14)
	return;

const autoCatEditSummary = typeof window.autoCatEditSummary == "undefined"
	? false : window.autoCatEditSummary;

const $textBox = $("#wpTextbox1");
const $editSummaryBox = $("#wpSummary");

function fillAutoCat(autoSave) {
	$textBox.val("{{auto cat}}");
	if ( autoCatEditSummary )
		$editSummaryBox.val("added [[T:auto cat]] with [[User:Erutuon/scripts/addAutoCat.js|JavaScript]]");
	
	$(autoSave ? "#wpSave" : "#wpPreview").click();
}
	
const action = mw.config.get("wgAction");

mw.loader.using("oojs-ui").done(() => {
	if (mw.config.get("wgCurRevisionId") === 0) { // new page
		$(() => {
			const buttons = [];
			if (action === "edit") {
				const button1 = new OO.ui.ButtonWidget({
					label: "Add auto cat and save",
				});
				const button2 = new OO.ui.ButtonWidget({
					label: "or preview",
				});
				
				button1.$element
					.click(() => fillAutoCat(true))
					.attr("title", "Create this category using topic cat");
				button2.$element
					.click(() => fillAutoCat(false))
					.attr("title", "Create this category using topic cat");
				
				buttons.push(button1.$element);
				buttons.push(button2.$element);
			} else if (action === "view") {
				const button = new OO.ui.ButtonWidget({
					label: "Add auto cat and edit",
				});
				
				button.$element.click(() => {
					location.href = new mw.Uri(location.href).extend({
						action: "edit",
						preloadtext: "{{autocat}}",
					}).toString();
				});
				
				buttons.push(button.$element);
			}
			
			if (buttons.length > 0) {
				const div = $("<div>").append(buttons);
				
				$("#firstHeading").after(div);
			}
		});
	} else if (action === "edit" && !$textBox.val().includes("auto cat")) {
		$(() => {
			const button = new OO.ui.ButtonWidget({
				label: "Replace with autocat",
				id: "add-autocat",
			});
			button.$element.click(() => $textBox.val("{{auto cat}}"));
			
			$("#editform").prepend(button.$element);
		});
	}
});

})();

// </nowiki>