Jump to content

MediaWiki:Common.js: Difference between revisions

From Artemis Archive
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
No edit summary
Line 1: Line 1:
$(function () {
$(function () {
     // identify mobile device
     // Ensure required MediaWiki modules are loaded, including 'mediawiki.config'
     const isMobile = /Mobi|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
     mw.loader.using(['mediawiki.util', 'mediawiki.config']).then(function () {
    if (isMobile) {
         console.log('[ReportAbuseLink] Initializing...');
         console.log('[ReportAbuseLink] Mobile device detected – button disabled.');
        return;
    }


    // desktop load button
        // --- Mobile Check ---
    console.log('[ReportAbuseLink] Running...');
        // Get the current skin name from MediaWiki's configuration
        const currentSkin = mw.config.get('skin');


    const linkURL = '/index.php/Report_Animal_Abuse'; // target page
        // Check if the current skin is a known mobile skin (e.g., 'minerva', 'minervaneue')
    const linkText = 'Report Abuse';
        // You might need to adjust this list if your wiki uses a different custom mobile skin.
        if (currentSkin === 'minerva' || currentSkin === 'minervaneue') {
            console.log('[ReportAbuseLink] Mobile skin (' + currentSkin + ') detected. Button will not be added.');
            return; // Exit the function early if it's a mobile skin
        }
        // --- End Mobile Check ---


    const button = document.createElement('a');
        console.log('[ReportAbuseLink] Desktop skin (' + currentSkin + ') detected. Proceeding to add button...');
    button.href = linkURL;
    button.textContent = linkText;
    button.style.position = 'absolute';
    button.style.top = '0.5rem';
    button.style.right = '1rem';
    button.style.padding = '10px 16px';
    button.style.backgroundColor = '#ff4444';
    button.style.color = 'white';
    button.style.fontWeight = 'bold';
    button.style.borderRadius = '6px';
    button.style.textDecoration = 'none';
    button.style.zIndex = '9999';
    button.style.fontSize = '16px';
    button.style.transition = 'background-color 0.3s ease';


    // Hover effect
        const pageTitle = 'Report_Animal_Abuse';
    button.addEventListener('mouseover', function () {
        const linkHref = mw.util.getUrl(pageTitle);
         button.style.backgroundColor = '#cc0000';
        const linkText = '🚨 Report Animal Abuse';
 
        // Check if the button already exists to avoid duplicates
        if (document.getElementById('report-abuse-button')) {
            console.log('[ReportAbuseLink] Button element already exists. Exiting.');
            return;
        }
 
        // Create a <style> element for hover effect
        console.log('[ReportAbuseLink] Creating styles...');
        const style = document.createElement('style');
        style.textContent = `
            #report-abuse-button a {
                /* Add initial transition for smoothness when hover ends */
                transition: all 0.2s ease-in-out;
            }
            #report-abuse-button a:hover {
                background-color: #ff0000 !important; /* Brighter red on hover */
                box-shadow: 0 0 12px rgba(255, 0, 0, 0.8); /* Red glow */
                transform: scale(1.05); /* Slightly enlarge */
                /* Transition is already defined above */
            }
        `;
        document.head.appendChild(style);
 
        // Create the button container and the link
         console.log('[ReportAbuseLink] Creating button element...');
        const container = document.createElement('div');
        container.id = 'report-abuse-button';
        container.innerHTML = `<a href="${linkHref}" style="
            display: inline-block;
            background-color: #b10000; /* Dark red */
            color: #fff; /* White text */
            font-weight: bold;
            font-size: 18px; /* Large font */
            padding: 10px 18px; /* Generous padding */
            border-radius: 8px; /* Rounded corners */
            text-decoration: none; /* No underline */
            position: fixed; /* Fixed position relative to viewport */
            top: 12px; /* Position from top */
            right: 12px; /* Position from right */
            z-index: 9999; /* Ensure it's on top */
            box-shadow: 0 4px 8px rgba(0,0,0,0.25); /* Subtle shadow */
        ">${linkText}</a>`;
 
        // Append the button container to the body
        document.body.appendChild(container);
        console.log('[ReportAbuseLink] Button rendered with hover effect.');
 
    }).catch(function(e) {
        // Optional: Add error handling in case modules fail to load
        console.error('[ReportAbuseLink] Failed to load required MediaWiki modules:', e);
     });
     });
    button.addEventListener('mouseout', function () {
        button.style.backgroundColor = '#ff4444';
    });
    document.body.appendChild(button);
    console.log('[ReportAbuseLink] Button inserted.');
});
});

Revision as of 07:57, 21 April 2025

$(function () {
    // Ensure required MediaWiki modules are loaded, including 'mediawiki.config'
    mw.loader.using(['mediawiki.util', 'mediawiki.config']).then(function () {
        console.log('[ReportAbuseLink] Initializing...');

        // --- Mobile Check ---
        // Get the current skin name from MediaWiki's configuration
        const currentSkin = mw.config.get('skin');

        // Check if the current skin is a known mobile skin (e.g., 'minerva', 'minervaneue')
        // You might need to adjust this list if your wiki uses a different custom mobile skin.
        if (currentSkin === 'minerva' || currentSkin === 'minervaneue') {
            console.log('[ReportAbuseLink] Mobile skin (' + currentSkin + ') detected. Button will not be added.');
            return; // Exit the function early if it's a mobile skin
        }
        // --- End Mobile Check ---

        console.log('[ReportAbuseLink] Desktop skin (' + currentSkin + ') detected. Proceeding to add button...');

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

        // Check if the button already exists to avoid duplicates
        if (document.getElementById('report-abuse-button')) {
            console.log('[ReportAbuseLink] Button element already exists. Exiting.');
            return;
        }

        // Create a <style> element for hover effect
        console.log('[ReportAbuseLink] Creating styles...');
        const style = document.createElement('style');
        style.textContent = `
            #report-abuse-button a {
                /* Add initial transition for smoothness when hover ends */
                transition: all 0.2s ease-in-out;
            }
            #report-abuse-button a:hover {
                background-color: #ff0000 !important; /* Brighter red on hover */
                box-shadow: 0 0 12px rgba(255, 0, 0, 0.8); /* Red glow */
                transform: scale(1.05); /* Slightly enlarge */
                /* Transition is already defined above */
            }
        `;
        document.head.appendChild(style);

        // Create the button container and the link
        console.log('[ReportAbuseLink] Creating button element...');
        const container = document.createElement('div');
        container.id = 'report-abuse-button';
        container.innerHTML = `<a href="${linkHref}" style="
            display: inline-block;
            background-color: #b10000; /* Dark red */
            color: #fff; /* White text */
            font-weight: bold;
            font-size: 18px; /* Large font */
            padding: 10px 18px; /* Generous padding */
            border-radius: 8px; /* Rounded corners */
            text-decoration: none; /* No underline */
            position: fixed; /* Fixed position relative to viewport */
            top: 12px; /* Position from top */
            right: 12px; /* Position from right */
            z-index: 9999; /* Ensure it's on top */
            box-shadow: 0 4px 8px rgba(0,0,0,0.25); /* Subtle shadow */
        ">${linkText}</a>`;

        // Append the button container to the body
        document.body.appendChild(container);
        console.log('[ReportAbuseLink] Button rendered with hover effect.');

    }).catch(function(e) {
         // Optional: Add error handling in case modules fail to load
         console.error('[ReportAbuseLink] Failed to load required MediaWiki modules:', e);
    });
});