User:BZPN/Ostrzegarka.js

From Test Wiki
Jump to navigation Jump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
mw.loader.using(['oojs-ui', 'mediawiki.util']).done(function () {
    function addReportButton() {
        // Przejście przez wszystkie linki edycji
        $('.mw-changeslist-links .mw-diff').each(function () {
            var $diffLink = $(this);
            var diffHref = $diffLink.attr('href');
            var diffNumber = diffHref.match(/oldid=(\d+)/)[1]; // Wyciągnięcie numeru edycji

            // Sprawdzenie, czy przycisk już nie istnieje
            if ($diffLink.siblings('.pt-report').length === 0) {
                // Tworzenie przycisku
                var $button = $('<a>')
                    .attr('href', '#')
                    .text('(zgłoś)')
                    .addClass('pt-report')
                    .css({ 'margin-left': '10px', 'cursor': 'pointer', 'color': 'red' });

                // Dodanie przycisku po linku edycji
                $diffLink.after($button);

                // Obsługa kliknięcia w przycisk
                $button.click(function (e) {
                    e.preventDefault();

                    // Wyświetlenie popupu z możliwością wpisania uzasadnienia
                    var reason = prompt('Podaj uzasadnienie zgłoszenia:');
                    if (!reason || reason.trim() === '') {
                        alert('Uzasadnienie jest wymagane.');
                        return;
                    }

                    // Potwierdzenie zgłoszenia
                    var confirmReport = confirm('Czy na pewno chcesz zgłosić tę edycję do ukrycia?');
                    if (!confirmReport) {
                        return;
                    }

                    // API do edycji strony z prośbami
                    var api = new mw.Api();

                    api.postWithToken('csrf', {
                        action: 'edit',
                        title: 'Wikipedia:Prośby do administratorów',
                        section: 'new',
                        sectiontitle: 'Prośba o ukrycie',
                        text: '[[Specjalna:Diff/' + diffNumber + '|Diff ' + diffNumber + ']] - ' + reason + '\nZgłasza: [[User:BZPN|BZPN]] ([[User talk:BZPN|talk]]) 19:57, 4 October 2024 (UTC)',
                        summary: 'Prośba o ukrycie edycji nr ' + diffNumber
                    }).done(function () {
                        alert('Zgłoszenie zostało wysłane.');
                    }).fail(function (error) {
                        console.error('Błąd podczas wysyłania zgłoszenia:', error);
                        alert('Wystąpił błąd podczas wysyłania zgłoszenia.');
                    });
                });
            }
        });
    }

    // Uruchomienie funkcji po załadowaniu strony
    $(document).ready(function () {
        addReportButton();
    });
});