User:Euphoria/common.js: Difference between revisions

From Test Wiki
Content deleted Content added
fix
Tag: Reverted
restore
Tag: Manual revert
Line 1:
//<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;
 
$(async function () {
$('#mw-content-text').find('h2, h3').each(function () {
const api = new mw.Api();
 
$('#mw-content-text').find('h2, h3').each(function () {
const heading = this;
 
// Skip if already inside a .vfd section
if ($(heading).closest('.vfd').length) return;
 
// Grab all content until next heading
let sectionText = '';
$(heading).nextUntil('h2,h3').each(function () {
sectionText += $(this).text();
});
 
// Match [[Article]] or {{User:Euphoria/TestVfD/...}} template
const match = heading.textContent.match(/\[\[([^\]]+)\]\]/)
|| sectionText.match(/\[\[([^\]]+)\]\]/)
|| heading.textContent.match(/\{\{User:Euphoria\/TestVfD\/([^\}]+)\}\}/)
|| sectionText.match(/\{\{User:Euphoria\/TestVfD\/([^\}]+)\}\}/);
if (!match) return;
 
const targetPage = match[1];
 
const container = document.createElement('span');
Line 33 ⟶ 14:
 
const actions = [
{ name: 'delete', color: '#e74c3c' }, // red
{ name: 'keep', color: '#27ae60' }, // green
{ name: 'no consensus', color: '#f1c40f' } // yellow
];
 
constactions.forEach(function(actionObj) buttons = [];{
 
actions.forEach(actionObj => {
const btn = document.createElement('button');
btn.textContent = actionObj.name.charAt(0).toUpperCase(); // D/K/N
 
Object.assign(btn.style, {
// Ultra-small width: '16px',styling
btn.style.width height:= '16px',;
btn.style.height fontSize:= '65%16px',;
btn.style.fontSize padding:= '065%',;
btn.style.padding marginRight:= '3px0',;
btn.style.marginRight border:= 'none3px',;
btn.style.border borderRadius:= '2pxnone',;
btn.style.borderRadius = backgroundColor: actionObj.color,'2px';
btn.style.backgroundColor = actionObj.color: '#fff',;
btn.style.color cursor:= 'pointer#fff',;
btn.style.cursor verticalAlign:= 'middlepointer',;
btn.style.verticalAlign transition:= '0.15smiddle';
})btn.style.transition = '0.15s';
 
btn.addEventListener('mouseenter', () => btn.style.filter = 'brightness(1.3)');
}); // Hover
btn.addEventListener('mouseleave', () => btn.style.filter = 'brightness(1)');
btn.addEventListener('mouseenter', () => btn.style.filter = 'brightness(1.3)');
btn.addEventListener('mouseleave', () => btn.style.filter = 'brightness(1)');
 
btn.addEventListener('click', async function (e) {
e.preventDefault();
if (!confirm('Are you sure you want to close as ' + actionObj.name + '?')) return;
 
buttons.forEach(bconst api => bnew mw.disabled = trueApi();
btn.textContent = '…';
 
try// {Step 1: Get discussion page content
// Step 1: Edit discussion pageapi.get({
constaction: discData = await api.get({'query',
actionprop: 'queryrevisions',
proptitles: mw.config.get('revisionswgPageName'),
titlesrvprop: mw.config.get('wgPageNamecontent'),
rvpropformat: 'contentjson',
}).done(function(data) format: 'json'{
})const pages = data.query.pages;
const pages = discData.query.pages;
const pageId = Object.keys(pages)[0];
let content = pages[pageId].revisions[0]['*'];
 
const discussionNewContenttopText = `'{{subst:vt|${' + actionObj.name} + '. --~~~~}}\n${content.trim()}\n{{subst:vb}}`';
const bottomText = '\n{{subst:vb}}';
const discussionNewContent = topText + content.replace(/^\s+|\s+$/g,'') + bottomText;
 
await// api.postWithToken('csrf',Step {2: Edit discussion page
}api.postWithToken('csrf', {
action: 'edit',
title: mw.config.get('wgPageName'),
Line 89 ⟶ 71:
minor: true,
bot: true
});.done(function() {
// Extract target article from heading (assumes first [[...]] in content)
const match = heading.textContentcontent.match(/==\s*\[\[([^\]]+)\]\]\s*==/) ;
Object.assign if (btn.style,!match) {
alert('Error:Cannot 'find +target errarticle in heading!');
location.reload();
if (!match) return;
btn.textContent = '…'; }
|| sectionText. const targetPage = match(/\[\[([^\]]+)\]\1]/);
 
if (actionObj.name === 'delete') {
await api.postWithToken('csrf', { // Delete target article
action: api.postWithToken('deletecsrf', {
title action: targetPage'delete',
reason: '[[' + mw.config.get('wgPageName') +title: ']]'targetPage,
bot reason: true'[[' + mw.config.get('wgPageName') + ']]',
}); bot: true
await api }).postWithTokendone('csrf',() => {
action: ' // Also delete', talk page
title: const talkPage = 'Talk:' + targetPage,;
reason: 'Parent page deleted via VfD closure [[' + mwapi.config.getpostWithToken('wgPageNamecsrf'), + ']]',{
bot action: true'delete',
}); title: talkPage,
alert('Discussion closed and "' + targetPage + '" with talk reason: 'Parent page deleted via VfD closure [[' + mw.config.get('wgPageName'); + ']]',
location.reload(); bot: true
}).done(() else=> {
// Keep / No consensus alert('Discussion closed and "' + targetPage + '" along with its talk page deleted.');
const artData = await api location.getreload({);
action }).fail(err => alert('Error deleting talk page: 'query', + err));
prop}).fail(err => alert('Error deleting page: 'revisions', + err));
} else titles: targetPage,{
rvprop// Keep / No consensus: 'content',remove {{vfd-new}} from article
format: 'json'api.get({
}); action: 'query',
const artPages = artData.query.pages; prop: 'revisions',
const artId = Object.keys(artPages)[0]; titles: targetPage,
let articleContent = artPages[artId].revisions[0][ rvprop: '*content'];,
articleContent = articleContent.replace(/\{\{vfd-new\}\}/gi, format: 'json').trim();
bot:}).done(function(articleData) true{
const pagesarticlePages = discDataarticleData.query.pages;
const articleId = Object.keys(articlePages)[0];
text: let articleContent, = articlePages[articleId].revisions[0]['*'];
 
await api.postWithToken('csrf', // Remove {{vfd-new}}
action: articleContent = articleContent.replace(/\{\{vfd-new\}\}/gi, 'edit').replace(/^\s+|\s+$/g,'');
title: targetPage,
text: articleContent,
summary: 'VFD closed as ' + actionObj.name,
minor: true,
bot: true
});
 
// Update talkEdit pagearticle
const talkData = await api.getpostWithToken('csrf', {
action: 'queryedit',
prop title: 'revisions'targetPage,
titles: 'Talk:' + targetPage text: articleContent,
rvprop summary: 'contentVFD closed as ' + actionObj.name,
format minor: 'json'true,
}); bot: true
const talkPages = talkData }).query.pages;done(() => {
const talkId = Object.keys(talkPages)[0]; // Talk page
let talkContent const talkPage = 'Talk:' + targetPage;
if (talkId !== '-1' && talkPages[talkId].revisions) api.get({
talkContent = '{{vfd-kept-new}}\n' + talkPages[talkId].revisions[0]['*'].replace(/^\s+/, action: 'query');,
} else { prop: 'revisions',
talkContent = '{{vfd-kept-new}}'; titles: talkPage,
rvprop: 'content',
format: 'json'
bot: true }).done(function(talkData) {
const talkPages = talkData.query.pages;
const talkId = Object.keys(talkPages)[0];
text: let talkContent, = '';
if (talkId !== '-1' && talkPages[talkId].revisions) {
talkContent = talkPages[talkId].revisions[0]['*'];
talkContent = '{{vfd-kept-new}}\n' + talkContent.replace(/^\s+/, '');
} else {
talkContent = '{{vfd-kept-new}}';
});
 
api.postWithToken('csrf', {
action: 'edit',
title: talkPage,
text: talkContent,
summary: 'VFD closed as ' + actionObj.name,
minor: true,
minor bot: true,
});.done(() => {
alert('Discussion closed and "' + targetPage + '" updated. Talk page updated.');
location.reload();
}).fail(err => alert('Error editing talk page: ' + err));
title: targetPage, });
}).fail(err => alert('Error editing article: ' + err));
sectionText += $(this).text( });
}
}).fail(err => alert('Error editing awaitdiscussion page: api.postWithToken('csrf', {+ err));
action: 'edit',});
title: 'Talk:' + targetPage,
text: talkContent,
summary: 'VFD closed as ' + actionObj.name,
minor: true,
bot: true
});
alert('Discussion closed and "' + targetPage + '" updated. Talk page updated.');
location.reload();
}
 
} catch (err) {
alert('Error: ' + err);
buttons.forEach(b => { b.disabled = false; b.textContent = b.textContent.charAt(0).toUpperCase(); });
}
});
 
container.appendChild(btn);
buttons.push(btn);
});