User:Euphoria/common.js: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
fix |
Cleanup Tag: Blanked |
||
| (15 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
//<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 helpers --- |
|||
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 })); |
|||
} |
|||
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 })); |
|||
} |
|||
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()); |
|||
}); |
|||
} |
|||
// --- 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') { |
|||
apiDelete(targetPage, `[[${currentPage}]]`, () => { |
|||
fetchPage('Talk:' + targetPage, talkContent => { |
|||
if (talkContent) { |
|||
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 { |
|||
mw.notify('Discussion closed. Page deleted.', { title: 'VfDcloser', type: 'success', timeout: 1500 }); |
|||
setTimeout(() => location.reload(), 1500); |
|||
} |
|||
}); |
|||
}); |
|||
} else { |
|||
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 gadget-style buttons --- |
|||
$(function () { |
|||
$('#mw-content-text').find('h2').each(function () { |
|||
const heading = this; |
|||
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', 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'; |
|||
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', () => { btn.style.filter = 'brightness(1.3)'; btn.style.transform = 'scale(1.2)'; }); |
|||
btn.addEventListener('mouseleave', () => { btn.style.filter = 'brightness(1)'; btn.style.transform = 'scale(1)'; }); |
|||
btn.addEventListener('click', e => { |
|||
e.preventDefault(); |
|||
if (!confirm(`Are you sure you want to close as ${actionObj.name}?`)) return; |
|||
btn.style.opacity = '0.5'; |
|||
btn.style.cursor = 'not-allowed'; |
|||
handleAction(targetPage, actionObj.name); |
|||
}); |
|||
container.appendChild(btn); |
|||
}); |
|||
heading.appendChild(container); |
|||
}); |
|||
}); |
|||
}); |
|||
//</nowiki> |
|||