User:Euphoria/common.js: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
fix |
fix |
||
| Line 1: | Line 1: | ||
//<nowiki> |
//<nowiki> |
||
mw.loader.using(['mediawiki.util', 'mediawiki.api |
mw.loader.using(['mediawiki.util', 'mediawiki.api'], function () { |
||
const pagePrefix = 'User:Euphoria/Test VfD'; |
const pagePrefix = 'User:Euphoria/Test VfD'; |
||
// Only run on relevant pages in view mode |
|||
const currentPage = mw.config.get('wgPageName').replace(/_/g, ' '); |
const currentPage = mw.config.get('wgPageName').replace(/_/g, ' '); |
||
if (!currentPage.startsWith(pagePrefix + '/') || mw.config.get('wgAction') !== 'view') return; |
|||
if ( |
|||
!currentPage.startsWith(pagePrefix + '/') || |
|||
mw.config.get('wgAction') !== 'view' |
|||
) return; |
|||
const api = new mw.Api(); |
|||
$(function () { |
|||
$('#mw-content-text').find('h2, h3').each(function () { |
|||
const heading = this; |
|||
// --- Universal API edit helper --- |
|||
// Skip headings already inside a .vfd container or without links |
|||
function apiEdit(title, content, summary, callback) { |
|||
if ($(heading).closest('.vfd').length) return; |
|||
api.postWithToken('csrf', { action: 'edit', title, text: content, summary, minor: true }) |
|||
if (!$(heading).find('a').length) return; |
|||
.done(callback) |
|||
.fail(err => mw.notify(`Error editing "${title}": ${JSON.stringify(err)}`, { title: 'VfDcloser', type: 'error', timeout: 1500 })); |
|||
} |
|||
// --- Universal delete helper --- |
|||
const container = document.createElement('span'); |
|||
function apiDelete(title, reason, callback) { |
|||
container.style.marginLeft = '6px'; |
|||
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 page content helper --- |
|||
const actions = [ |
|||
function fetchPage(title, callback) { |
|||
{ name: 'delete', colorClass: 'oo-ui-destructiveElement' }, |
|||
api.get({ action: 'query', prop: 'revisions', titles: title, rvslots: 'main', rvprop: 'content', format: 'json' }) |
|||
{ name: 'keep', colorClass: 'oo-ui-progressiveElement' }, |
|||
.done(data => { |
|||
{ name: 'no consensus', colorClass: 'oo-ui-warningElement' } |
|||
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 --- |
|||
actions.forEach(function (actionObj) { |
|||
function handleAction(targetPage, actionName) { |
|||
const btn = new OO.ui.ButtonWidget({ |
|||
fetchPage(currentPage, discussionContent => { |
|||
label: actionObj.name.charAt(0).toUpperCase(), |
|||
const newDiscussion = `{{subst:vt|${actionName}. --~~~~}}\n${discussionContent}\n{{subst:vb}}`; |
|||
title: 'Close as ' + actionObj.name, |
|||
apiEdit(currentPage, newDiscussion, `Closed as ${actionName}`, () => { |
|||
classes: ['vfd-btn', actionObj.colorClass], |
|||
flags: ['small'] // small button size |
|||
}); |
|||
if (actionName === 'delete') { |
|||
// Delete target and talk page |
|||
apiDelete(targetPage, `[[${currentPage}]]`, () => { |
|||
if (!confirm('Are you sure you want to close as ' + actionObj.name + '?')) return; |
|||
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 { |
|||
// Keep / No Consensus: clean target page and update 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 --- |
|||
btn.setDisabled(true); |
|||
$(function () { |
|||
const api = new mw.Api(); |
|||
$('#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; |
|||
action: 'query', |
|||
prop: 'revisions', |
|||
titles: currentPage, |
|||
rvslots: 'main', |
|||
rvprop: 'content', |
|||
format: 'json' |
|||
}).done(function (data) { |
|||
const pages = data.query.pages; |
|||
const pageId = Object.keys(pages)[0]; |
|||
let content = pages[pageId].revisions[0].slots.main['*']; |
|||
const link = $(heading).find('a').first(); |
|||
if (!link.length) return; |
|||
'{{subst:vt|' + actionObj.name + '. --~~~~}}\n' + |
|||
content.trim() + |
|||
'\n{{subst:vb}}'; |
|||
const targetPage = link.attr('title') || link.text().trim(); |
|||
if (!targetPage) return; |
|||
title: currentPage, |
|||
text: discussionNewContent, |
|||
summary: 'Closed as ' + actionObj.name, |
|||
minor: true |
|||
}).done(function () { |
|||
const link = $(heading).find('a').first(); |
|||
if (!link.length) { |
|||
mw.notify('Cannot find target page link!', { title: 'VfDcloser', type: 'error', timeout: 1500 }); |
|||
setTimeout(() => location.reload(), 1500); |
|||
return; |
|||
} |
|||
const container = document.createElement('span'); |
|||
container.style.marginLeft = '6px'; |
|||
if (!targetPage) { |
|||
mw.notify('Cannot determine target page title!', { title: 'VfDcloser', type: 'error', timeout: 1500 }); |
|||
setTimeout(() => location.reload(), 1500); |
|||
return; |
|||
} |
|||
const actions = [ |
|||
{ name: 'delete', color: '#e74c3c' }, |
|||
{ name: 'keep', color: '#27ae60' }, |
|||
{ name: 'no consensus', color: '#f1c40f' } |
|||
]; |
|||
reason: '[[' + currentPage + ']]' |
|||
}).done(() => { |
|||
const talkPage = 'Talk:' + targetPage; |
|||
api.get({ |
|||
action: 'query', |
|||
titles: talkPage, |
|||
format: 'json' |
|||
}).done(data => { |
|||
const talkPages = data.query.pages; |
|||
const talkId = Object.keys(talkPages)[0]; |
|||
actions.forEach(actionObj => { |
|||
const btn = document.createElement('button'); |
|||
btn.textContent = actionObj.name.charAt(0).toUpperCase(); |
|||
btn.title = 'Close as ' + actionObj.name; |
|||
reason: 'Parent page deleted via VfD' |
|||
}).done(() => { |
|||
mw.notify('Discussion closed. Page and talk page deleted.', { title: 'VfDcloser', type: 'success', timeout: 1500 }); |
|||
setTimeout(() => location.reload(), 1500); |
|||
}).fail(err => |
|||
mw.notify('Error deleting talk page: ' + JSON.stringify(err), { title: 'VfDcloser', type: 'error', timeout: 1500 }) |
|||
); |
|||
} else { |
|||
mw.notify('Discussion closed. Page deleted.', { title: 'VfDcloser', type: 'success', timeout: 1500 }); |
|||
setTimeout(() => location.reload(), 1500); |
|||
} |
|||
}); |
|||
}).fail(err => |
|||
mw.notify('Error deleting page: ' + JSON.stringify(err), { title: 'VfDcloser', type: 'error', timeout: 1500 }) |
|||
); |
|||
} else { |
|||
api.get({ |
|||
action: 'query', |
|||
prop: 'revisions', |
|||
titles: targetPage, |
|||
rvslots: 'main', |
|||
rvprop: 'content', |
|||
format: 'json' |
|||
}).done(function (articleData) { |
|||
const articlePages = articleData.query.pages; |
|||
const articleId = Object.keys(articlePages)[0]; |
|||
let articleContent = articlePages[articleId].revisions[0].slots.main['*']; |
|||
articleContent = articleContent.replace(/\{\{vfd-new\}\}/gi, '').trim(); |
|||
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' |
|||
}); |
|||
const talkPage = 'Talk:' + targetPage; |
|||
api.get({ |
|||
action: 'query', |
|||
prop: 'revisions', |
|||
titles: talkPage, |
|||
rvslots: 'main', |
|||
rvprop: 'content', |
|||
format: 'json' |
|||
}).done(function (talkData) { |
|||
const talkPages = talkData.query.pages; |
|||
const talkId = Object.keys(talkPages)[0]; |
|||
let talkContent = ''; |
|||
if (talkId !== '-1' && talkPages[talkId].revisions) { |
|||
talkContent = talkPages[talkId].revisions[0].slots.main['*']; |
|||
talkContent = '{{vfd-kept-new}}\n' + talkContent.trim(); |
|||
} else { |
|||
talkContent = '{{vfd-kept-new}}'; |
|||
} |
|||
btn.addEventListener('mouseenter', () => btn.style.filter = 'brightness(1.3)'); |
|||
api.postWithToken('csrf', { |
|||
btn.addEventListener('mouseleave', () => btn.style.filter = 'brightness(1)'); |
|||
action: 'edit', |
|||
btn.addEventListener('click', e => { |
|||
title: talkPage, |
|||
e.preventDefault(); |
|||
if (!confirm(`Are you sure you want to close as ${actionObj.name}?`)) return; |
|||
btn.disabled = true; |
|||
handleAction(targetPage, actionObj.name); |
|||
mw.notify('Discussion closed. Page and talk page updated.', { title: 'VfDcloser', type: 'success', timeout: 1500 }); |
|||
setTimeout(() => location.reload(), 1500); |
|||
}).fail(err => |
|||
mw.notify('Error editing talk page: ' + JSON.stringify(err), { title: 'VfDcloser', type: 'error', timeout: 1500 }) |
|||
); |
|||
}); |
|||
}).fail(err => |
|||
mw.notify('Error editing page: ' + JSON.stringify(err), { title: 'VfDcloser', type: 'error', timeout: 1500 }) |
|||
); |
|||
}); |
|||
} |
|||
}).fail(err => |
|||
mw.notify('Error editing discussion page: ' + JSON.stringify(err), { title: 'VfDcloser', type: 'error', timeout: 1500 }) |
|||
); |
|||
}); |
|||
}); |
}); |
||
container.appendChild(btn |
container.appendChild(btn); |
||
}); |
}); |
||
Revision as of 04:58, 1 October 2025
//<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 edit helper ---
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 }));
}
// --- Universal delete helper ---
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 }));
}
// --- Fetch page content helper ---
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 and talk page
apiDelete(targetPage, `[[${currentPage}]]`, () => {
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 {
// Keep / No Consensus: clean target page and update 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>