User:TyA/createCharacterCategoryPage.js

/* * Create Character Category Page * * Creates a character's image category page in the form of Category:Images of $x by adding a button *  on the category page. * * @author TyA */

(function (window, $, mw) {   if( // CCCP == CreateCharacterCategoryPage window.CCCPLoaded || mw.config.get('wgArticleId') != 0 || // page exists mw.config.get('wgNamespaceNumber') != 14 || // Not in category: namespace mw.config.get('wgTitle').indexOf('Images of ') == -1 // Not an Images of $x category ) {       return;    }    window.CCCPLoaded = true;     var CCCP = {        init: function {            this.api = new mw.Api;            this.addButton;        },        onClick: function {            // We want to create the category page if clicked             var characterName = mw.config.get('wgTitle').replace('Images of ', '');            var pageText = 'This category contains images of ' + characterName + '.\n\n';            this.api.post({ action:'edit', title: mw.config.get('wgPageName'), token: mw.user.tokens.get('editToken'), bot:true, text: pageText, summary: 'creating category page for ' + characterName }).done(function(e) { if(e.error) { console.error(e); } else { // if we did it, reload to see our masterpiece window.location.reload; }           });// can add a .error for error catching here if needed later         },        addButton: function {            $('.page-header__contribution-buttons').prepend( $('', {                       'class': 'wds-is-squished wds-button',                        id: 'btn-create-category',                        title: 'Create Category',                        text: 'Create Category'                    }) );           $('#btn-create-category').click($.proxy(this.onClick, this));        }    };    mw.loader.using('mediawiki.api', $.proxy(CCCP.init, CCCP)); }(this, jQuery, mediaWiki));