MediaWiki:Gadget-TalkPageManagement.js

/** * @Name           TalkPageManagement * @Version        v1.0 * @Author(s)      TheGoldenPatrik1 KockaAdmiralac * @Protect * @Description    Adds a button to quickly create and protect talk pages. * @Bugs           Banner notifs do not work. */ mw.loader.using(['mediawiki.api', 'mediawiki.Title']).then(function {   var config = mw.config.get([ 'wgArticleId', 'wgNamespaceNumber', 'wgPageName', 'wgUserGroups' ]),   token = mw.user.tokens.get('editToken');    if ( [0,4,6,8,10,12,14,112,116].indexOf(config.wgNamespaceNumber) === -1 || config.wgArticleId === 0 || !/sysop|content-moderator/.test(config.wgUserGroups.join) ) {       return;    }    var title = new mw.Title(config.wgPageName);    title.namespace += 1;    var titleText = title.getPrefixedText;    new mw.Api.get({ cb: Date.now, action: 'query', titles: titleText }).done(function(d) { if (d.error) { return; }       var pages = d.query.pages; if (!pages[-1]) { return; }       $('').append(            $('', { 'class': 'TalkButtonBubble', href: '#', id: 'ca-create', text: 'Create Talk' })       ).appendTo('.page-header__contribution-buttons .wds-list') .click(function {           $.ajax({ type: 'POST', url: mw.util.wikiScript('api'), dataType: 'json', data: { action: 'edit', title: titleText, summary: '+Talk', text: '', format: 'json', token: token }           }).done(function(data) { if (data.error) { new BannerNotification('An error occured while creating the page.', 'error').show; } else { new mw.Api.post({                   action: 'protect',                    expiry: 'indefinite',                    protections: 'move=sysop',                    watchlist: 'nochange',                    title: titleText,                    reason: 'Talk Page',                    token: token                    }) .done(function(data) {                    if (data.error) {                    new BannerNotification('An error occurred while protecting the page.', 'error').show;                } else {                    new BannerNotification('Talk page created and protected!', 'success').show;                    window.location.href = mw.util.getUrl(titleText);                    }                }); }       });    }); });});