MediaWiki:AddArticleToCategory/code.js

mw.loader.using('jquery.autocomplete', function {   var config = mw.config.get([ 'wgNamespaceNumber', 'wgTitle' ]);   if (config.wgNamespaceNumber !== 14) {        return;    }    function callback {        var $el = $('#article-search').autocomplete({ serviceUrl: mw.util.wikiScript('api') + '?action=opensearch', appendTo: $('#article-search-form'), deferRequestBy: 250, maxHeight: 1000, queryParamName: 'search', selectedClass: 'selected', width: '270px', namespace: 0, fnPreprocessResults: function(response) { response.query = response[0]; response.suggestions = response[1]; response.data = response[1]; return response; },               onSelect: function(value, data, event) { appendCategory(value,false); }           }, function(e) { console.error(e); });           $('.add-to-new').click(function(e) { e.preventDefault; appendCategory($('#article-search-form #article-search').val,true); });   }    function click {        $.showCustomModal( 'Choose Article', $(' ').append(               $(' ', { type: 'text', id: 'article-search', style: 'width: 100%' }),               $(' '),                $('', { 'class': 'wds-button wds-is-secondary wds-is-squished add-to-new', html: '   &emsp;' }).append( $(' ', { text: 'Insert into new article' }) ).click(function { appendCategory($('#article-search-form #article-search').val,true); })           ).html, {               id: 'article-search-form', callback: callback, buttons: [{ message: 'Cancel', handler: function { $('#article-search-form').closeModal; },                   id: 'article-search-cancel', defaultButton: true, }]           }        );    }    $('.page-header__contribution-buttons .wds-dropdown .wds-list').append( $('').append(           $('', { id: 'ca-add2cat', text: 'Add to category' }).click(click)       ) ); });

function appendCategory(page,isNew,callback) { $.post('/api.php',{		action: 'edit',		title: page,		summary: 'Add category',		appendtext:  + wgPageName + ,		[isNew ? 'createonly' : 'nocreate']: true,		token: mw.user.tokens.get('editToken'),		format: 'json'   },function {		if(isNew) {            window.location.href = mw.util.getUrl(page, { action: 'edit' });       }		else {			window.location.reload;        }    },'json'); }