Jump to content

MediaWiki:Common.js: Difference between revisions

From Artemis Archive
No edit summary
No edit summary
Line 1: Line 1:
// Wait for the page elements to be ready
$(function() {
$(function() {
    // Use mw.loader.using to ensure necessary MediaWiki JS utilities are loaded
     mw.loader.using( 'mediawiki.util' ).then( function () {
     mw.loader.using( 'mediawiki.util' ).then( function () {
         console.log( '[ReportAbuseLink] Attempting to add link...' ); // Log message for console
         console.log( '[ReportAbuseLink] Initializing link insertion...' );


         // Use the selector for the list containing personal tools based on inspection
         const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if needed
         const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' );
         const linkText = 'Report Abuse';


         // Use the correct ID for the login list item based on inspection
         let attempts = 0;
         const loginItem = document.querySelector( '#pt-login-2' );
        const maxAttempts = 50; // Try for 5 seconds (50 * 100ms)
         const interval = 100; // Check every 100ms


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


        // --- Configuration ---
            const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' );
        const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if your wiki page has a different name
            const loginItem = document.querySelector( '#pt-login-2' );
        const linkText = 'Report Abuse'; // Text displayed for the link
        // --- End Configuration ---


        // Create the new list item (<li>)
            if ( personalToolsList && loginItem && loginItem.parentNode === personalToolsList ) {
        const li = document.createElement( 'li' );
                // Both list and login item found in correct place!
        li.id = 'pt-reportabuse'; // Set an ID for the new list item
                console.log( '[ReportAbuseLink] Found list and #pt-login-2. Inserting link.' );


        // Create the link element (<a>)
                const reportPageTitle = mw.Title.newFromText( reportPageName ); // Use mw.Title if available
        const link = document.createElement( 'a' );
                if (!reportPageTitle) return true; // Stop if title invalid
        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
                const li = document.createElement( 'li' );
        li.appendChild( link );
                li.id = 'pt-reportabuse';
                const link = document.createElement( 'a' );
                link.href = reportPageTitle.getUrl(); // Use mw object method
                link.textContent = linkText;
                li.appendChild( link );


        // Try to insert the new list item before the 'Log in' list item
                personalToolsList.insertBefore( li, loginItem );
        if ( loginItem && loginItem.parentNode === personalToolsList ) {
                return true; // Indicate success
            console.log( '[ReportAbuseLink] Found login item (#pt-login-2), inserting link before it.' );
 
            personalToolsList.insertBefore( li, loginItem );
            } else if ( attempts >= maxAttempts ) {
        } else {
                // Max attempts reached, fallback to appending if list found
            // Fallback if login item isn't found (e.g., user is logged in) or has unexpected parent
                console.log( '[ReportAbuseLink] Max attempts reached. #pt-login-2 not found correctly. Appending to end if list exists.' );
            // Try appending to the end of the list as a fallback
                if (personalToolsList) {
            console.log( '[ReportAbuseLink] Login item (#pt-login-2) not found or has wrong parent. Appending link to end.' );
                    const reportPageTitle = mw.Title.newFromText( reportPageName );
             personalToolsList.appendChild( li );
                    if (!reportPageTitle) return true;
             // Note: Inserting before 'Preferences' or after 'ULS' dynamically when logged in
                    const li = document.createElement( 'li' );
             // requires finding those elements reliably too. Appending is a simpler fallback.
                    li.id = 'pt-reportabuse';
         }
                    const link = document.createElement( 'a' );
                    link.href = reportPageTitle.getUrl();
                    link.textContent = linkText;
                    li.appendChild( link );
                    personalToolsList.appendChild(li);
                } else {
                      console.log( '[ReportAbuseLink] Personal tools list not found even on fallback.' );
                }
                return true; // Indicate fallback finished
            }
 
             return false; // Indicate insertion not yet successful
        };
 
        // Start the interval timer
        const checkInterval = setInterval( function() {
             if ( tryInsertLink() ) {
                clearInterval( checkInterval ); // Stop checking once inserted or max attempts reached
                console.log( '[ReportAbuseLink] Link insertion process finished.' );
             }
         }, interval );
     });
     });
});
});

Revision as of 04:47, 21 April 2025

$(function() {
    mw.loader.using( 'mediawiki.util' ).then( function () {
        console.log( '[ReportAbuseLink] Initializing link insertion...' );

        const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if needed
        const linkText = 'Report Abuse';

        let attempts = 0;
        const maxAttempts = 50; // Try for 5 seconds (50 * 100ms)
        const interval = 100; // Check every 100ms

        const tryInsertLink = function() {
            attempts++;
            console.log( '[ReportAbuseLink] Attempt #' + attempts );

            const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' );
            const loginItem = document.querySelector( '#pt-login-2' );

            if ( personalToolsList && loginItem && loginItem.parentNode === personalToolsList ) {
                // Both list and login item found in correct place!
                console.log( '[ReportAbuseLink] Found list and #pt-login-2. Inserting link.' );

                const reportPageTitle = mw.Title.newFromText( reportPageName ); // Use mw.Title if available
                if (!reportPageTitle) return true; // Stop if title invalid

                const li = document.createElement( 'li' );
                li.id = 'pt-reportabuse';
                const link = document.createElement( 'a' );
                link.href = reportPageTitle.getUrl(); // Use mw object method
                link.textContent = linkText;
                li.appendChild( link );

                personalToolsList.insertBefore( li, loginItem );
                return true; // Indicate success

            } else if ( attempts >= maxAttempts ) {
                // Max attempts reached, fallback to appending if list found
                console.log( '[ReportAbuseLink] Max attempts reached. #pt-login-2 not found correctly. Appending to end if list exists.' );
                 if (personalToolsList) {
                     const reportPageTitle = mw.Title.newFromText( reportPageName );
                     if (!reportPageTitle) return true;
                     const li = document.createElement( 'li' );
                     li.id = 'pt-reportabuse';
                     const link = document.createElement( 'a' );
                     link.href = reportPageTitle.getUrl();
                     link.textContent = linkText;
                     li.appendChild( link );
                     personalToolsList.appendChild(li);
                 } else {
                      console.log( '[ReportAbuseLink] Personal tools list not found even on fallback.' );
                 }
                return true; // Indicate fallback finished
            }

            return false; // Indicate insertion not yet successful
        };

        // Start the interval timer
        const checkInterval = setInterval( function() {
            if ( tryInsertLink() ) {
                clearInterval( checkInterval ); // Stop checking once inserted or max attempts reached
                console.log( '[ReportAbuseLink] Link insertion process finished.' );
            }
        }, interval );
    });
});