MediaWiki:Citizen.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 5: Line 5:
 
function handleElement($element) {
 
function handleElement($element) {
 
if ($element.attr('aria-disabled') === 'true') {
 
if ($element.attr('aria-disabled') === 'true') {
  +
console.log('User not logged in; adding login message');
 
$element.after('<p>Please <a href="/wiki/Special:UserLogin">login</a> or <a href="/wiki/Special:UserSignup">sign up</a> to continue.</p>');
 
$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');
 
}
 
}
 
}
 
}
Line 14: Line 17:
 
Array.prototype.forEach.call(mutation.addedNodes, function (node) {
 
Array.prototype.forEach.call(mutation.addedNodes, function (node) {
 
if ($(node).hasClass('oo-ui-buttonElement-button')) {
 
if ($(node).hasClass('oo-ui-buttonElement-button')) {
  +
console.log('Handling dynamic element');
 
handleElement($(node));
 
handleElement($(node));
 
}
 
}
 
});
 
});
 
} else if (mutation.type === 'attributes' && mutation.attributeName === 'aria-disabled') {
 
} else if (mutation.type === 'attributes' && mutation.attributeName === 'aria-disabled') {
  +
console.log('Handling attribute change');
 
handleElement($(mutation.target));
 
handleElement($(mutation.target));
 
}
 
}
Line 27: Line 32:
   
 
$(document).ready(function () {
 
$(document).ready(function () {
  +
console.log('Checking initial state');
 
$('.oo-ui-buttonElement-button').each(function (index, element) {
 
$('.oo-ui-buttonElement-button').each(function (index, element) {
  +
console.log('Handling initial element');
 
handleElement($(element));
 
handleElement($(element));
 
});
 
});

Revision as of 01:11, 15 October 2023

function isUserLoggedIn() {
    return !mw.user.isAnon();
}

function handleElement($element) {
    if ($element.attr('aria-disabled') === 'true') {
        console.log('User not logged in; adding login message');
        $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');
    }
}

function handleMutations(mutationsList) {
    mutationsList.forEach(function (mutation) {
        if (mutation.type === 'childList') {
            Array.prototype.forEach.call(mutation.addedNodes, function (node) {
                if ($(node).hasClass('oo-ui-buttonElement-button')) {
                    console.log('Handling dynamic element');
                    handleElement($(node));
                }
            });
        } else if (mutation.type === 'attributes' && mutation.attributeName === 'aria-disabled') {
            console.log('Handling attribute change');
            handleElement($(mutation.target));
        }
    });
}

var observer = new MutationObserver(handleMutations);
observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['aria-disabled'] });

$(document).ready(function () {
    console.log('Checking initial state');
    $('.oo-ui-buttonElement-button').each(function (index, element) {
        console.log('Handling initial element');
        handleElement($(element));
    });
});