MediaWiki:Common.js: Difference between revisions

From Lost Waves Wiki
(Made a script such that users can add themselves to the DMZ)
 
(Changes the code)
Line 2: Line 2:


mw.loader.using(['mediawiki.api']).then(function () {
mw.loader.using(['mediawiki.api']).then(function () {
    // Function to add a user to the DMZ
     function addToDMZ() {
     function addToDMZ() {
         var api = new mw.Api();
         var api = new mw.Api();
         api.postWithToken('csrf', {
         api.postWithToken('csrf', {
             action: 'edit',
             action: 'edit',
             title: 'DMZ:Users',
             title: 'DMZ:Users', // Adjust this title to match the actual DMZ list page within the DMZ namespace
             appendtext: mw.config.get('wgUserName') + '\n',  // Append the current user's name
             appendtext: '\n* ' + mw.config.get('wgUserName'),  // Append the current user's name
             summary: 'User self-added to DMZ'
             summary: 'User self-added to DMZ'
         }).done(function () {
         }).done(function (response) {
             alert('You have successfully added yourself to the DMZ.');
             if (response.edit && response.edit.result === 'Success') {
                alert('You have successfully added yourself to the DMZ.');
            } else {
                alert('Error: Unable to add to DMZ.');
            }
         }).fail(function () {
         }).fail(function () {
             alert('Error: Unable to add to DMZ.');
             alert('Error contacting the API.');
         });
         });
     }
     }


    // Bind the click event to elements with the class 'addToDMZLink'
     $(document).on('click', '.addToDMZLink', function (e) {
     $(document).on('click', '.addToDMZLink', function (e) {
         e.preventDefault(); // Prevent default action of the link
         e.preventDefault();
         addToDMZ();
         addToDMZ();
     });
     });
});
});

Revision as of 13:25, 8 May 2024

/* Any JavaScript here will be loaded for all users on every page load. */

mw.loader.using(['mediawiki.api']).then(function () {
    function addToDMZ() {
        var api = new mw.Api();
        api.postWithToken('csrf', {
            action: 'edit',
            title: 'DMZ:Users',  // Adjust this title to match the actual DMZ list page within the DMZ namespace
            appendtext: '\n* ' + mw.config.get('wgUserName'),  // Append the current user's name
            summary: 'User self-added to DMZ'
        }).done(function (response) {
            if (response.edit && response.edit.result === 'Success') {
                alert('You have successfully added yourself to the DMZ.');
            } else {
                alert('Error: Unable to add to DMZ.');
            }
        }).fail(function () {
            alert('Error contacting the API.');
        });
    }

    $(document).on('click', '.addToDMZLink', function (e) {
        e.preventDefault();
        addToDMZ();
    });
});