Jump to content

User:Polygnotus/Scripts/WikiTypoInterface.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
(function() {
    function removeElements() {
        const elementsToRemove = [
            '#mw-head', '#mw-page-base', '#mw-panel',
            '#editpage-specialchars', '#mw-userconfigdangerous', '.editpage-head-copywarn'
        ];
        elementsToRemove.forEach(selector => {
            const element = document.querySelector(selector);
            if (element) element.remove();
        });
    }

    function addCustomCSS() {
        const style = document.createElement('style');
        style.textContent = `
            body {
                padding-top: 0 !important;
            }
            
            .mw-body {
                margin-left: 0 !important;
                padding-top: 0 !important;
            }
            
            #content {
                margin-top: 0 !important;
            }
            
            #bodyContent {
                margin-top: 1em;
            }
            #custom-top-buttons {
                margin-bottom: 15px;
            }
            #custom-top-buttons .oo-ui-buttonElement {
                margin-right: 8px;
            }
        `;
        document.head.appendChild(style);
    }

    function addCustomButtons() {
        const buttonContainer = document.createElement('div');
        buttonContainer.id = 'custom-top-buttons';
        buttonContainer.className = 'editButtons';
        const buttons = [
            { text: 'Publish changes', id: 'wpSave', className: 'oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-buttonElement oo-ui-buttonElement-framed oo-ui-labelElement oo-ui-flaggedElement-progressive oo-ui-flaggedElement-primary oo-ui-buttonInputWidget' },
            { text: 'Show preview', id: 'wpPreview', className: 'oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-buttonElement oo-ui-buttonElement-framed oo-ui-labelElement oo-ui-buttonInputWidget' },
            { text: 'Show changes', id: 'wpDiff', className: 'oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-buttonElement oo-ui-buttonElement-framed oo-ui-labelElement oo-ui-buttonInputWidget' }
        ];
        buttons.forEach(btn => {
            const buttonSpan = document.createElement('span');
            buttonSpan.className = btn.className;
            const button = document.createElement('input');
            button.type = 'button';
            button.value = btn.text;
            button.className = 'oo-ui-inputWidget-input oo-ui-buttonElement-button';
            button.addEventListener('click', () => {
                document.getElementById(btn.id).click();
            });
            buttonSpan.appendChild(button);
            buttonContainer.appendChild(buttonSpan);
        });
    
        const bodyContent = document.getElementById('bodyContent');
        const currentAction = mw.config.get('wgAction');
        if (['edit', 'submit', 'diff'].includes(currentAction) && bodyContent) {
            bodyContent.insertBefore(buttonContainer, bodyContent.firstChild);
        }
    }

    function init() {
        removeElements();
        addCustomCSS();
        addCustomButtons();
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();