User:Sur/common.js: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Sur (talk | contribs)
Created page with "// Wait for the document to be ready mw.hook('wikipage.content').add(function ($content) { // Check if the input box exists on the page if (document.getElementById('userInputBox')) { // Function to update the variable function updateVariable() { var inputValue = document.getElementById('userInputBox').value; // Make an API call to update the page variable new mw.Api().postWithToken('csrf', {..."
 
Sur (talk | contribs)
mNo edit summary
Line 1: Line 1:
// Wait for the document to be ready
document.addEventListener('DOMContentLoaded', function() {
mw.hook('wikipage.content').add(function ($content) {
     const statBoxes = document.querySelectorAll('.stat-box');
     // Check if the input box exists on the page
    if (document.getElementById('userInputBox')) {
        // Function to update the variable
        function updateVariable() {
            var inputValue = document.getElementById('userInputBox').value;
           
            // Make an API call to update the page variable
            new mw.Api().postWithToken('csrf', {
                action: 'parse',
                text: '{{#vardefine:userInput|' + inputValue + '}}',
                title: mw.config.get('wgPageName'),
                prop: 'text'
            }).done(function(data) {
                console.log('Variable updated successfully');
                // Refresh only the parts of the page that use the variable
                $('.user-input-display').each(function() {
                    $(this).text(inputValue);
                });
            }).fail(function(error) {
                console.error('Failed to update variable:', error);
            });
        }


        // Add event listener to the update button
    statBoxes.forEach(function(statBox) {
         var updateButton = document.getElementById('updateVariableButton');
         statBox.addEventListener('click', function() {
        if (updateButton) {
             statBox.classList.toggle('clicked');
             updateButton.addEventListener('click', updateVariable);
         });
         }
     });
     }
});
});

Revision as of 23:35, 21 September 2024

document.addEventListener('DOMContentLoaded', function() {
    const statBoxes = document.querySelectorAll('.stat-box');

    statBoxes.forEach(function(statBox) {
        statBox.addEventListener('click', function() {
            statBox.classList.toggle('clicked');
        });
    });
});