User:DannyS712/TeahouseTB.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//Copied from [[User:Abelmoschus Esculentus/TeahouseTB.js]
//<nowiki>
$(function() {
    var namespace = mw.config.get('wgNamespaceNumber'),
    pageName = mw.config.get('wgPageName'),
    myUserName = mw.config.get('wgUserName'),
    targetUserName = mw.config.get('wgRelevantUserName');
    api = new mw.Api();
    mw.loader.using( ['mediawiki.util'], function() {
        if([-1,0,2,3].indexOf(namespace) !== -1 && myUserName != targetUserName && targetUserName != null) {
            mw.util.addPortletLink(
                'p-cactions', 'javascript:void(0)',
                'Teahouse Talkback',
                'aca-tb', 'Leave a message to the user when you have answered their question'
            );
            $('#aca-tb').on('click', function() {
                var section = prompt( "Section (leave blank if you are too lazy to type it):                              ","");
                if (section != null) {
                    templateUser(section);
                }
            });
        }
    });
    function templateUser(section) {
        api.postWithToken( "edit", {
            action: "edit",
            section: 'new',
            watchlist: "unwatch",
            sectiontitle: "Teahouse talkback: you've got messages!",
            summary: "Notifying user ([[User:DannyS712/TeahouseTB|THTB]])",
            text: "{{WP:Teahouse/Teahouse talkback|WP:Teahouse|" + section + "|ts=~~~~}}",
            title: "User talk:"+targetUserName
        }).then(function(editData) {
            alert("Successfully notified user");
        },function(error) {
            alert("An error occurred. Please try again.");
        });
    }
});
//</nowiki>