Jump to content

MediaWiki:Common.js: Difference between revisions

From Artemis Archive
No edit summary
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
$(function() {
$(function () {
     mw.loader.using( 'mediawiki.util' ).then( function () {
     // check if MinervaNeue
         console.log( '[ReportAbuseLink] Initializing link insertion...' );
    if (mw.config.get('skin') === 'minerva') {
         console.log('[ReportAbuseLink] when MinervaNeue skin,disable button');
        return;
    }


        const reportPageName = 'Report_Animal_Abuse'; // **CHANGE THIS** if needed
    mw.loader.using('mediawiki.util').then(function () {
         const linkText = 'Report Abuse';
         console.log('[ReportAbuseLink] insert hover button...');


         let attempts = 0;
         const pageTitle = 'Report_Animal_Abuse';
         const maxAttempts = 50; // Try for 5 seconds (50 * 100ms)
         const linkHref = mw.util.getUrl(pageTitle);
         const interval = 100; // Check every 100ms
         const linkText = '🚨 Report Animal Abuse';


         const tryInsertLink = function() {
         if (document.getElementById('report-abuse-button')) return;
            attempts++;
            console.log( '[ReportAbuseLink] Attempt #' + attempts );


            const personalToolsList = document.querySelector( '#p-personal ul.vector-menu-content-list' );
        // create <style> elements
            const loginItem = document.querySelector( '#pt-login-2' );
        const style = document.createElement('style');
 
        style.textContent = `
            if ( personalToolsList && loginItem && loginItem.parentNode === personalToolsList ) {
            #report-abuse-button a:hover {
                // Both list and login item found in correct place!
                 background-color: #ff0000 !important;
                console.log( '[ReportAbuseLink] Found list and #pt-login-2. Inserting link.' );
                 box-shadow: 0 0 12px rgba(255, 0, 0, 0.8);
 
                 transform: scale(1.05);
                 const reportPageTitle = mw.Title.newFromText( reportPageName ); // Use mw.Title if available
                 transition: all 0.2s ease-in-out;
                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
             }
             }
        `;
        document.head.appendChild(style);


             return false; // Indicate insertion not yet successful
        // build button
         };
        const container = document.createElement('div');
        container.id = 'report-abuse-button';
        container.innerHTML = `<a href="${linkHref}" style="
            display: inline-block;
            background-color: #b10000;
            color: #fff;
            font-weight: bold;
            font-size: 18px;
            padding: 10px 18px;
            border-radius: 8px;
            text-decoration: none;
            position: fixed;
            top: 12px;
             right: 12px;
            z-index: 9999;
            box-shadow: 0 4px 8px rgba(0,0,0,0.25);
         ">${linkText}</a>`;


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

Latest revision as of 08:03, 21 April 2025

$(function () {
    // check if MinervaNeue
    if (mw.config.get('skin') === 'minerva') {
        console.log('[ReportAbuseLink] when MinervaNeue skin,disable button');
        return;
    }

    mw.loader.using('mediawiki.util').then(function () {
        console.log('[ReportAbuseLink] insert hover button...');

        const pageTitle = 'Report_Animal_Abuse';
        const linkHref = mw.util.getUrl(pageTitle);
        const linkText = '🚨 Report Animal Abuse';

        if (document.getElementById('report-abuse-button')) return;

        // create <style> elements
        const style = document.createElement('style');
        style.textContent = `
            #report-abuse-button a:hover {
                background-color: #ff0000 !important;
                box-shadow: 0 0 12px rgba(255, 0, 0, 0.8);
                transform: scale(1.05);
                transition: all 0.2s ease-in-out;
            }
        `;
        document.head.appendChild(style);

        // build button
        const container = document.createElement('div');
        container.id = 'report-abuse-button';
        container.innerHTML = `<a href="${linkHref}" style="
            display: inline-block;
            background-color: #b10000;
            color: #fff;
            font-weight: bold;
            font-size: 18px;
            padding: 10px 18px;
            border-radius: 8px;
            text-decoration: none;
            position: fixed;
            top: 12px;
            right: 12px;
            z-index: 9999;
            box-shadow: 0 4px 8px rgba(0,0,0,0.25);
        ">${linkText}</a>`;

        document.body.appendChild(container);
        console.log('[ReportAbuseLink]');
    });
});