User:Euphoria/common.js

From Test Wiki
Revision as of 08:19, 25 September 2025 by Euphoria (talk | contribs) (fix)
Jump to navigation Jump to search

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.
//<nowiki>
mw.loader.using('mediawiki.util', function () {
    const pagePrefix = 'User:Euphoria/TestVfD/';

    if (mw.config.get('wgPageName').startsWith(pagePrefix)) {

        // Only on page view
        if (mw.config.get('wgAction') === 'view') {

            // Find all section headings (h2)
            document.querySelectorAll('h2 .mw-headline').forEach(function (headline) {
                const sectionName = headline.textContent.trim();

                // Only add buttons to sections that match your criteria (optional)
                if (sectionName) {

                    // Create container for buttons
                    const container = document.createElement('span');
                    container.style.marginLeft = '10px';

                    // Add buttons
                    ['delete', 'keep', 'no consensus'].forEach(function (action) {
                        const btn = document.createElement('a');
                        btn.href = '#';
                        btn.textContent = action.charAt(0).toUpperCase() + action.slice(1);
                        btn.style.marginRight = '5px';
                        btn.style.fontSize = '90%';
                        btn.addEventListener('click', function (e) {
                            e.preventDefault();

                            const topText = '{{subst:vt|' + action + '. --~~~~}}\n==' + sectionName + '==\n';
                            const bottomText = '\n{{subst:vb}}';
                            const summary = 'Closed section "' + sectionName + '" as ' + action;

                            localStorage.setItem('vfdPrefillContent', JSON.stringify({ top: topText, bottom: bottomText }));
                            localStorage.setItem('vfdPrefillSummary', summary);

                            window.location.href = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'edit' });
                        });
                        container.appendChild(btn);
                    });

                    // Insert container next to the section headline
                    headline.parentNode.appendChild(container);
                }
            });
        }

        // Edit page prefill (same as before)
        if (mw.config.get('wgAction') === 'edit') {
            const textbox = document.getElementById('wpTextbox1');
            const summaryBox = document.getElementById('wpSummary');

            if (textbox) {
                const stored = localStorage.getItem('vfdPrefillContent');
                const storedSummary = localStorage.getItem('vfdPrefillSummary');

                if (stored) {
                    const { top, bottom } = JSON.parse(stored);
                    const content = textbox.value.replace(/^\s+/, '').replace(/\s+$/, '');
                    textbox.value = top + content + bottom;

                    if (summaryBox && storedSummary) {
                        summaryBox.value = storedSummary;
                    }

                    localStorage.removeItem('vfdPrefillContent');
                    localStorage.removeItem('vfdPrefillSummary');
                }
            }
        }

    }
});
//</nowiki>