User:Euphoria/common.js: Difference between revisions

From Test Wiki
Content deleted Content added
fix
Tag: Reverted
fix
Tag: Reverted
Line 3:
const pagePrefix = 'User:Euphoria/TestVfD';
 
// Only run on allowed pages
if (!mw.config.get('wgPageName').startsWith(pagePrefix) || mw.config.get('wgAction') !== 'view') return;
 
$(async function () {
const api = new }mw.Api();
 
$('#mw-content-text').find('h2, h3').each(function () {
const heading = this;
Line 13 ⟶ 14:
if ($(heading).closest('.vfd').length) return;
 
// Collect section text (everything until next h2/h3)
let nextNode = heading.nextSibling;
let sectionText = '';
while $(nextNode && !/H[23]/heading).testnextUntil(nextNode.nodeName'h2,h3').each(function () {
sectionText += nextNode$(this).textContent || ''text();
nextNode = nextNode.nextSibling});
 
}
// Only add buttons if section contains [[TargetPage]] link
ifconst linkMatch = heading.textContent.match(!/\[\[([^\]]+)\]\]/) || sectionText.match(/\[\[([^\]]+)\]\]/)) return;
if (!linkMatch) return;
 
//const OnlytargetPage proceed if section contains a= linkMatch[[link]1];
if (!sectionText.match(/\[\[([^\]]+)\]\]/)) return;
 
// Create button container
const container = document.createElement('span');
container.style.marginLeft = '6px';
 
const actions = [
{ name: 'delete', color: '#e74c3c' }, // red
{ name: 'keep', color: '#27ae60' }, // green
{ name: 'no consensus', color: '#f1c40f' } // yellow
];
 
// Createconst buttons = [];
 
actions.forEach(function (actionObj) => {
const btn = document.createElement('button');
btn.textContent = actionObj.name.charAt(0).toUpperCase(); // D/K/N
 
// Ultra-small styling
Object.assign(btn.style, {
width: '16px',
Line 55 ⟶ 56:
});
 
// Hover effect
btn.addEventListener('mouseenter', () => btn.style.filter = 'brightness(1.3)');
btn.addEventListener('mouseleave', () => btn.style.filter = 'brightness(1)');
 
//btn.addEventListener('click', Clickasync handlerfunction (e) {
btn.addEventListener('click', function (e) {
e.preventDefault();
if (!confirm('Are you sure you want to close as ' + actionObj.name + '?')) return;
 
const// apiDisable =all newbuttons mw.Api();in this section
buttons.forEach(b => b.disabled = true);
btn.textContent = '…'; // processing indicator
 
// Ultra-small styling try {
// Step 1:1️⃣ Get discussion page content
const data = await api.get({
action: 'query',
prop: 'revisions',
titles: mw.config.get('wgPageName'),
rvprop: 'content',
format: 'json'
});
 
// Step 1: Get discussion page content
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]['*'];
const discussionNewContent = `{{subst:vt|${actionObj.name}. --~~~~}}\n${content.trim()}\n{{subst:vb}}`;
 
// Wrap2️⃣ Edit discussion contentpage
constawait discussionNewContent = '{{subst:vt|' + actionObjapi.name + postWithToken('. --~~~~}}\ncsrf', +{
content.trim() +
'\n{{subst:vb}}';
 
// Step 2: Edit discussion page
api.postWithToken('csrf', {
action: 'edit',
title: mw.config.get('wgPageName'),
Line 91 ⟶ 90:
minor: true,
bot: true
}).done(function () {;
// Extract target article from heading
const match = content.match(/==\s*\[\[([^\]]+)\]\]\s*==/);
if (!match) {
alert('Cannot find target article in heading!');
location.reload();
return;
}
const targetPage = match[1];
 
// 3️⃣ Handle target if (actionObj.name === 'delete') {page
if (actionObj.name === 'delete') deletePageAndTalk(api, targetPage);{
}await elsedeletePageAndTalk(api, {targetPage);
} else keepOrNoConsensus(api, targetPage, actionObj.name);{
}await keepOrNoConsensus(api, targetPage, actionObj.name);
}).fail(err => alert('Error editing discussion page: ' + err));
 
});
}).done(function catch (dataerr) {
// Step 2alert('Error: Edit' discussion+ pageerr);
buttons.forEach(b => b.disabled = }false);
buttons.forEach(b => b.textContent = b.textContent.charAt(0).toUpperCase());
// Hover effect }
});
 
