Jump to content

MediaWiki:Common.js: Difference between revisions

From Artemis Archive
No edit summary
No edit summary
Line 1: Line 1:
$(function() {
$(function () {
     mw.loader.using( 'mediawiki.util' ).then( function () {
     mw.loader.using('mediawiki.util').then(function () {
         console.log( '[ReportAbuseLink] Initializing link insertion...' );
         console.log('[ReportAbuseLink] Starting...');


         const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if needed
         const reportLinkText = '🐾 Report Abuse';
         const linkText = 'Report Abuse';
         const reportPageURL = mw.util.getUrl('Report_Animal_Abuse'); // Change if your page name differs


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


         const tryInsertLink = function() {
         const tryInsert = function () {
             attempts++;
             const personalMenu = document.querySelector('#p-personal ul.vector-menu-content-list');
             console.log( '[ReportAbuseLink] Attempt #' + attempts );
             const loginItem = document.querySelector('#pt-login-2');


             const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' );
             if (personalMenu && loginItem) {
            const loginItem = document.querySelector( '#pt-login-2' );
                console.log('[ReportAbuseLink] Found login item. Inserting before it.');
 
                const li = document.createElement('li');
            if ( personalToolsList && loginItem && loginItem.parentNode === personalToolsList ) {
                li.id = 'pt-reportabuse';
                // 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
                 const a = document.createElement('a');
                 if (!reportPageTitle) return true; // Stop if title invalid
                a.href = reportPageURL;
                 a.textContent = reportLinkText;
                a.title = 'Report animal abuse to authorities or NGOs';


                 const li = document.createElement( 'li' );
                li.appendChild(a);
                personalMenu.insertBefore(li, loginItem);
                return true;
            } else if (personalMenu && attempts >= maxAttempts) {
                console.log('[ReportAbuseLink] Login item not found after max attempts. Appending to end.');
                 const li = document.createElement('li');
                 li.id = 'pt-reportabuse';
                 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 );
                 const a = document.createElement('a');
                 return true; // Indicate success
                 a.href = reportPageURL;
                a.textContent = reportLinkText;
                a.title = 'Report animal abuse to authorities or NGOs';


            } else if ( attempts >= maxAttempts ) {
                 li.appendChild(a);
                // Max attempts reached, fallback to appending if list found
                personalMenu.appendChild(li);
                 console.log( '[ReportAbuseLink] Max attempts reached. #pt-login-2 not found correctly. Appending to end if list exists.' );
                 return true;
                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
             return false;
         };
         };


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

Revision as of 04:53, 21 April 2025

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

        const reportLinkText = '🐾 Report Abuse';
        const reportPageURL = mw.util.getUrl('Report_Animal_Abuse'); // Change if your page name differs

        let attempts = 0;
        const maxAttempts = 50;

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

            if (personalMenu && loginItem) {
                console.log('[ReportAbuseLink] Found login item. Inserting before it.');
                const li = document.createElement('li');
                li.id = 'pt-reportabuse';

                const a = document.createElement('a');
                a.href = reportPageURL;
                a.textContent = reportLinkText;
                a.title = 'Report animal abuse to authorities or NGOs';

                li.appendChild(a);
                personalMenu.insertBefore(li, loginItem);
                return true;
            } else if (personalMenu && attempts >= maxAttempts) {
                console.log('[ReportAbuseLink] Login item not found after max attempts. Appending to end.');
                const li = document.createElement('li');
                li.id = 'pt-reportabuse';

                const a = document.createElement('a');
                a.href = reportPageURL;
                a.textContent = reportLinkText;
                a.title = 'Report animal abuse to authorities or NGOs';

                li.appendChild(a);
                personalMenu.appendChild(li);
                return true;
            }

            return false;
        };

        const interval = setInterval(function () {
            attempts++;
            if (tryInsert() || attempts >= maxAttempts) {
                clearInterval(interval);
                console.log('[ReportAbuseLink] Done.');
            }
        }, 100);
    });
});