Jump to content

MediaWiki:Common.js: Difference between revisions

From Artemis Archive
No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
// Wait for the page elements to be ready
$(function () {
$(function() {
     if (mw.config.get('skin') !== 'vector-2022') {
     // Use mw.loader.using to ensure necessary MediaWiki JS utilities are loaded
         console.log('Not using Vector 2022');
    mw.loader.using( 'mediawiki.util' ).then( function () {
        return;
         console.log( '[ReportAbuseLink] Attempting to add link...' ); // Log message for console
    }


    console.log('Using Vector 2022');
        // Use the selector for the list containing personal tools based on inspection
        const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' );


    const tryInsert = () => {
        // Use the correct ID for the login list item based on inspection
         const personalToolsList = document.querySelector('#vector-user-links ul');
         const loginItem = document.querySelector( '#pt-login-2' );


         if (!personalToolsList) {
        // Check if we found the list where links should go
             console.log('Still waiting for #vector-user-links ul...');
         if ( !personalToolsList ) {
             return false;
             console.log( '[ReportAbuseLink] Could not find personal tools list (#p-personal ul.vector-menu-content-list).' );
             return; // Exit if list element is not found
         }
         }
        console.log( '[ReportAbuseLink] Found personal tools list.' );


         console.log('Found user links list — inserting Report Abuse link');
         // --- Configuration ---
        const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if your wiki page has a different name
        const linkText = 'Report Abuse'; // Text displayed for the link
        // --- End Configuration ---


         // Create the new list item
         // Create the new list item (<li>)
         const li = document.createElement('li');
         const li = document.createElement( 'li' );
         li.id = 'pt-reportabuse';
         li.id = 'pt-reportabuse'; // Set an ID for the new list item


         const link = document.createElement('a');
        // Create the link element (<a>)
         link.href = '/wiki/ReportAnimalAbuse'; // Update to your real URL if needed
         const link = document.createElement( 'a' );
         link.textContent = '🐾 Report Abuse';
         link.href = mw.util.getUrl( reportPageName ); // Generates correct URL for the wiki page
         link.title = 'Report animal abuse';
         link.textContent = linkText;
         // link.title = 'Report animal abuse'; // Optional: Add a tooltip title


         li.appendChild(link);
        // Add the link inside the list item
         li.appendChild( link );


         // Try to insert before Log in link
         // Try to insert the new list item before the 'Log in' list item
        const loginItem = document.querySelector('#pt-login');
         if ( loginItem && loginItem.parentNode === personalToolsList ) {
         if (loginItem && loginItem.parentNode === personalToolsList) {
            console.log( '[ReportAbuseLink] Found login item (#pt-login-2), inserting link before it.' );
             personalToolsList.insertBefore(li, loginItem);
             personalToolsList.insertBefore( li, loginItem );
         } else {
         } else {
             personalToolsList.appendChild(li);
            // Fallback if login item isn't found (e.g., user is logged in) or has unexpected parent
            // Try appending to the end of the list as a fallback
            console.log( '[ReportAbuseLink] Login item (#pt-login-2) not found or has wrong parent. Appending link to end.' );
             personalToolsList.appendChild( li );
            // Note: Inserting before 'Preferences' or after 'ULS' dynamically when logged in
            // requires finding those elements reliably too. Appending is a simpler fallback.
         }
         }
 
     });
        return true;
     };
 
    // Retry every 100ms for up to 5 seconds
    let attempts = 0;
    const maxAttempts = 50;
    const interval = setInterval(() => {
        if (tryInsert() || ++attempts >= maxAttempts) {
            clearInterval(interval);
        }
    }, 100);
});
});

Revision as of 04:37, 21 April 2025

// Wait for the page elements to be ready
$(function() {
    // Use mw.loader.using to ensure necessary MediaWiki JS utilities are loaded
    mw.loader.using( 'mediawiki.util' ).then( function () {
        console.log( '[ReportAbuseLink] Attempting to add link...' ); // Log message for console

        // Use the selector for the list containing personal tools based on inspection
        const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' );

        // Use the correct ID for the login list item based on inspection
        const loginItem = document.querySelector( '#pt-login-2' );

        // Check if we found the list where links should go
        if ( !personalToolsList ) {
            console.log( '[ReportAbuseLink] Could not find personal tools list (#p-personal ul.vector-menu-content-list).' );
            return; // Exit if list element is not found
        }
         console.log( '[ReportAbuseLink] Found personal tools list.' );

        // --- Configuration ---
        const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if your wiki page has a different name
        const linkText = 'Report Abuse'; // Text displayed for the link
        // --- End Configuration ---

        // Create the new list item (<li>)
        const li = document.createElement( 'li' );
        li.id = 'pt-reportabuse'; // Set an ID for the new list item

        // Create the link element (<a>)
        const link = document.createElement( 'a' );
        link.href = mw.util.getUrl( reportPageName ); // Generates correct URL for the wiki page
        link.textContent = linkText;
        // link.title = 'Report animal abuse'; // Optional: Add a tooltip title

        // Add the link inside the list item
        li.appendChild( link );

        // Try to insert the new list item before the 'Log in' list item
        if ( loginItem && loginItem.parentNode === personalToolsList ) {
            console.log( '[ReportAbuseLink] Found login item (#pt-login-2), inserting link before it.' );
            personalToolsList.insertBefore( li, loginItem );
        } else {
            // Fallback if login item isn't found (e.g., user is logged in) or has unexpected parent
            // Try appending to the end of the list as a fallback
            console.log( '[ReportAbuseLink] Login item (#pt-login-2) not found or has wrong parent. Appending link to end.' );
            personalToolsList.appendChild( li );
            // Note: Inserting before 'Preferences' or after 'ULS' dynamically when logged in
            // requires finding those elements reliably too. Appending is a simpler fallback.
        }
    });
});