User:Euphoria/common.js: Difference between revisions
From Test Wiki
Content deleted Content added
async/await structure |
fix |
||
Line 5:
if (!mw.config.get('wgPageName').startsWith(pagePrefix) || mw.config.get('wgAction') !== 'view') return;
$(
const api = new mw.Api();
Line 16:
const actions = [
{name: 'delete', color: '#e74c3c'},
{name: 'keep', color: '#27ae60'},
{name: 'no consensus', color: '#f1c40f'}
];
actions.forEach(function(actionObj) {
const btn = document.createElement('button');
btn.textContent = actionObj.name.charAt(0).toUpperCase();
btn.title = actionObj.name;
Object.assign(btn.style
transition: '0.15s'
});
btn.addEventListener('mouseenter', () => btn.style.filter='brightness(1.3)');
btn.addEventListener('mouseleave', () => btn.style.filter='brightness(1)');
// Async click handler
btn.addEventListener('click', async function(e) {
e.preventDefault();
Line 46 ⟶ 50:
try {
//
const data = await api.get({
action: 'query',
Line 59 ⟶ 63:
let content = pages[pageId].revisions[0]['*'];
//
const topText = '{{subst:vt|' + actionObj.name + '. --~~~~}}\n';
const bottomText = '\n{{subst:vb}}';
const discussionNewContent = topText + content.trim() + bottomText;
await api.postWithToken('csrf', {
action: 'edit',
Line 74 ⟶ 77:
});
// 3️⃣ Extract target article from heading
const match = content.match(/==\s*\[\[([^\]]+)\]\]\s*==/);
if (!match) {
alert('Cannot find target article in heading!');
return;
}
Line 84 ⟶ 86:
if (actionObj.name === 'delete') {
// 4️⃣ Delete target article
await api.postWithToken('csrf', {
action: 'delete',
Line 91 ⟶ 93:
bot: true
});
// Delete talk page
const talkPage = 'Talk:' + targetPage;
Line 103 ⟶ 106:
location.reload();
} else {
// 5️⃣ Keep / No consensus: edit article
const articleData = await api.get({
action: 'query',
Line 126 ⟶ 129:
});
// 6️⃣ Update talk page
const talkData = await api.get({
action: 'query',
Line 158 ⟶ 161:
}
} catch
console.error(err);
alert('An error occurred: ' + err);
| |||