User:Euphoria/common.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
fix
fix
Line 1: Line 1:
//<nowiki>
//<nowiki>
mw.loader.using('mediawiki.util', function () {
mw.loader.using(['mediawiki.util', 'mediawiki.api'], function () {
const pagePrefix = 'User:Euphoria/TestVfD/';
const pagePrefix = 'User:Euphoria/TestVfD/';


if (!mw.config.get('wgPageName').startsWith(pagePrefix) || mw.config.get('wgAction') !== 'view') return;
if (!mw.config.get('wgPageName').startsWith(pagePrefix) || mw.config.get('wgAction') !== 'view') return;


// Wait for page content to be ready
$(function() {
$(function() {
// Find all h2/h3 in the content area
$('#mw-content-text').find('h2, h3').each(function() {
$('#mw-content-text').find('h2, h3').each(function() {
const heading = this;
const heading = this;
Line 22: Line 20:
btn.style.marginRight = '5px';
btn.style.marginRight = '5px';
btn.style.fontSize = '90%';
btn.style.fontSize = '90%';

btn.addEventListener('click', function(e) {
btn.addEventListener('click', function(e) {
e.preventDefault();
e.preventDefault();

// Confirmation dialog
if (!confirm('Are you sure you want to close the section "' + sectionName + '" as ' + action + '?')) {
return; // user cancelled
}

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


localStorage.setItem('vfdPrefillContent', JSON.stringify({ top: topText, bottom: bottomText }));
const api = new mw.Api();
localStorage.setItem('vfdPrefillSummary', summary);


window.location.href = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'edit' });
api.get({
action: 'query',
prop: 'revisions',
titles: mw.config.get('wgPageName'),
rvprop: 'content',
format: 'json'
}).done(function(data) {
const pages = data.query.pages;
const pageId = Object.keys(pages)[0];
let content = pages[pageId].revisions[0]['*'];

content = content.replace(/^\s+/, '').replace(/\s+$/, '');
const newContent = topText + content + bottomText;

api.postWithToken('csrf', {
action: 'edit',
title: mw.config.get('wgPageName'),
text: newContent,
summary: summary,
minor: true,
bot: true
}).done(function() {
alert('Section "' + sectionName + '" closed as ' + action + '. Page updated!');
location.reload();
}).fail(function(err) {
alert('Error closing section: ' + err);
});
});
});
});

container.appendChild(btn);
container.appendChild(btn);
});
});

Revision as of 08:27, 25 September 2025

//<nowiki>
mw.loader.using(['mediawiki.util', 'mediawiki.api'], function () {
    const pagePrefix = 'User:Euphoria/TestVfD/';

    if (!mw.config.get('wgPageName').startsWith(pagePrefix) || mw.config.get('wgAction') !== 'view') return;

    $(function() {
        $('#mw-content-text').find('h2, h3').each(function() {
            const heading = this;
            const sectionName = heading.textContent.replace('[edit]', '').trim();
            if (!sectionName) return;

            const container = document.createElement('span');
            container.style.marginLeft = '10px';

            ['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();

                    // Confirmation dialog
                    if (!confirm('Are you sure you want to close the section "' + sectionName + '" as ' + action + '?')) {
                        return; // user cancelled
                    }

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

                    const api = new mw.Api();

                    api.get({
                        action: 'query',
                        prop: 'revisions',
                        titles: mw.config.get('wgPageName'),
                        rvprop: 'content',
                        format: 'json'
                    }).done(function(data) {
                        const pages = data.query.pages;
                        const pageId = Object.keys(pages)[0];
                        let content = pages[pageId].revisions[0]['*'];

                        content = content.replace(/^\s+/, '').replace(/\s+$/, '');
                        const newContent = topText + content + bottomText;

                        api.postWithToken('csrf', {
                            action: 'edit',
                            title: mw.config.get('wgPageName'),
                            text: newContent,
                            summary: summary,
                            minor: true,
                            bot: true
                        }).done(function() {
                            alert('Section "' + sectionName + '" closed as ' + action + '. Page updated!');
                            location.reload();
                        }).fail(function(err) {
                            alert('Error closing section: ' + err);
                        });
                    });
                });

                container.appendChild(btn);
            });

            heading.appendChild(container);
        });
    });
});
//</nowiki>