User:Euphoria/common.js
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', 'mediawiki.api'], function () {
const pagePrefix = 'User:Euphoria/Test VfD';
const currentPage = mw.config.get('wgPageName').replace(/_/g, ' ');
if (!currentPage.startsWith(pagePrefix + '/') || mw.config.get('wgAction') !== 'view') return;
const api = new mw.Api();
// --- Universal API helpers ---
function apiEdit(title, content, summary, callback) {
api.postWithToken('csrf', { action: 'edit', title, text: content, summary, minor: true })
.done(callback)
.fail(err => mw.notify(`Error editing "${title}": ${JSON.stringify(err)}`, { title: 'VfDcloser', type: 'error', timeout: 1500 }));
}
function apiDelete(title, reason, callback) {
api.postWithToken('csrf', { action: 'delete', title, reason })
.done(callback)
.fail(err => mw.notify(`Error deleting "${title}": ${JSON.stringify(err)}`, { title: 'VfDcloser', type: 'error', timeout: 1500 }));
}
function fetchPage(title, callback) {
api.get({ action: 'query', prop: 'revisions', titles: title, rvslots: 'main', rvprop: 'content', format: 'json' })
.done(data => {
const pages = data.query.pages;
const pageId = Object.keys(pages)[0];
const content = (pageId !== '-1' && pages[pageId].revisions) ? pages[pageId].revisions[0].slots.main['*'] : '';
callback(content.trim());
});
}
// --- Main action handler ---
function handleAction(targetPage, actionName) {
fetchPage(currentPage, discussionContent => {
const newDiscussion = `{{subst:vt|${actionName}. --~~~~}}\n${discussionContent}\n{{subst:vb}}`;
apiEdit(currentPage, newDiscussion, `Closed as ${actionName}`, () => {
if (actionName === 'delete') {
// Delete target page
apiDelete(targetPage, `[[${currentPage}]]`, () => {
// Check if talk page exists before deleting
fetchPage('Talk:' + targetPage, talkContent => {
if (talkContent) {
apiDelete('Talk:' + targetPage, 'Parent page deleted via VfD', () => {
mw.notify('Discussion closed. Page and talk page deleted.', { title: 'VfDcloser', type: 'success', timeout: 1500 });
setTimeout(() => location.reload(), 1500);
});
} else {
mw.notify('Discussion closed. Page deleted.', { title: 'VfDcloser', type: 'success', timeout: 1500 });
setTimeout(() => location.reload(), 1500);
}
});
});
} else {
// Keep / No Consensus: update target page and talk page
fetchPage(targetPage, targetContent => {
targetContent = targetContent.replace(/\{\{vfd-new\}\}/gi, '').trim();
apiEdit(targetPage, targetContent, `VFD closed as ${actionName}`, () => {
const talkTemplate = '{{vfd-kept-new}}';
fetchPage('Talk:' + targetPage, talkContent => {
talkContent = talkContent ? `${talkTemplate}\n${talkContent}` : talkTemplate;
apiEdit('Talk:' + targetPage, talkContent, `VFD closed as ${actionName}`, () => {
mw.notify('Discussion closed. Page and talk page updated.', { title: 'VfDcloser', type: 'success', timeout: 1500 });
setTimeout(() => location.reload(), 1500);
});
});
});
});
}
});
});
}
// --- Build buttons ---
$(function () {
$('#mw-content-text').find('h2, h3').each(function () {
const heading = this;
// Skip page if in Category:VfD archive entries
const categories = mw.config.get('wgCategories') || [];
if (categories.includes('VfD archive entries')) return;
const link = $(heading).find('a').first();
if (!link.length) return;
const targetPage = link.attr('title') || link.text().trim();
if (!targetPage) return;
const container = document.createElement('span');
container.style.marginLeft = '6px';
const actions = [
{ name: 'delete', color: '#e74c3c' },
{ name: 'keep', color: '#27ae60' },
{ name: 'no consensus', color: '#f1c40f' }
];
actions.forEach(actionObj => {
const btn = document.createElement('button');
btn.textContent = actionObj.name.charAt(0).toUpperCase();
btn.title = 'Close as ' + actionObj.name;
Object.assign(btn.style, {
width: '16px', height: '16px', fontSize: '65%',
padding: '0', marginRight: '3px', border: 'none',
borderRadius: '2px', backgroundColor: actionObj.color,
color: '#fff', cursor: 'pointer', verticalAlign: 'middle',
transition: '0.15s'
});
btn.addEventListener('mouseenter', () => btn.style.filter = 'brightness(1.3)');
btn.addEventListener('mouseleave', () => btn.style.filter = 'brightness(1)');
btn.addEventListener('click', e => {
e.preventDefault();
if (!confirm(`Are you sure you want to close as ${actionObj.name}?`)) return;
btn.disabled = true;
handleAction(targetPage, actionObj.name);
});
container.appendChild(btn);
});
heading.appendChild(container);
});
});
});
//</nowiki>