User:Euphoria/common.js: Difference between revisions

From Test Wiki
Content deleted Content added
Tooltip added
mw.notify
Line 3: Line 3:
const pagePrefix = 'User:Euphoria/TestVfD';
const pagePrefix = 'User:Euphoria/TestVfD';


// Only run on relevant pages in view mode
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;


Line 9: Line 10:
const heading = this;
const heading = this;


// Skip if wrapped inside .vfd
// Skip headings already inside a .vfd container
if ($(heading).closest('.vfd').length) return;
if ($(heading).closest('.vfd').length) return;


// Skip if no wikilinks in this heading
// Skip headings without links
if (!$(heading).find('a').length) return;
if (!$(heading).find('a').length) return;


Line 27: Line 28:
const btn = document.createElement('button');
const btn = document.createElement('button');
btn.textContent = actionObj.name.charAt(0).toUpperCase(); // D/K/N
btn.textContent = actionObj.name.charAt(0).toUpperCase(); // D/K/N
btn.title = 'Close as ' + actionObj.name;
btn.title = 'Close as ' + actionObj.name;


// Ultra-small styling
// Small inline button styling
btn.style.width = '16px';
btn.style.width = '16px';
btn.style.height = '16px';
btn.style.height = '16px';
Line 50: Line 51:
if (!confirm('Are you sure you want to close as ' + actionObj.name + '?')) return;
if (!confirm('Are you sure you want to close as ' + actionObj.name + '?')) return;


// Disable button to prevent double clicks
btn.disabled = true; // prevent double-clicks
btn.disabled = true;

const api = new mw.Api();
const api = new mw.Api();


// Step 1: Get discussion page content
// Fetch discussion page content
api.get({
api.get({
action: 'query',
action: 'query',
Line 72: Line 71:
const discussionNewContent = topText + content.trim() + bottomText;
const discussionNewContent = topText + content.trim() + bottomText;


// Step 2: Edit discussion page
// Edit discussion page
api.postWithToken('csrf', {
api.postWithToken('csrf', {
action: 'edit',
action: 'edit',
Line 82: Line 81:
const link = $(heading).find('a').first();
const link = $(heading).find('a').first();
if (!link.length) {
if (!link.length) {
alert('Cannot find target article link in heading!');
mw.notify('Cannot find target article link!', {title: 'VfDcloser', type: 'error'});
location.reload();
location.reload();
return;
return;
Line 89: Line 88:


if (actionObj.name === 'delete') {
if (actionObj.name === 'delete') {
// Delete target article
// Delete target article and talk page
api.postWithToken('csrf', {
api.postWithToken('csrf', {action: 'delete', title: targetPage, reason: '[[' + mw.config.get('wgPageName') + ']]'})
action: 'delete',
.done(() => {
title: targetPage,
const talkPage = 'Talk:' + targetPage;
reason: '[[' + mw.config.get('wgPageName') + ']]'
api.postWithToken('csrf', {action: 'delete', title: talkPage, reason: 'Parent page deleted via VfD'})
}).done(() => {
.done(() => {
const talkPage = 'Talk:' + targetPage;
mw.notify('Discussion closed and "' + targetPage + '" deleted with talk page.', {title: 'VfDcloser', type: 'success'});
api.postWithToken('csrf', {
location.reload();
action: 'delete',
}).fail(err => mw.notify('Error deleting talk page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error'}));
title: talkPage,
}).fail(err => mw.notify('Error deleting page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error'}));
reason: 'Parent page deleted via VfD'
}).done(() => {
alert('Discussion closed and "' + targetPage + '" along with its talk page deleted.');
location.reload();
}).fail(err => alert('Error deleting talk page: ' + err));
}).fail(err => alert('Error deleting page: ' + err));
} else {
} else {
// Keep / No consensus
// Keep / No consensus: update target and talk page
api.get({
api.get({
action: 'query',
action: 'query',
Line 154: Line 147:
minor: true
minor: true
}).done(() => {
}).done(() => {
alert('Discussion closed and "' + targetPage + '" updated. Talk page updated.');
mw.notify('Discussion closed and "' + targetPage + '" updated. Talk page updated.', {title: 'VfDcloser', type: 'success'});
location.reload();
location.reload();
}).fail(err => alert('Error editing talk page: ' + err));
}).fail(err => mw.notify('Error editing talk page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error'}));
});
});
}).fail(err => alert('Error editing article: ' + err));
}).fail(err => mw.notify('Error editing article: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error'}));
});
});
}
}
}).fail(err => alert('Error editing discussion page: ' + err));
}).fail(err => mw.notify('Error editing discussion page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error'}));
});
});
});
});