User:Euphoria/common.js: Difference between revisions
From Test Wiki
Content deleted Content added
fix |
fix |
||
| Line 3: | Line 3: | ||
const pagePrefix = 'User:Euphoria/TestVfD'; |
const pagePrefix = 'User:Euphoria/TestVfD'; |
||
// Only run on relevant pages in view mode |
|||
if (!mw.config.get('wgPageName').startsWith(pagePrefix) || mw.config.get('wgAction') !== 'view') return; |
if (!mw.config.get('wgPageName').startsWith(pagePrefix) || mw.config.get('wgAction') !== 'view') return; |
||
| Line 9: | Line 10: | ||
const heading = this; |
const heading = this; |
||
// Skip headings already inside a .vfd container |
|||
if ($(heading).closest('.vfd').length) return; |
if ($(heading).closest('.vfd').length) return; |
||
// Skip headings without links |
|||
if (!$(heading).find('a').length) return; |
if (!$(heading).find('a').length) return; |
||
| Line 16: | Line 20: | ||
const actions = [ |
const actions = [ |
||
{name: 'delete', color: '#e74c3c'}, |
{name: 'delete', color: '#e74c3c'}, // red |
||
{name: 'keep', color: '#27ae60'}, |
{name: 'keep', color: '#27ae60'}, // green |
||
{name: 'no consensus', color: '#f1c40f'} |
{name: 'no consensus', color: '#f1c40f'} // yellow |
||
]; |
]; |
||
| Line 27: | Line 31: | ||
// Small inline button styling |
// Small inline button styling |
||
btn.style.width = '16px'; |
|||
btn.style.height = '16px'; |
|||
btn.style.fontSize = '65%'; |
|||
btn.style.padding = '0'; |
|||
btn.style.marginRight = '3px'; |
|||
btn.style.border = 'none'; |
|||
btn.style.borderRadius = '2px'; |
|||
btn.style.backgroundColor = actionObj.color; |
|||
btn.style.color = '#fff'; |
|||
btn.style.cursor = 'pointer'; |
|||
btn.style.verticalAlign = 'middle'; |
|||
btn.style.transition = '0.15s'; |
|||
transition: '0.15s' |
|||
}); |
|||
btn.addEventListener('mouseenter', () => btn.style.filter='brightness(1.3)'); |
btn.addEventListener('mouseenter', () => btn.style.filter='brightness(1.3)'); |
||
| Line 49: | Line 51: | ||
if (!confirm('Are you sure you want to close as ' + actionObj.name + '?')) return; |
if (!confirm('Are you sure you want to close as ' + actionObj.name + '?')) return; |
||
btn.disabled = true; |
btn.disabled = true; // prevent double-clicks |
||
const api = new mw.Api(); |
const api = new mw.Api(); |
||
// Fetch discussion page content |
|||
api.get({ |
api.get({ |
||
action: 'query', |
action: 'query', |
||
| Line 64: | Line 67: | ||
let content = pages[pageId].revisions[0].slots.main['*']; |
let content = pages[pageId].revisions[0].slots.main['*']; |
||
const |
const topText = '{{subst:vt|' + actionObj.name + '. --~~~~}}\n'; |
||
const bottomText = '\n{{subst:vb}}'; |
|||
const discussionNewContent = topText + content.trim() + bottomText; |
|||
// Edit discussion page |
|||
api.postWithToken('csrf', { |
api.postWithToken('csrf', { |
||
action: 'edit', |
action: 'edit', |
||
| Line 75: | Line 81: | ||
const link = $(heading).find('a').first(); |
const link = $(heading).find('a').first(); |
||
if (!link.length) { |
if (!link.length) { |
||
mw.notify('Cannot find target page link!', {title: 'VfDcloser', type: 'error', timeout: |
mw.notify('Cannot find target page link!', {title: 'VfDcloser', type: 'error', timeout: 5000}); |
||
return; |
return; |
||
} |
} |
||
const targetPage = link.attr('title') || link.text().trim(); |
const targetPage = link.attr('title') || link.text().trim(); |
||
if (!targetPage) { |
if (!targetPage) { |
||
mw.notify('Cannot determine target page title!', {title: 'VfDcloser', type: 'error', timeout: |
mw.notify('Cannot determine target page title!', {title: 'VfDcloser', type: 'error', timeout: 5000}); |
||
return; |
return; |
||
} |
} |
||
if (actionObj.name === 'delete') { |
if (actionObj.name === 'delete') { |
||
// Delete target page |
|||
api.postWithToken('csrf', {action: 'delete', title: targetPage, reason: '[[' + mw.config.get('wgPageName') + ']]'}) |
api.postWithToken('csrf', {action: 'delete', title: targetPage, reason: '[[' + mw.config.get('wgPageName') + ']]'}) |
||
.done(() => { |
.done(() => { |
||
const talkPage = 'Talk:' + targetPage; |
const talkPage = 'Talk:' + targetPage; |
||
// Check if talk page exists before deletion |
|||
api.get({action: 'query', titles: talkPage, format: 'json'}).done(data => { |
api.get({action: 'query', titles: talkPage, format: 'json'}).done(data => { |
||
const talkPages = data.query.pages; |
const talkPages = data.query.pages; |
||
| Line 96: | Line 104: | ||
api.postWithToken('csrf', {action: 'delete', title: talkPage, reason: 'Parent page deleted via VfD'}) |
api.postWithToken('csrf', {action: 'delete', title: talkPage, reason: 'Parent page deleted via VfD'}) |
||
.done(() => { |
.done(() => { |
||
mw.notify('Discussion closed. Page and talk page deleted.', {title: 'VfDcloser', type: 'success', timeout: |
mw.notify('Discussion closed. Page and talk page deleted.', {title: 'VfDcloser', type: 'success', timeout: 5000}); |
||
location.reload(); |
location.reload(); |
||
}).fail(err => mw.notify('Error deleting talk page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: |
}).fail(err => mw.notify('Error deleting talk page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 5000})); |
||
} else { |
} else { |
||
mw.notify('Discussion closed. Page deleted.', {title: 'VfDcloser', type: 'success', timeout: |
mw.notify('Discussion closed. Page deleted.', {title: 'VfDcloser', type: 'success', timeout: 5000}); |
||
location.reload(); |
location.reload(); |
||
} |
} |
||
}); |
}); |
||
}).fail(err => mw.notify('Error deleting page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: |
}).fail(err => mw.notify('Error deleting page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 5000})); |
||
} else { |
} else { |
||
// Keep / No consensus |
// Keep / No consensus: update target and talk page |
||
api.get({ |
api.get({ |
||
action: 'query', |
action: 'query', |
||
| Line 140: | Line 148: | ||
let talkContent = ''; |
let talkContent = ''; |
||
if (talkId !== '-1' && talkPages[talkId].revisions) { |
if (talkId !== '-1' && talkPages[talkId].revisions) { |
||
talkContent = |
talkContent = talkPages[talkId].revisions[0].slots.main['*']; |
||
talkContent = '{{vfd-kept-new}}\n' + talkContent.trim(); |
|||
} else { |
} else { |
||
talkContent = '{{vfd-kept-new}}'; |
talkContent = '{{vfd-kept-new}}'; |
||
| Line 152: | Line 161: | ||
minor: true |
minor: true |
||
}).done(() => { |
}).done(() => { |
||
mw.notify('Discussion closed. Page and talk page updated.', {title: 'VfDcloser', type: 'success', timeout: |
mw.notify('Discussion closed. Page and talk page updated.', {title: 'VfDcloser', type: 'success', timeout: 5000}); |
||
location.reload(); |
location.reload(); |
||
}).fail(err => mw.notify('Error editing talk page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: |
}).fail(err => mw.notify('Error editing talk page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 5000})); |
||
}); |
}); |
||
}).fail(err => mw.notify('Error editing page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: |
}).fail(err => mw.notify('Error editing page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 5000})); |
||
}); |
}); |
||
} |
} |
||
}).fail(err => mw.notify('Error editing discussion page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: |
}).fail(err => mw.notify('Error editing discussion page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 5000})); |
||
}); |
}); |
||
}); |
}); |
||