Wikipedysta:AramilFeraxa/ReportSolver.js: Różnice pomiędzy wersjami

Z Wikipedii, wolnej encyklopedii
Usunięta treść Dodana treść
Nie podano opisu zmian
dr
Znacznik: Wycofane
Linia 137: Linia 137:
var editUrl = mw.util.getUrl(pageTitle, { action: 'edit', section: sectionNumber });
var editUrl = mw.util.getUrl(pageTitle, { action: 'edit', section: sectionNumber });
setTimeout(function() {
console.log("Otwieranie okna edycji...");
var editWindow;
var editWindow = window.open(editUrl);
editWindow = window.open(editUrl);
if (editWindow && editWindow.document && editWindow.document.getElementById('wpTextbox1')) {
var interval = setInterval(function () {
if (editWindow && editWindow.document && editWindow.document.getElementById('wpTextbox1')) {
clearInterval(interval);
console.log("Okno edycji otwarte.");
var textarea = editWindow.document.getElementById('wpTextbox1');
var template, signature, statusTemplate, editSummary;
var template, signature, statusTemplate, editSummary;
var classList = $(e.target).attr('class').split(/\s+/);
var classList = $(e.target).attr('class').split(/\s+/);

Wersja z 13:40, 8 maj 2024

// <nowiki>
$(function () {
    var RS = {};
    const RSConfig = window.RSConfig || {
	allowedPages: []
	};
    window.ReportSolver = RS;
    var wgPageName = mw.config.get('wgPageName');
    RS.summary = ' - za pomocą [[Wikipedysta:AramilFeraxa/ReportSolver|ReportSolver]]';

    RS.setup = function () {
        // dla WP:PdA
        if (wgPageName === 'Wikipedia:Prośby_do_administratorów') {
            $('span.mw-editsection-bracket:first-child').each(function () {
                try {
                    var sectionNumber = this.parentElement.childNodes[1].href.match(/action=edit&section=(\d+)/)[1];
                    this.after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-expired" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-mark-expired" data-section=' + sectionNumber + '>przedawnione</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-not-done" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-mark-not-done" data-section=' + sectionNumber + '>niezałatwione</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-rejected" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-mark-rejected" data-section=' + sectionNumber + '>odrzucone</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-done" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-mark-done" data-section=' + sectionNumber + '>Oznacz jako załatwione</a>'));
                } catch (e) {

                }
            });
            // dla WP:PUR
        } else if (wgPageName === 'Wikipedia:Wersje_przejrzane/Przyznawanie_uprawnień') {
            $('span.mw-editsection-bracket:first-child').each(function () {
                try {
                    var sectionNumber = this.parentElement.childNodes[1].href.match(/action=edit&section=(\d+)/)[1];
                    this.after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-rejectedPUR" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolverPUR-mark-rejected" data-section=' + sectionNumber + '>odmowa</a>'));
                    this.after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-grantedPUR" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolverPUR-mark-granted" data-section=' + sectionNumber + '>włączone</a>'));
                } catch (e) {

                }
            });
            // dla Wikipedia:Zgłoś_błąd_w_artykule
        } else if (wgPageName === 'Wikipedia:Zgłoś_błąd_w_artykule') {
            $('span.mw-editsection-bracket:first-child').each(function () {
                try {
                    var sectionNumber = this.parentElement.childNodes[1].href.match(/action=edit&section=(\d+)/)[1];
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-wrongZB" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolverZB-mark-wrong" data-section=' + sectionNumber + '>błędne (!)</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-rejectedZB" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolverZB-mark-rejected" data-section=' + sectionNumber + '>odrzucone (-)</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-doneZB" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolverZB-mark-done" data-section=' + sectionNumber + '>Oznacz jako załatwione (+)</a>'));
                } catch (e) {

                }
            });
            // opcja domyślna
        } else if (wgPageName.startsWith('Wikipedia:Kawiarenka/') || wgPageName === 'Wikiprojekt:Czy_wiesz/Podziękowania' || RSConfig.allowedPages.includes(wgPageName) || [1, 15, 5, 103, 11].includes(mw.config.get('wgNamespaceNumber'))) {
            $('span.mw-editsection-bracket:first-child').each(function () {
                try {
                    var sectionNumber = this.parentElement.childNodes[1].href.match(/action=edit&section=(\d+)/)[1];
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-expired" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-mark-expired" data-section="' + sectionNumber + '">przedawnione</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-not-done" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-mark-not-done" data-section="' + sectionNumber + '">niezałatwione</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-rejected" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-mark-rejected" data-section="' + sectionNumber + '">odrzucone</a>'));
                    $(this).after(' | ');
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-edit-done" data-section="' + sectionNumber + '"> (K)</a>'));
                    $(this).after($('<a href="javascript:void(0)" class="ReportSolver-mark-done" data-section="' + sectionNumber + '">Oznacz jako załatwione</a>'));
                } catch (e) {
                    console.error('Błąd podczas dodawania przycisku: ', e);
                }
            });
        }

        $('a.ReportSolver-mark-done').click(function (e) {
            $(this).text("Przetwarzanie...");
            RS.doEdit(this, '{{załatwione}}', 'Oznaczenie jako załatwione');
        });
        $('a.ReportSolver-mark-rejected').click(function (e) {
            $(this).text("Przetwarzanie...");
            RS.doEdit(this, '{{odrzucone}}', 'Oznaczenie jako odrzucone');
        });
        $('a.ReportSolver-mark-not-done').click(function (e) {
            $(this).text("Przetwarzanie...");
            RS.doEdit(this, '{{odrzucone|niezałatwione}}', 'Oznaczenie jako niezałatwione');
        });
        $('a.ReportSolver-mark-expired').click(function (e) {
            $(this).text("Przetwarzanie...");
            RS.doEdit(this, '{{przedawnione}}', 'Oznaczenie jako przedawnione');
        });
        $('a.ReportSolverPUR-mark-granted').click(function (e) {
            $(this).text("Przetwarzanie...");
            RS.doEdit(this, '{{załatwione|Włączone}}', 'Uprawnienia włączone');
        });
        $('a.ReportSolverPUR-mark-rejected').click(function (e) {
            $(this).text("Przetwarzanie...");
            RS.doEdit(this, '{{odrzucone|Odmowa}}', 'Odmowa');
        });
        $('a.ReportSolverZB-mark-done').click(function (e) {
            $(this).text("Przetwarzanie...");
            RS.doEdit(this, '{{załatwione}}', 'Oznaczenie jako załatwione', '+');
        });
        $('a.ReportSolverZB-mark-rejected').click(function (e) {
            $(this).text("Przetwarzanie...");
            RS.doEdit(this, '{{odrzucone}}', 'Oznaczenie jako odrzucone', '-');
        });
        $('a.ReportSolverZB-mark-wrong').click(function (e) {
            $(this).text("Przetwarzanie...");
            RS.doEdit(this, '{{odrzucone|Błędne}}', 'Oznaczenie jako błędne', '!');
        });
        $('a.ReportSolver-mark-done-edit').click(function (e) {
            $(this).text("Przetwarzanie...");
            RS.editSection(this, '{{załatwione}}', 'Oznaczenie jako załatwione');
        });
        RS.handleEditButtonClick();
    };
    
	RS.handleEditButtonClick = function () {
	    $(document).on('click', '[class^="ReportSolver-edit-"]', function (e) {
	        var sectionNumber = $(this).data('section');
	        var sectionTitle = $('span.mw-headline').eq(sectionNumber - 1).text();
	        var pageTitle = mw.config.get('wgPageName');
	        var editUrl = mw.util.getUrl(pageTitle, { action: 'edit', section: sectionNumber });
	
        	setTimeout(function() {
            	var editWindow = window.open(editUrl);
            	if (editWindow && editWindow.document && editWindow.document.getElementById('wpTextbox1')) {
	                var template, signature, statusTemplate, editSummary;
	                var classList = $(e.target).attr('class').split(/\s+/);
	                for (var i = 0; i < classList.length; i++) {
	                    if (classList[i].startsWith('ReportSolver-edit-')) {
	                        var action = classList[i].substring('ReportSolver-edit-'.length);
	                        console.log("Wybrana akcja:", action);
	                        if (action === 'done') {
	                            template = '{{załatwione}}. ';
	                            editSummary = 'Oznaczenie jako załatwione';
	                        } else if (action === 'rejected') {
	                            template = '{{odrzucone}}. ';
	                            editSummary = 'Oznaczenie jako odrzucone';
	                        } else if (action === 'expired') {
	                            template = '{{przedawnione}}. ';
	                            editSummary = 'Oznaczenie jako przedawnione';
	                        } else if (action === 'not-done') {
	                            template = '{{odrzucone|niezałatwione}}. ';
	                            editSummary = 'Oznaczenie jako niezałatwione';
	                        } else if (action === 'donePUR') {
	                            template = '{{załatwione|włączone}}. ';
	                            editSummary = 'Uprawnienia włączone';
	                        } else if (action === 'rejectedPUR') {
	                            template = '{{odrzucone|odmowa}}. ';
	                            editSummary = 'Odmowa';
	                        } else if (action === 'wrongZB') {
	                            template = '{{odrzucone|błędne}}. ';
	                            editSummary = 'Oznaczenie jako błędne';
	                            statusTemplate = '!';
	                        } else if (action === 'doneZB') {
	                            template = '{{załatwione}}. ';
	                            editSummary = 'Oznaczenie jako załatwione';
	                            statusTemplate = '+';
	                        } else if (action === 'rejectedZB') {
	                            template = '{{odrzucone}}. ';
	                            editSummary = 'Oznaczenie jako odrzucone';
	                            statusTemplate = '-';
	                        }
	                        break;
	                    }
	                }
	                signature = '~~~~';
	                textarea.value = textarea.value + template + signature;
	                if (statusTemplate) {
	                    textarea.value = textarea.value.replace(/\{\{\s*Status\s+zgłoszenia\s*\|\s*[^\|\}]*\s*\}\}/g, '{{Status zgłoszenia|' + statusTemplate + '}}');
	                }
	                var cursorPosition = textarea.value.length - signature.length;
	                textarea.scrollTop = textarea.scrollHeight;
	                textarea.setSelectionRange(cursorPosition, cursorPosition);
	                textarea.focus();
	                if (editSummary) {
	                    editWindow.document.getElementsByName('wpSummary')[0].value = '/* ' + sectionTitle + ' */ ' + editSummary + RS.summary;
	                }
	            }
	        }, 3000);
	    });
	};


	RS.editSection = function (section, comment, editSummary, template) {
	    var sectionNumber = $(section).data('section');
		var sectionTitle = $('span.mw-headline').eq(sectionNumber - 1).text();
		console.log('Nazwa sekcji:', sectionTitle);
	    var pageTitle = mw.config.get('wgPageName');
	    var editUrl = mw.util.getUrl(pageTitle, { action: 'edit', section: sectionNumber });
	
	    if (!template) {
	        console.error('Nie przekazano szablonu.');
	        return;
	    }
	
	    new mw.Api().get({
	        action: 'parse',
	        page: pageTitle,
	        prop: 'wikitext',
	        section: sectionNumber
	    }).done(function (result) {
	        var wikitext = result.parse.wikitext['*'];
	        wikitext += '\n' + template;
	
	        new mw.Api().postWithEditToken({
	            action: 'edit',
	            title: pageTitle,
	            section: sectionNumber,
	            text: wikitext,
	            summary: '/* ' + sectionTitle + ' */ ' + editSummary + RS.summary,
	            minor: true,
	            nocreate: true
	        }).done(function (result) {
	            if (result && result.edit && result.edit.result && result.edit.result === 'Success') {
	                location.reload();
	            }
	        });
	    });
	};
	
	RS.doEdit = function (section, comment, editSummary, status) {
	    var sectionNumber = $(section).data('section');
		var sectionTitle = $('span.mw-headline').eq(sectionNumber - 1).text();
		console.log('Nazwa sekcji:', sectionTitle);
	    var pageTitle = mw.config.get('wgPageName');
	    new mw.Api().get({
	        action: 'parse',
	        page: pageTitle,
	        prop: 'wikitext',
	        section: sectionNumber
	    }).done(function (result) {
	        var wikitext = result.parse.wikitext['*'];
	        wikitext = wikitext.replace(/\{\{\s*Status\s+zgłoszenia\s*\|\s*[^\|\}]*\s*\}\}/g, '{{Status zgłoszenia|' + status + '}}');
	        wikitext = wikitext + '\n:' + comment + '. ~~~~';
	        new mw.Api().postWithEditToken({
	            action: 'edit',
	            title: pageTitle,
	            section: sectionNumber,
	            text: wikitext,
	            summary: '/* ' + sectionTitle + ' */ ' + editSummary + RS.summary,
	            minor: true,
	            nocreate: true
	        }).done(function (result) {
	            if (result && result.edit && result.edit.result && result.edit.result === 'Success') {
	                location.reload();
	            }
	        });
	    });
	};
});
mw.loader.using('mediawiki.api', function () {
    $(document).ready(function () {
        window.ReportSolver.setup();
    });
});
// </nowiki>