User:Ritenerek/js/toggle edit bg.js

Source: 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.
// Check if editing
if (mw.config.get('wgEditMessage')) {
    // Load mw.util for addPortletLink
    mw.loader.using('mediawiki.util', (r) => {
        // Add portlet link (who'd have thought!)
        var link = r('mediawiki.util').addPortletLink(
            'p-tb',
            '#',
            'Toggle background',
            'n-toggle-bg',
            'Toggle the edit box\'s background',
            null
        );
        // Remove href so there's no redirection
        link.lastChild.attributes.removeNamedItem('href');

        let editbox = document.getElementById('wpTextbox1');

        $(link).on('click', () => {
            if (editbox.style.backgroundColor == 'white') {
                editbox.style.removeProperty('background');
            } else {
                editbox.style.background = 'white';
            }
        });
    }).catch(console.error);
}