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)
m Blanked the page
Tags: Blanking Manual revert
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
// 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', {
                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
        var updateButton = document.getElementById('updateVariableButton');
        if (updateButton) {
            updateButton.addEventListener('click', updateVariable);
        }
    }
});

Latest revision as of 00:47, 23 September 2024