Jump to content

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

From Wikipedia

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(['oojs-ui', 'mediawiki.api', 'mediawiki.ForeignApi']).then(function () {

    if (!mw.config.get('wgNamespaceNumber') || ![2, 3].includes(mw.config.get('wgNamespaceNumber'))) {
        return;
    }

    const localApi = new mw.Api();
	const foreignApi = new mw.ForeignApi('https://test2.wikipedia.org/w/api.php');

    function fetchTemplates() {
        return foreignApi.get({
            action: 'query',
            format: 'json',
            prop: 'revisions',
            titles: 'User:π–π’π€π’ππšπ²πžπ«/test2.json',
            rvprop: 'content',
            formatversion: 2
        }).then(function (data) {
            const page = data.query.pages[0];
            if (page && page.revisions && page.revisions.length > 0) {
                return JSON.parse(page.revisions[0].content);
            }
            return [];
        });
    }

	function blockUser(template, params) {
	    const userName = mw.config.get('wgTitle');
	    localApi.postWithEditToken({
	        action: 'block',
	        user: userName,
	        expiry: params.expiry,
	        reason: params.reason,
	        anononly: params.anononly || false,
	        nocreate: params.nocreate || false,
	        autoblock: params.autoblock || false,
	        allowusertalk: params.allowusertalk || false,
	        reblock: params.reblockCheckbox || false,
	    }).then(function () {
	        mw.notify(`Benutzer ${userName} wurde gesperrt: ${params.reason}`);
	    }).catch(function (error) {
	        console.error('Fehler beim Sperren:', error);
	        mw.notify('Fehler beim Sperren des Benutzers', { type: 'error' });
	    });
	}


    function createBlockMenu(templates) {
        const dropdown = new OO.ui.DropdownWidget({
            label: 'Benutzer sperren',
            menu: { items: [] }
        });

        templates.forEach(template => {
            const menuItem = new OO.ui.MenuOptionWidget({
                data: template,
                label: template.label
            });

            dropdown.getMenu().addItems([menuItem]);
        });

        dropdown.getMenu().on('select', function (selectedItem) {
            if (selectedItem) {
                const template = selectedItem.getData();

const popup = new OO.ui.PopupWidget({
    $content: $(
        `<div style="font-size: 12px;">
            <label>Sperrdauer:</label><br>
            <select id="expiryDropdown" style="width: 100%; margin-bottom: 8px;">
                <option value="3 hour">1 Stunde</option>
                <option value="24 hours">24 Stunden</option>
                <option value="7 Days">1 Woche</option>
                <option value="30 days">1 Monat</option>
                <option value="Infinity">Unbefristet</option>
                <option value="benutzerdefiniert">Benutzerdefiniert</option>
            </select>
            <input type="text" id="expiryInput" placeholder="Benutzerdefinierte Dauer (z. B. 2 Wochen)" style="width: 100%;"><br>
            <label>Sperrgrund:</label><br>
            <select id="reasonSelect" style="width: 100%; margin-bottom: 8px;">
                ${template.reasons.map(reason => `<option value="${reason}">${reason}</option>`).join('')}
            </select>
            <label><input type="checkbox" id="anononlyCheckbox" ${template.anononly ? 'checked' : ''}> Nur anonyme Benutzer sperren</label><br>
            <label><input type="checkbox" id="nocreateCheckbox" ${template.nocreate ? 'checked' : ''}> Kontenerstellung verhindern</label><br>
            <label><input type="checkbox" id="autoblockCheckbox" ${template.autoblock ? 'checked' : ''}> Automatische Sperre von IPs</label><br>
            <label><input type="checkbox" id="allowusertalkCheckbox" ${template.allowusertalk ? 'checked' : ''}> Bearbeitung der Diskussionsseite erlauben</label><br>
            <label><input type="checkbox" id="reblockCheckbox"> Bestehende Sperren ΓΌberschreiben</label><br>
            <button id="blockUserButton" style="margin-top: 10px;">Sperren</button>
        </div>`
    ),
    padded: true,
    width: 300,
    height: 'auto',
    head: true,
    tail: false
});

                popup.toggle(true);
                popup.$element.css({ position: 'absolute', zIndex: 5, top: '100px', left: '50%' });
                $(document.body).append(popup.$element);


		$('#blockUserButton').on('click', function () {
		    const expiryValue = $('#expiryDropdown').val() === 'benutzerdefiniert'
		        ? $('#expiryInput').val()
		        : $('#expiryDropdown').val();
		
		    if ($('#expiryDropdown').val() === 'benutzerdefiniert' && !expiryValue) {
		        mw.notify('Bitte eine benutzerdefinierte Sperrdauer eingeben.', { type: 'error' });
		        return;
		    }
		
		    const params = {
		        expiry: expiryValue,
		        reason: $('#reasonSelect').val(),
		        anononly: $('#anononlyCheckbox').is(':checked'),
		        nocreate: $('#nocreateCheckbox').is(':checked'),
		        autoblock: $('#autoblockCheckbox').is(':checked'),
		        allowusertalk: $('#allowusertalkCheckbox').is(':checked'),
		        reblockCheckbox: $('#reblockCheckbox').is(':checked')
		    };
		    blockUser(template, params);
		    popup.toggle(false);
		});

            }
        });

        const heading = document.querySelector('#firstHeading .mw-page-title-main');
        if (heading) {
            dropdown.$element.css('position', 'absolute').css('top', '0').css('right', '0').css('max-width', '200px');
            $(heading).append(dropdown.$element);
        }
    }

    fetchTemplates().then(createBlockMenu).catch(function (error) {
        console.error('Fehler beim Laden der Sperrvorlagen:', error);
        mw.notify('Fehler beim Laden der Sperrvorlagen', { type: 'error' });
    });
});