User:π–π’π€π’ππšπ²πžπ«/test.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
mw.loader.using(['mediawiki.api', 'oojs-ui'], function () {
const api = new mw.Api();
const gapi = mw.config.get('wgDBname') === 'metawiki' ? new mw.Api() : new mw.ForeignApi('https://meta.wikimedia.org/w/api.php');

const messages = {
en: {
blockUser: 'Block user',
noReasonsFound: 'No block reasons found.',
selectReason: 'Select reason',
editParameters: 'Edit block parameters',
confirmationText: 'You selected the reason "{reason}" with parameters "{params}". Do you really want to block the user?',
confirmBlock: 'Confirm block',
cancel: 'Cancel',
success: 'User successfully blocked.',
error: 'Error blocking the user.',
feedback: 'Operation feedback: {feedback}',
expiry: 'Expiry',
preventaccountcreation: 'Prevent account creation',
autoblock: 'Autoblock',
email: 'no email',
owntalkpage: 'Allow user to edit own talk page'
},
de: {
blockUser: 'Benutzer sperren',
noReasonsFound: 'Keine SperrgrΓΌnde gefunden.',
selectReason: 'Grund auswΓ€hlen',
editParameters: 'Sperrparameter bearbeiten',
confirmationText: 'Du hast den Grund "{reason}" mit Parametern "{params}" ausgewΓ€hlt. MΓΆchtest du den Benutzer wirklich sperren?',
confirmBlock: 'Sperre bestΓ€tigen',
cancel: 'Abbrechen',
success: 'Benutzer erfolgreich gesperrt.',
error: 'Fehler beim Sperren des Benutzers.',
feedback: 'RΓΌckmeldung zur Aktion: {feedback}',
expiry: 'Sperrdauer',
preventaccountcreation: 'Kontoerstellung verhindern',
autoblock: 'Autoblock',
email: 'Versenden von E-mails verhindern',
owntalkpage: 'Erlaube eigene Diskussion zu bearbeiten'
}
};

function msg(key, params = {}) {
const lang = mw.config.get('wgUserLanguage');
const text = messages[lang]?.[key] || messages['en'][key];
return text.replace(/\{(\w+)\}/g, (_, p1) => params[p1] || '');
}

async function loadBlockReasons() {
try {
const userPage = `User:${mw.config.get('wgUserName')}/block.json`;
const reasons = await api.get({
action: 'query',
titles: userPage,
prop: 'revisions',
rvprop: 'content',
formatversion: 2
});

const page = reasons.query.pages[0];
if (page.missing) {
throw new Error('User-specific file not found');
}

return JSON.parse(page.revisions[0].content);
} catch (e) {
const fallbackPage = 'User:WikiBayer/blockreasonsglobal.json';
const reasons = await gapi.get({
action: 'query',
titles: fallbackPage,
prop: 'revisions',
rvprop: 'content',
formatversion: 2
});

const page = reasons.query.pages[0];
if (page.missing) {
mw.notify(msg('noReasonsFound'), { type: 'error' });
return null;
}

return JSON.parse(page.revisions[0].content);
}
}

function applyButtonStyles(button) {
button.$element.css({
'transform': 'scale(0.7)',
'transform-origin': 'left'
});
}

function createBlockButton() {
const heading = document.getElementById('firstHeading');
if (!heading) return;

const blockButton = new OO.ui.ButtonWidget({
label: msg('blockUser'),
classes: ['mw-ui-button'],
flags: ['destructive'],
framed: true
});
applyButtonStyles(blockButton);

heading.appendChild(blockButton.$element[0]);

blockButton.on('click', async () => {
blockButton.$element.remove();
const reasons = await loadBlockReasons();
if (!reasons) return;

const menuItems = reasons.templates.map((template) => {
return new OO.ui.MenuOptionWidget({
data: template,
label: template.labeltext || template.label
});
});

const dropdownMenu = new OO.ui.MenuSelectWidget({
items: menuItems
});

dropdownMenu.on('select', (item) => {
if (item) {
const template = item.getData();
showParameterForm(template);
}
});

const dropdown = new OO.ui.DropdownWidget({
label: msg('selectReason'),
menu: dropdownMenu
});
heading.appendChild(dropdown.$element[0]);
dropdown.menu.toggle(true);
});
}

function showParameterForm(template) {
const heading = document.getElementById('firstHeading');
if (!heading) return;

const existingForm = document.getElementById('block-parameter-form');
if (existingForm) existingForm.remove();

const formContainer = document.createElement('div');
formContainer.id = 'block-parameter-form';
formContainer.style.marginTop = '10px';

const reasonInput = new OO.ui.TextInputWidget({
value: template.labeltext || template.label,
label: 'Reason'
});

const expiryInput = new OO.ui.TextInputWidget({
value: template.parameters.expiry,
label: msg('expiry')
});

const nocreateCheckbox = new OO.ui.CheckboxInputWidget({
selected: template.parameters.nocreate
});

const autoblockCheckbox = new OO.ui.CheckboxInputWidget({
selected: template.parameters.autoblock
});

const allowUserTalkCheckbox = new OO.ui.CheckboxInputWidget({
selected: template.parameters.allowusertalk
});

const formFields = new OO.ui.FieldsetLayout({
items: [
new OO.ui.FieldLayout(reasonInput, { label: msg('editParameters') + ' - ' + msg('selectReason') }),
new OO.ui.FieldLayout(expiryInput, { label: msg('expiry')}),
new OO.ui.FieldLayout(nocreateCheckbox, { label: msg('preventaccountcreation') }),
new OO.ui.FieldLayout(autoblockCheckbox, { label: msg('autoblock') }),
new OO.ui.FieldLayout(allowUserTalkCheckbox, { label:  msg('owntalkpage') })
]
});

const confirmButton = new OO.ui.ButtonWidget({
label: msg('confirmBlock'),
flags: ['primary', 'destructive']
});
applyButtonStyles(confirmButton);

const cancelButton = new OO.ui.ButtonWidget({
label: msg('cancel'),
flags: ['safe']
});
applyButtonStyles(cancelButton);

formContainer.appendChild(formFields.$element[0]);
formContainer.appendChild(cancelButton.$element[0]);
formContainer.appendChild(confirmButton.$element[0]);

heading.appendChild(formContainer);

confirmButton.on('click', async () => {
try {
	
const feedback = await api.postWithToken('csrf', {
action: 'block',
user: mw.config.get('wgRelevantUserName'),
reason: reasonInput.getValue(),
expiry: expiryInput.getValue(),
nocreate: nocreateCheckbox.isSelected(),
autoblock: autoblockCheckbox.isSelected(),
allowusertalk: allowUserTalkCheckbox.isSelected()
});
mw.notify(msg('success') + ' ' + msg('feedback', { feedback: JSON.stringify(feedback) }), { type: 'success' });
location.reload();
} catch (error) {
mw.notify(msg('error') + ' ' + msg('feedback', { feedback: error.message }), { type: 'error' });
}
});

cancelButton.on('click', () => {
formContainer.remove();
});
}

mw.hook('wikipage.content').add(createBlockButton);
});