User:1234qwer1234qwer4/BookCat.js

// based on https://en.wikipedia.org/wiki/User:DannyS712/Draft_no_cat.js // Set `var bookCatAJAX = true;` to add BookCat without reloading the page $( => { const bookCat = {}; window.bookCat = bookCat; bookCat.config = {	name: 'BookCat.js',	debug: false };

bookCat.summary = "Added using " + bookCat.config.name; bookCat.run = function { var editSummary = bookCat.summary; if ( bookCat.config.debug ) { console.log ( editSummary ); }	var api = new mw.Api; api.get( {		action: 'query',		titles: mw.config.get( 'wgPageName' ),		prop: 'revisions',		rvprop: 'content',		rvslots: 'main',		formatversion: 2	} ).done(		function ( response ) {			console.log( response );			var text = response.query.pages[0].revisions[0].slots.main.content;			if(text.match(/\{\{\s*Bookcat\s*\}\}/i)){				return mw.notify("BookCat already present.");			}			text += '\n\n';			api.postWithEditToken( { action: 'edit', minor: true, title: mw.config.get( 'wgPageName' ), text: text, summary: editSummary } ).done( function { if(bookCatAJAX === undefined || bookCatAJAX == false){ location.reload; }					else mw.notify("BookCat successfully added."); }			);		}	); };

});

$( document ).ready( => {	if ( mw.config.get( 'wgNamespaceNumber' ) === 0 && mw.config.get('wgAction') === 'view' && mw.config.get('wgCategories').every(e => !e.includes("Book:")) ) {		mw.loader.using( [ 'mediawiki.util' ], function { var link = mw.util.addPortletLink( 'p-cactions', '#', 'BookCat', 'ca-bookcat', 'Add BookCat'); $( link ).click( function ( event ) {					event.preventDefault;					mw.loader.using( 'mediawiki.api', window.bookCat.run );				} ); }		);	} } );