Toggle menu
862
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
 
(20 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
function attachModifiedClickHandler(element) {
    const eventsData = $._data(element, 'events');
    const handlers = eventsData && eventsData.click;
   
    if (handlers && handlers.length > 0) {
        const originalHandler = handlers[0].handler;


function ModifySidebar(action, section, name, link) {
         // Properly detach and reattach the click handler using jQuery
    try {
         $(element).off('click').on('click', function(event) {
         switch (section) {
             event.stopPropagation();
          case "languages":
             originalHandler.call(this, event);
            var target = "p-lang";
        });
            break;
 
          case "toolbox":
         // Use native JavaScript to add the fix class
            var target = "p-tb";
         element.classList.add('FixCatTreeClick');
            break;
          case "navigation":
            var target = "p-navigation";
            break;
          default:
            var target = "p-" + section;
            break;
        }
         if (action == "add") {
            var node = document.getElementById(target)
                              .getElementsByTagName('div')[0]
                              .getElementsByTagName('ul')[0];
            var aNode = document.createElement('a');
             var liNode = document.createElement('li');
            aNode.appendChild(document.createTextNode(name));
             aNode.setAttribute('href', link);
            liNode.appendChild(aNode);
            liNode.className='plainlinks';
            node.appendChild(liNode);
         }
         if (action == "remove") {
            var list = document.getElementById(target)
                              .getElementsByTagName('div')[0]
                              .getElementsByTagName('ul')[0];
            var listelements = list.getElementsByTagName('li');
            for (var i = 0; i < listelements.length; i++) {
                if (listelements[i].getElementsByTagName('a')[0].innerHTML == name ||
                    listelements[i].getElementsByTagName('a')[0].href == link) {
                    list.removeChild(listelements[i]);
                }
            }
        }
    } catch(e) {
      // lets just ignore what's happened
      return;
     }
     }
}
}
 
function CustomizeModificationsOfSidebar() {
addOnloadHook(function() {
     //adds [[Special:CategoryTree]] to toolbox
     const observer = new MutationObserver((mutations) => {
    ModifySidebar("add", "toolbox", "CategoryTree", "http://en.wikipedia.org/wiki/Special:CategoryTree");
        mutations.forEach((mutation) => {
     //removes [[Special:Upload]] from toolbox
            const target = mutation.target;
     ModifySidebar("remove", "toolbox", "Upload file", "http://en.wikipedia.org/wiki/Special:Upload");
 
}
            // Check if the element has the specified class and has not already been processed
            if (
addOnloadHook(CustomizeModificationsOfSidebar);
                mutation.attributeName === 'class' &&
                target.classList.contains('CategoryTreeToggleHandlerAttached') &&
                !target.classList.contains('FixCatTreeClick')
            ) {
                attachModifiedClickHandler(target);
            } else if (target && mutation.attributeName == null) {
                target.querySelectorAll(".CategoryTreeToggle.CategoryTreeToggleHandlerAttached:not(.FixCatTreeClick)").forEach((e) => attachModifiedClickHandler(e));
            }
        });
    });
 
     // Observe changes to attributes, specifically the 'class' attribute, throughout the document
     observer.observe(document.body, {
        childList: true,
        attributes: true,
        attributeFilter: ['class'],
        subtree: true
    });
});

Latest revision as of 11:21, 30 October 2024

function attachModifiedClickHandler(element) {
    const eventsData = $._data(element, 'events');
    const handlers = eventsData && eventsData.click;
    
    if (handlers && handlers.length > 0) {
        const originalHandler = handlers[0].handler;

        // Properly detach and reattach the click handler using jQuery
        $(element).off('click').on('click', function(event) {
            event.stopPropagation();
            originalHandler.call(this, event);
        });

        // Use native JavaScript to add the fix class
        element.classList.add('FixCatTreeClick');
    }
}

addOnloadHook(function() {
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            const target = mutation.target;

            // Check if the element has the specified class and has not already been processed
            if (
                mutation.attributeName === 'class' &&
                target.classList.contains('CategoryTreeToggleHandlerAttached') &&
                !target.classList.contains('FixCatTreeClick')
            ) {
                attachModifiedClickHandler(target);
            } else if (target && mutation.attributeName == null) {
                target.querySelectorAll(".CategoryTreeToggle.CategoryTreeToggleHandlerAttached:not(.FixCatTreeClick)").forEach((e) => attachModifiedClickHandler(e));
            }
        });
    });

    // Observe changes to attributes, specifically the 'class' attribute, throughout the document
    observer.observe(document.body, {
        childList: true,
        attributes: true,
        attributeFilter: ['class'],
        subtree: true
    });
});