MediaWiki:Citizen.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 1: Line 1:
function isUserLoggedIn() {
+
$(document).ready(function() {
  +
// Check if the user is not logged in
return !mw.user.isAnon();
+
if (mw.user.isAnon()) {
}
 
  +
var handleElements = function() {
  +
// Select the elements with the specified class
 
var elements = $('.oo-ui-buttonElement-button');
   
  +
// Iterate through the selected elements
function handleElement($element) {
 
  +
elements.each(function() {
if ($element.attr('aria-disabled') === 'true') {
 
console.log('User not logged in; adding login message');
+
var $element = $(this);
$element.after('<p>Please <a href="/wiki/Special:UserLogin">login</a> or <a href="/wiki/Special:UserSignup">sign up</a> to continue.</p>');
 
} else {
 
console.log('User is logged in; no message added');
 
}
 
}
 
   
  +
// Check if aria-disabled is set to "true"
function handleMutations(mutationsList) {
 
 
if ($element.attr('aria-disabled') === 'true') {
mutationsList.forEach(function (mutation) {
 
  +
// Create a new HTML snippet with MediaWiki links
if (mutation.type === 'childList') {
 
 
var loginMessage = '<p>Please <a href="/wiki/Special:UserLogin">login</a> or <a href="/wiki/Special:CreateAccount">sign up</a> to continue.</p>';
Array.prototype.forEach.call(mutation.addedNodes, function (node) {
 
  +
if ($(node).hasClass('oo-ui-buttonElement-button')) {
 
console.log('Handling dynamic element');
+
// Check if the message is already present to avoid duplication
handleElement($(node));
+
if (!$element.next().hasClass('login-message')) {
  +
// Insert the message after the disabled button
  +
$element.after(loginMessage);
 
}
 
}
 
}
 
});
 
});
 
};
} else if (mutation.type === 'attributes' && mutation.attributeName === 'aria-disabled') {
 
console.log('Handling attribute change');
 
handleElement($(mutation.target));
 
}
 
});
 
}
 
   
  +
// Initial check
  +
handleElements();
   
  +
// Use delegated event handling to handle dynamically added elements
$(document).ready(function () {
 
  +
$(document).on('DOMNodeInserted', function(event) {
console.log('Checking initial state');
 
var observer = new MutationObserver(handleMutations);
+
var target = $(event.target);
observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['aria-disabled'] });
 
   
  +
// Check if the target has the specified class
 
$('.oo-ui-buttonElement-button').each(function (index, element) {
+
if (target.hasClass('oo-ui-buttonElement-button')) {
  +
handleElements();
console.log('Handling initial element');
 
handleElement($(element));
+
}
});
+
});
 
}
 
});
 
});

Revision as of 01:18, 15 October 2023

$(document).ready(function() {
    // Check if the user is not logged in
    if (mw.user.isAnon()) {
        var handleElements = function() {
            // Select the elements with the specified class
            var elements = $('.oo-ui-buttonElement-button');

            // Iterate through the selected elements
            elements.each(function() {
                var $element = $(this);

                // 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>';

                    // Check if the message is already present to avoid duplication
                    if (!$element.next().hasClass('login-message')) {
                        // Insert the message after the disabled button
                        $element.after(loginMessage);
                    }
                }
            });
        };

        // Initial check
        handleElements();

        // Use delegated event handling to handle dynamically added elements
        $(document).on('DOMNodeInserted', function(event) {
            var target = $(event.target);

            // Check if the target has the specified class
            if (target.hasClass('oo-ui-buttonElement-button')) {
                handleElements();
            }
        });
    }
});