MediaWiki:Citizen.js: Difference between revisions

MediaWiki interface page
(Created page with "$(document).ready(function() { // Check if the user is not logged in if (mw.user.isAnon()) { // 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') { // Crea...")
 
No edit summary
Line 1: Line 1:
  +
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:UserSignup">sign up</a> to continue.</p>';
  +
 
// Insert the message after the disabled button
 
$element.after(loginMessage);
  +
}
  +
}
  +
 
$(document).ready(function() {
 
$(document).ready(function() {
// Check if the user is not logged in
+
// Check the initial state
  +
checkElements();
if (mw.user.isAnon()) {
 
// Select the elements with the specified class
 
var elements = $('.oo-ui-buttonElement-button');
 
   
// Iterate through the selected elements
+
// Use event delegation to handle dynamically added elements
  +
$(document).on('DOMNodeInserted', function(event) {
  +
var target = $(event.target);
  +
if (target.hasClass('oo-ui-buttonElement-button')) {
 
checkElements();
  +
}
  +
});
  +
  +
function checkElements() {
 
// Select all elements with the specified class
 
var elements = $('.oo-ui-buttonElement-button');
 
elements.each(function() {
 
elements.each(function() {
 
var $element = $(this);
 
var $element = $(this);
  +
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);
 
}
 
 
});
 
});
 
}
 
}

Revision as of 01:03, 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:UserSignup">sign up</a> to continue.</p>';

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

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

    // Use event delegation to handle dynamically added elements
    $(document).on('DOMNodeInserted', function(event) {
        var target = $(event.target);
        if (target.hasClass('oo-ui-buttonElement-button')) {
            checkElements();
        }
    });

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