User:Euphoria/common.js: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
fix |
fix |
||
| Line 8: | Line 8: | ||
const api = new mw.Api(); |
const api = new mw.Api(); |
||
// |
// API helper functions |
||
const apiHelper = { |
const apiHelper = { |
||
edit(title, content, summary, callback) { |
edit(title, content, summary, callback) { |
||
| Line 31: | Line 31: | ||
}; |
}; |
||
// |
// Handle VfD action for a target page |
||
function handleAction(targetPage, actionName) { |
function handleAction(targetPage, actionName) { |
||
apiHelper.fetch(currentPage, discussionContent => { |
apiHelper.fetch(currentPage, discussionContent => { |
||
// Prompt user for an optional comment |
|||
const newDiscussion = `{{subst:vt|${actionName}. --~~~~}}\n${discussionContent}\n{{subst:vb}}`; |
|||
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(currentPage, newDiscussion, `Closed as ${actionName} ([[User:Euphoria/VfDcloser|VfDcloser]])`, () => { |
|||
if (actionName === 'delete') { |
if (actionName === 'delete') { |
||
deletePageAndTalk(targetPage); |
deletePageAndTalk(targetPage); |
||
| Line 43: | Line 46: | ||
keepOrNoConsensus(targetPage, actionName); |
keepOrNoConsensus(targetPage, actionName); |
||
} |
} |
||
}); |
}); |
||
}); |
}); |
||
| Line 49: | Line 51: | ||
function deletePageAndTalk(title) { |
function deletePageAndTalk(title) { |
||
apiHelper.delete(title, `[[${currentPage}]]`, () => { |
apiHelper.delete(title, `[[${currentPage}]] ([[User:Euphoria/VfDcloser|VfDcloser]])`, () => { |
||
apiHelper.fetch('Talk:' + title, talkContent => { |
apiHelper.fetch('Talk:' + title, talkContent => { |
||
if (talkContent) { |
if (talkContent) { |
||
apiHelper.delete('Talk:' + title, 'Parent page deleted via VfD', () => { |
apiHelper.delete('Talk:' + title, 'Parent page deleted via VfD ([[User:Euphoria/VfDcloser|VfDcloser]])', () => { |
||
notifyAndReload('Discussion closed. Page and talk page deleted.'); |
notifyAndReload('Discussion closed. Page and talk page deleted.'); |
||
}); |
}); |
||
| Line 65: | Line 67: | ||
apiHelper.fetch(title, content => { |
apiHelper.fetch(title, content => { |
||
const updatedContent = content.replace(/\{\{vfd-new\}\}/gi, '').trim(); |
const updatedContent = content.replace(/\{\{vfd-new\}\}/gi, '').trim(); |
||
apiHelper.edit(title, updatedContent, `VFD closed as ${actionName}`, () => { |
apiHelper.edit(title, updatedContent, `VFD closed as ${actionName} ([[User:Euphoria/VfDcloser|VfDcloser]])`, () => { |
||
updateTalkPage(title, actionName); |
updateTalkPage(title, actionName); |
||
}); |
}); |
||
| Line 75: | Line 77: | ||
apiHelper.fetch('Talk:' + title, talkContent => { |
apiHelper.fetch('Talk:' + title, talkContent => { |
||
const updatedTalk = talkContent ? `${talkTemplate}\n${talkContent}` : talkTemplate; |
const updatedTalk = talkContent ? `${talkTemplate}\n${talkContent}` : talkTemplate; |
||
apiHelper.edit('Talk:' + title, updatedTalk, `VFD closed as ${actionName}`, () => { |
apiHelper.edit('Talk:' + title, updatedTalk, `VFD closed as ${actionName} ([[User:Euphoria/VfDcloser|VfDcloser]])`, () => { |
||
notifyAndReload('Discussion closed. Page and talk page updated.'); |
notifyAndReload('Discussion closed. Page and talk page updated.'); |
||
}); |
}); |
||
| Line 86: | Line 88: | ||
} |
} |
||
// |
// Create UI buttons for each heading |
||
function createButtonsForHeading(heading, targetPage) { |
function createButtonsForHeading(heading, targetPage) { |
||
const container = document.createElement('span'); |
const container = document.createElement('span'); |
||
| Line 157: | Line 159: | ||
} |
} |
||
// |
// Initialize buttons on page load |
||
$(function () { |
$(function () { |
||
const categories = mw.config.get('wgCategories') || []; |
const categories = mw.config.get('wgCategories') || []; |
||
Revision as of 07:03, 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();
// 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) {
apiHelper.fetch(currentPage, discussionContent => {
// Prompt user for an optional comment
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(currentPage, 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 () {
const categories = mw.config.get('wgCategories') || [];
if (categories.includes('VfD archive entries')) return;
$('#mw-content-text').find('h2').each(function () {
const headingText = $(this).text().trim();
if (!headingText) return;
createButtonsForHeading(this, headingText);
});
});
});
//</nowiki>