MediaWiki:Citizen.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 8: Line 8:
 
if ($element.attr('aria-disabled') === 'true') {
 
if ($element.attr('aria-disabled') === 'true') {
 
// Create a new HTML snippet with MediaWiki links
 
// Create a new HTML snippet with MediaWiki links
var loginMessage = '<p>Please <a href="/wiki/Special:UserLogin">login</a> or <a href="/wiki/Special:UserSignup">sign up</a> to continue.</p>';
+
var loginMessage = '<p>Please <a href="/wiki/Special:UserLogin">login</a> or <a href="/wiki/Special:CreateAccount">sign up</a> to continue.</p>';
   
 
// Insert the message after the disabled button
 
// Insert the message after the disabled button
Line 15: Line 15:
 
}
 
}
   
$(document).ready(function() {
+
function observeDOM() {
  +
// Select the target node where you expect the dynamic changes
// Check the initial state
 
  +
var targetNode = document.body; // You can specify a more specific container if needed
checkElements();
 
   
  +
// Create a MutationObserver instance
// Use event delegation to handle dynamically added elements
 
  +
var observer = new MutationObserver(function (mutationsList) {
$(document).on('DOMNodeInserted', function(event) {
 
var target = $(event.target);
+
for (var mutation of mutationsList) {
if (target.hasClass('oo-ui-buttonElement-button')) {
+
if (mutation.type === 'childList') {
checkElements();
+
// Handle added nodes
  +
mutation.addedNodes.forEach(function (node) {
  +
if (node.nodeType === Node.ELEMENT_NODE) {
  +
var $element = $(node);
  +
if ($element.hasClass('oo-ui-buttonElement-button')) {
  +
insertLoginMessage($element);
  +
}
  +
}
  +
});
  +
} else if (mutation.type === 'attributes' && mutation.attributeName === 'aria-disabled') {
  +
// Handle attribute changes
  +
var $element = $(mutation.target);
  +
insertLoginMessage($element);
  +
}
 
}
 
}
 
});
 
});
  +
  +
// Configure and start the observer
  +
var config = { childList: true, subtree: true, attributes: true, attributeFilter: ['aria-disabled'] };
  +
observer.observe(targetNode, config);
  +
}
  +
  +
$(document).ready(function () {
 
// Check the initial state
 
checkElements();
  +
  +
// Observe DOM for changes
  +
observeDOM();
   
 
function checkElements() {
 
function checkElements() {
 
// Select all elements with the specified class
 
// Select all elements with the specified class
 
var elements = $('.oo-ui-buttonElement-button');
 
var elements = $('.oo-ui-buttonElement-button');
elements.each(function() {
+
elements.each(function () {
 
var $element = $(this);
 
var $element = $(this);
 
insertLoginMessage($element);
 
insertLoginMessage($element);

Revision as of 01:05, 15 October 2023

function isUserLoggedIn() {
    // Use MediaWiki's user interface elements to check the login status
    return !mw.user.isAnon();
}

function insertLoginMessage($element) {
    // Check if aria-disabled is set to "true"
    if ($element.attr('aria-disabled') === 'true') {
        // Create a new HTML snippet with MediaWiki links
        var loginMessage = '<p>Please <a href="/wiki/Special:UserLogin">login</a> or <a href="/wiki/Special:CreateAccount">sign up</a> to continue.</p>';

        // Insert the message after the disabled button
        $element.after(loginMessage);
    }
}

function observeDOM() {
    // Select the target node where you expect the dynamic changes
    var targetNode = document.body; // You can specify a more specific container if needed

    // Create a MutationObserver instance
    var observer = new MutationObserver(function (mutationsList) {
        for (var mutation of mutationsList) {
            if (mutation.type === 'childList') {
                // Handle added nodes
                mutation.addedNodes.forEach(function (node) {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        var $element = $(node);
                        if ($element.hasClass('oo-ui-buttonElement-button')) {
                            insertLoginMessage($element);
                        }
                    }
                });
            } else if (mutation.type === 'attributes' && mutation.attributeName === 'aria-disabled') {
                // Handle attribute changes
                var $element = $(mutation.target);
                insertLoginMessage($element);
            }
        }
    });

    // Configure and start the observer
    var config = { childList: true, subtree: true, attributes: true, attributeFilter: ['aria-disabled'] };
    observer.observe(targetNode, config);
}

$(document).ready(function () {
    // Check the initial state
    checkElements();

    // Observe DOM for changes
    observeDOM();

    function checkElements() {
        // Select all elements with the specified class
        var elements = $('.oo-ui-buttonElement-button');
        elements.each(function () {
            var $element = $(this);
            insertLoginMessage($element);
        });
    }
});