User:Euphoria/common.js

From Test Wiki
Revision as of 08:12, 25 September 2025 by Euphoria (talk | contribs) (fix)

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/';

    // Only run on relevant pages
    if (mw.config.get('wgPageName').startsWith(pagePrefix)) {

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

            function addClosureButton(label, action) {
                mw.util.addPortletLink(
                    'p-cactions',
                    '#',
                    label,
                    'ca-close-' + action.replace(/\s+/g, '-'),
                    'Close this VfD as ' + action
                ).addEventListener('click', function (e) {
                    e.preventDefault();

                    // Store the content in localStorage
                    const topText = '{{subst:vt|' + action + '. --~~~~}}\n\n';
                    const bottomText = '\n\n{{subst:vb}}';
                    const summary = 'Closed as ' + action;

                    localStorage.setItem('vfdPrefillContent', topText + bottomText);
                    localStorage.setItem('vfdPrefillSummary', summary);

                    // Redirect to edit page
                    window.location.href = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'edit' });
                });
            }

            addClosureButton('Close as delete', 'delete');
            addClosureButton('Close as keep', 'keep');
            addClosureButton('Close as no consensus', 'no consensus');

        }

        // On edit page: insert stored content
        if (mw.config.get('wgAction') === 'edit') {
            const textbox = document.getElementById('wpTextbox1');
            const summaryBox = document.getElementById('wpSummary');

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

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

                    // Clear storage after use
                    localStorage.removeItem('vfdPrefillContent');
                    localStorage.removeItem('vfdPrefillSummary');
                }
            }
        }

    }
});
//</nowiki>