User:Euphoria/common.js
From Test Wiki
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 + '/') && currentPage !== pagePrefix) || mw.config.get('wgAction') !== 'view') return;
const api = new mw.Api();
// API helper functions
const apiHelper = {
edit(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 }));
},
delete(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 }));
},
fetch(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());
});
}
};
// Handle VfD action for a target page
function handleAction(targetPage, actionName) {
const pageToEdit = (currentPage === pagePrefix) ? pagePrefix + '/' + targetPage : currentPage;
apiHelper.fetch(pageToEdit, discussionContent => {
const userComment = prompt('Add an additional comment (optional):', '');
const commentText = userComment ? ` ${userComment}` : '';
const newDiscussion = `{{subst:vt|${actionName}.${commentText} --~~~~}}\n${discussionContent}\n{{subst:vb}}`;
apiHelper.edit(pageToEdit, newDiscussion, `Closed as ${actionName} ([[User:Euphoria/VfDcloser|VfDcloser]])`, () => {
if (actionName === 'delete') {
deletePageAndTalk(targetPage);
} else {
keepOrNoConsensus(targetPage, actionName);
}
});
});
}
function deletePageAndTalk(title) {
apiHelper.delete(title, `[[${currentPage}]] ([[User:Euphoria/VfDcloser|VfDcloser]])`, () => {
apiHelper.fetch('Talk:' + title, talkContent => {
if (talkContent) {
apiHelper.delete('Talk:' + title, 'Parent page deleted via VfD ([[User:Euphoria/VfDcloser|VfDcloser]])', () => {
notifyAndReload('Discussion closed. Page and talk page deleted.');
});
} else {
notifyAndReload('Discussion closed. Page deleted.');
}
});
});
}
function keepOrNoConsensus(title, actionName) {
apiHelper.fetch(title, content => {
const updatedContent = content.replace(/\{\{vfd-new\}\}/gi, '').trim();
apiHelper.edit(title, updatedContent, `VFD closed as ${actionName} ([[User:Euphoria/VfDcloser|VfDcloser]])`, () => {
updateTalkPage(title, actionName);
});
});
}
function updateTalkPage(title, actionName) {
const talkTemplate = '{{vfd-kept-new}}';
apiHelper.fetch('Talk:' + title, talkContent => {
const updatedTalk = talkContent ? `${talkTemplate}\n${talkContent}` : talkTemplate;
apiHelper.edit('Talk:' + title, updatedTalk, `VFD closed as ${actionName} ([[User:Euphoria/VfDcloser|VfDcloser]])`, () => {
notifyAndReload('Discussion closed. Page and talk page updated.');
});
});
}
function notifyAndReload(message) {
mw.notify(message, { title: 'VfDcloser', type: 'success', timeout: 1500 });
setTimeout(() => location.reload(), 1500);
}
// Create UI buttons for each heading
function createButtonsForHeading(heading, targetPage) {
const container = document.createElement('span');
container.style.marginLeft = '6px';
const actions = [
{ name: 'delete', color: '#e74c3c', symbol: '✖' },
{ name: 'keep', color: '#27ae60', symbol: '✔' },
{ name: 'no consensus', color: '#f1c40f', symbol: '⚖' }
];
actions.forEach(actionObj => {
const btn = document.createElement('a');
btn.href = '#';
btn.textContent = actionObj.symbol;
btn.title = 'Close as ' + actionObj.name;
btn.className = 'vfd-action-link';
btn.dataset.disabled = 'false';
Object.assign(btn.style, {
display: 'inline-block',
width: '18px',
height: '18px',
textAlign: 'center',
lineHeight: '18px',
fontSize: '12px',
fontWeight: 'bold',
borderRadius: '2px',
backgroundColor: actionObj.color,
color: '#fff',
marginRight: '4px',
textDecoration: 'none',
cursor: 'pointer',
transition: '0.15s'
});
btn.addEventListener('mouseenter', () => {
if (btn.dataset.disabled === 'true') return;
btn.style.filter = 'brightness(1.3)';
btn.style.transform = 'scale(1.2)';
});
btn.addEventListener('mouseleave', () => {
if (btn.dataset.disabled === 'true') return;
btn.style.filter = 'brightness(1)';
btn.style.transform = 'scale(1)';
});
btn.addEventListener('click', e => {
e.preventDefault();
if (btn.dataset.disabled === 'true') return;
if (!confirm(`Are you sure you want to close as ${actionObj.name}?`)) return;
// Disable all buttons under this heading
$(heading).find('.vfd-action-link').each((i, b) => {
b.style.opacity = '0.5';
b.style.cursor = 'not-allowed';
b.dataset.disabled = 'true';
b.style.filter = 'brightness(1)';
b.style.transform = 'scale(1)';
});
handleAction(targetPage, actionObj.name);
});
container.appendChild(btn);
});
heading.appendChild(container);
}
// Initialize buttons on page load
$(function () {
// Look for all h2 headings (works on both main page and subpages)
$('#mw-content-text').find('h2').each(function () {
const heading = $(this);
// Skip heading if inside an archived discussion div
if (heading.closest('.vfd').length) return;
const link = heading.find('a').first();
// Only add buttons if the heading contains a link
if (!link.length) return;
const targetPage = link.attr('title'); // Use the linked page as target
createButtonsForHeading(this, targetPage);
});
});
});
//</nowiki>