User:Sur/common.js

From Deadlock Wiki
Revision as of 01:44, 19 September 2024 by 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', {...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// 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);
        }
    }
});