Jump to content

User:Ieditrandomarticles/autobox.js

From Simple English Wikipedia, the free encyclopedia

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* 
autobox gadget - One-click maintenance templates for Wikipedia
*/

(function() {
    // CONFIGURATION - Edit these to customize your tags
    const availableTags = {
        // Top-of-page templates
        'noref': {template: 'noref', placement: 'top'},
        'onesource': {template: 'onesource', placement: 'top'},
        'copyedit': {template: 'copyedit', placement: 'top'},
        'wikify': {template: 'wikify', placement: 'top'},
        'POV': {template: 'POV', placement: 'top'},
        'update': {template: 'update', placement: 'top'},
        'refimprove': {template: 'refimprove', placement: 'top'},
        
        // Inline templates
        'citation needed': {template: 'cn', placement: 'inline'},
        'dubious': {template: 'dubious', placement: 'inline'},
        'clarify': {template: 'clarify', placement: 'inline'},
        'who': {template: 'who', placement: 'inline'},
        'when': {template: 'when', placement: 'inline'}
    };

    // Get current month/year for dating templates
    function getCurrentDate() {
        const months = ['January', 'February', 'March', 'April', 'May', 'June', 
                       'July', 'August', 'September', 'October', 'November', 'December'];
        const now = new Date();
        return months[now.getMonth()] + ' ' + now.getFullYear();
    }

    // Modern URL parameter handler
    function buildEditUrl(title, params) {
        const baseUrl = `${window.location.origin}/w/index.php`;
        const queryParams = new URLSearchParams();
        queryParams.set('title', title);
        
        for (const [key, value] of Object.entries(params)) {
            if (value !== undefined && value !== null) {
                queryParams.set(key, value);
            }
        }
        
        return `${baseUrl}?${queryParams.toString()}`;
    }

    // Create the floating UI elements
    function initUI() {
        // Skip if on non-article pages or if UI already exists
        if (mw.config.get('wgNamespaceNumber') !== 0 || document.getElementById('autobox-button')) return;

        // Create main floating button
        const button = document.createElement('div');
        button.id = 'autobox-button';
        button.title = 'Quick maintenance tags';
        button.innerHTML = '📦 autobox';
        Object.assign(button.style, {
            position: 'fixed',
            bottom: '20px',
            right: '20px',
            zIndex: '9999',
            cursor: 'pointer',
            padding: '8px 12px',
            backgroundColor: '#2a4b8d',
            color: 'white',
            borderRadius: '4px',
            boxShadow: '0 2px 5px rgba(0,0,0,0.3)',
            fontFamily: 'sans-serif',
            fontSize: '14px'
        });
        
        // Create panel container
        const panel = document.createElement('div');
        panel.id = 'autobox-panel';
        Object.assign(panel.style, {
            display: 'none',
            position: 'fixed',
            bottom: '60px',
            right: '20px',
            zIndex: '9998',
            backgroundColor: '#f8f9fa',
            padding: '10px',
            borderRadius: '4px',
            boxShadow: '0 2px 10px rgba(0,0,0,0.2)',
            width: '200px',
            maxHeight: '60vh',
            overflowY: 'auto',
            border: '1px solid #a2a9b1'
        });
        
        // Add category heading
        const topHeading = document.createElement('h4');
        topHeading.textContent = 'Article-wide tags';
        topHeading.style.margin = '5px 0 10px 0';
        topHeading.style.fontSize = '13px';
        topHeading.style.color = '#54595d';
        panel.appendChild(topHeading);
        
        // Add top-of-page tags
        for (const [name, config] of Object.entries(availableTags)) {
            if (config.placement !== 'top') continue;
            
            const btn = createTagButton(name, config);
            panel.appendChild(btn);
        }
        
        // Add inline heading
        const inlineHeading = document.createElement('h4');
        inlineHeading.textContent = 'Inline tags';
        inlineHeading.style.margin = '15px 0 10px 0';
        inlineHeading.style.fontSize = '13px';
        inlineHeading.style.color = '#54595d';
        panel.appendChild(inlineHeading);
        
        // Add inline tags
        for (const [name, config] of Object.entries(availableTags)) {
            if (config.placement !== 'inline') continue;
            
            const btn = createTagButton(name, config);
            panel.appendChild(btn);
        }
        
        // Add elements to page
        document.body.appendChild(button);
        document.body.appendChild(panel);
        
        // Toggle panel visibility
        button.addEventListener('click', function(e) {
            e.stopPropagation();
            panel.style.display = panel.style.display === 'none' ? 'block' : 'none';
        });
        
        // Close panel when clicking elsewhere
        document.addEventListener('click', function() {
            panel.style.display = 'none';
        });
        
        // Prevent panel clicks from closing it
        panel.addEventListener('click', function(e) {
            e.stopPropagation();
        });
    }

    // Create a tag button element
    function createTagButton(name, config) {
        const btn = document.createElement('button');
        btn.textContent = name;
        Object.assign(btn.style, {
            display: 'block',
            width: '100%',
            margin: '4px 0',
            padding: '6px 8px',
            textAlign: 'left',
            backgroundColor: 'white',
            border: '1px solid #a2a9b1',
            borderRadius: '3px',
            cursor: 'pointer',
            fontSize: '13px'
        });
        
        btn.addEventListener('mouseover', function() {
            this.style.backgroundColor = '#eaf3ff';
        });
        
        btn.addEventListener('mouseout', function() {
            this.style.backgroundColor = 'white';
        });
        
        btn.addEventListener('click', function(e) {
            e.stopPropagation();
            addTag(name);
        });
        
        return btn;
    }

    // Add the selected tag to the page
    function addTag(tagName) {
        const config = availableTags[tagName];
        if (!config) return;
        
        const date = getCurrentDate();
        let tagText;
        
        // Format the template with date
        if (['noref', 'onesource', 'copyedit', 'wikify', 'POV', 'update', 'refimprove'].includes(config.template)) {
            tagText = `{{${config.template}|date=${date}}}`;
        } else if (config.template === 'cn') {
            tagText = `{{citation needed|date=${date}}}`;
        } else {
            tagText = `{{${config.template}|date=${date}}}`;
        }
        
        const title = mw.config.get('wgPageName');
        const section = mw.config.get('wgSectionNumber') || '0';
        const editSummary = `added ${tagName} using autobox`;
        
        if (config.placement === 'inline') {
            const selection = window.getSelection().toString().trim();
            if (selection) {
                // For inline tags with text selected
                window.location.href = buildEditUrl(title, {
                    action: 'edit',
                    section: section,
                    preloadtitle: `/* ${selection} */ ${tagText}`,
                    summary: editSummary
                });
                return;
            } else {
                // No text selected - prompt user
                const text = prompt('Enter the text to tag:');
                if (text) {
                    window.location.href = buildEditUrl(title, {
                        action: 'edit',
                        section: section,
                        preloadtitle: `/* ${text} */ ${tagText}`,
                        summary: editSummary
                    });
                    return;
                }
            }
        }
        
        // For top-of-page tags
        window.location.href = buildEditUrl(title, {
            action: 'edit',
            section: section,
            prependtext: `${tagText}\n`,
            summary: editSummary
        });
    }

    // Initialize when page loads
    if (window.mw) {
        $(initUI);
    } else {
        window.addEventListener('load', initUI);
    }
})();