container.appendChild(btn);
if buttons.push(!matchbtn) {;
});
 
Line 116 ⟶ 113:
});
 
// Delete===== targetHelper articlefunctions and its talk page=====
async function deletePageAndTalk(api, targetPage) {
api.postWithToken('csrf',try {
action:await api.postWithToken('deletecsrf', {
title action: targetPage'delete',
reason: '[[' + mw.config.get('wgPageName') +title: ']]'targetPage,
bot reason: true'[[' + mw.config.get('wgPageName') + ']]',
}).done(() => { bot: true
});
 
const talkPage = 'Talk:' + targetPage;
await api.postWithToken('csrf', {
action: 'delete',
title: talkPage,
reason: 'Parent page deleted via VfD closure [[' + mw.config.get('wgPageName') + ']]',
bot: true
}).done(() => {;
 
alert('Discussion closed and "' + targetPage + '" along with its talk page deleted.');
alert('Discussion closed and "' location+ targetPage + '" along with its talk page deleted.reload(');
})location.failreload(err => alert('Error deleting talk page: ' + err));
}).fail(err =>catch alert('Error deleting page: ' + err)); {
.fail(err => alert('Error editingdeleting page/talk articlepage: ' + err));
}
}
 
async function keepOrNoConsensus(api, targetPage, actionName) {
// Handle keep / no consensus
try {
function keepOrNoConsensus(api, targetPage, actionName) {
// Edit article: remove const articleData = await api.get({{vfd-new}}
api.get({ action: 'query',
action prop: 'queryrevisions',
prop titles: 'revisions'targetPage,
titles rvprop: targetPage'content',
rvprop format: 'contentjson',
format: 'json'});
 
}).done(function (articleData) {
const articlePages = articleData.query.pages;
const articleId = Object.keys(articlePages)[0];
Line 152 ⟶ 153:
articleContent = articleContent.replace(/\{\{vfd-new\}\}/gi, '').trim();
 
await api.postWithToken('csrf', {
action: 'edit',
title: targetPage,
Line 159 ⟶ 160:
minor: true,
bot: true
}).done(() => updateTalkPage(api, targetPage, actionName));
 
.fail(err => alert('Error editing article: ' + err));
} await updateTalkPage(api, targetPage, actionName);
 
}).done(function catch (articleDataerr) {
alert('CannotError find targetediting article: in' heading!'+ err);
}
}
 
async function updateTalkPage(api, targetPage, actionName) {
// Update talk page for keep / no consensus
function updateTalkPage(api, targetPage, actionName) try {
const talkPage = 'Talk:' + targetPage;
const talkData = await api.get({
action: 'query',
prop: 'revisions',
titles: talkPage,
rvprop: 'content',
format: 'json'
}).done(function (talkData) { });
 
const talkPages = talkData.query.pages;
const talkId = Object.keys(talkPages)[0];
Line 184 ⟶ 190:
}
 
await api.postWithToken('csrf', {
action: 'edit',
title: talkPage,
Line 191 ⟶ 197:
minor: true,
bot: true
}).done(() => {;
 
alert('Discussion closed and "' + targetPage + '" updated. Talk page updated.');
alert('Discussion closed and "' location+ targetPage + '" updated.reload( Talk page updated.');
})location.failreload(err => alert('Error editing talk page: ' + err));
 
});
} catch (err) {
alert('Error editing talk page: ' + err);
}
}
 
});
});