Jump to content

User:9t5/common.js

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by 9t5 (talk | contribs) at 03:56, 6 June 2024. The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
document.addEventListener('DOMContentLoaded', function() {
    var inputBox = document.querySelector('#mw-inputbox-form input[type="text"]');
    var submitButton = document.querySelector('#mw-inputbox-form input[type="submit"]');

    if (inputBox && submitButton) {
        submitButton.addEventListener('click', function(event) {
            event.preventDefault(); // Prevent the default form submission

            var username = inputBox.value.trim();
            if (username) {
                var pageTitle = 'User:9t5/contestants';
                var api = new mw.Api();

                api.get({
                    action: 'query',
                    prop: 'revisions',
                    rvprop: 'content',
                    titles: pageTitle,
                    formatversion: 2
                }).done(function(data) {
                    var content = data.query.pages[0].revisions[0].content;

                    // Find the position to insert the new row
                    var newRow = '<tr><td>' + username + '</td><td> </td><td> </td></tr>';
                    var newContent = content.replace('</tbody>', newRow + '</tbody>');

                    api.postWithEditToken({
                        action: 'edit',
                        title: pageTitle,
                        text: newContent,
                        summary: 'Adding contestant to the chart'
                    }).done(function() {
                        alert('Username added successfully!');
                        location.reload(); // Reload the page to reflect the change
                    }).fail(function(error) {
                        console.error(error);
                        alert('An error occurred while adding the username.');
                    });
                }).fail(function(error) {
                    console.error(error);
                    alert('An error occurred while retrieving the page content.');
                });
            } else {
                alert('Please enter a valid username.');
            }
        });
    }
});