E-Mesem Toplu Seçim ve Karne Oluşturma

E-Meseme Toplu Seçim özellikleri ve tüm karneleri oluşturma butonu ekler

// ==UserScript==
// @name         E-Mesem Toplu Seçim ve Karne Oluşturma
// @namespace    http://tampermonkey.net/
// @version      4.14
// @description  E-Meseme Toplu Seçim özellikleri ve tüm karneleri oluşturma butonu ekler
// @author       Fatih D.
// @match        https://e-mesem.meb.gov.tr/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Tanımlamalar
    const buttonTypes = [
        { buttonPrefix: 'rgBasvurular', styleUnchecked: 'imgCheck24n.png', styleChecked: 'imgCheck24e.png', headerText: 'Onay', onayTarihiHeaderText: 'Onay Tarihi', aciklamaHeaderText: 'Açıklama' },
        { buttonPrefix: 'rgSinifList', styleUnchecked: 'imgUyari.png', styleChecked: 'imgUyariSec.png', headerText: 'N. Fişi', onayTarihiHeaderText: 'Öğr.', aciklamaHeaderText: null },
        { buttonPrefix: 'rgSinifKilitList', styleUnchecked: 'imgCheck24n.png', styleChecked: 'imgCheck24r.png', headerText: 'Not Girişine Kilitle', onayTarihiHeaderText: 'Öğr.', aciklamaHeaderText: 'Not İşlemleri' },
        { buttonPrefix: 'rgOgrenciListesi', styleUnchecked: 'imgCheck24n.png', styleChecked: 'imgCheck24e.png', headerText: 'Seç' , onayTarihiHeaderText: 'No Yenile', aciklamaHeaderText: 'Açıklama' },
        { buttonPrefix: 'rgSinifOgrenciler', styleUnchecked: 'imgCheck16n.png', styleChecked: 'imgCheck16e.png', headerText: 'Seç' , onayTarihiHeaderText: 'Dal Adı', aciklamaHeaderText: null },
        { buttonPrefix: 'rgOgrenci', styleUnchecked: 'imgCheck24n.png', styleChecked: 'imgCheck24e.png', headerText: 'Sınava Girecek' , onayTarihiHeaderText: 'Sıra No', aciklamaHeaderText: 'Id' },
    ];

    function addManagementCheckbox(targetCell, buttonPrefix, styleUnchecked, styleChecked) {
        if (targetCell.querySelector(".management-checkbox")) {
            return;
        }

        const managementCheckbox = document.createElement("input");
        managementCheckbox.type = "checkbox";
        managementCheckbox.className = "management-checkbox";
        managementCheckbox.style.marginRight = "5px";

        managementCheckbox.addEventListener("change", function() {
            const styleToCheck = managementCheckbox.checked ? styleChecked : styleUnchecked;
            const buttons = document.querySelectorAll("button[name^='" + buttonPrefix + "']");
            buttons.forEach(button => {
                const style = button.getAttribute('style');
                if ((managementCheckbox.checked && style && style.includes(styleUnchecked)) ||
                    (!managementCheckbox.checked && style && style.includes(styleChecked))) {
                    button.style.backgroundImage = managementCheckbox.checked ? 'url(' + styleChecked + ')' : 'url(' + styleUnchecked + ')';
                    button.click();
                }
            });
        });

        targetCell.appendChild(managementCheckbox);
    }

    function findHeaderContainingText(text) {
        const headers = Array.from(document.querySelectorAll('th')).filter(th => th.innerText.trim() === text.trim());
        return headers.length > 0 ? headers[0] : null;
    }

    function watchForChanges() {
        const intervalId = setInterval(function() {
            buttonTypes.forEach(buttonType => {
                const header = findHeaderContainingText(buttonType.headerText);
                if (header) {
                    const prevHeader = header.previousElementSibling;
                    const nextHeader = header.nextElementSibling;
                    const prevHeaderText = prevHeader.innerText.trim();
                    const nextHeaderText = nextHeader ? nextHeader.innerText.trim() : null;
                    if (prevHeaderText === buttonType.onayTarihiHeaderText && (!buttonType.aciklamaHeaderText || nextHeaderText === buttonType.aciklamaHeaderText)) {
                        addManagementCheckbox(header, buttonType.buttonPrefix, buttonType.styleUnchecked, buttonType.styleChecked);
                    }
                }
            });
        }, 1000);

        window.addEventListener('beforeunload', function() {
            clearInterval(intervalId);
        });
    }

    watchForChanges();

    // Karneleri Oluşturma Fonksiyonu Tanımlamaları
    var startRowIndex = 0;
    var endRowIndex = 200;
    var failedRows = [];
    var isProcessing = false;
    var isObserverActive = true;

    function addKarneButton() {
        var existingButton = document.getElementById('btnKarneOlustur');

        if (existingButton && !document.getElementById('btnTumKarneleriOlustur') && !isProcessing) {
            var newButton = document.createElement('button');
            newButton.innerHTML = 'Tüm Karneleri Oluştur';
            newButton.style.marginLeft = '10px';
            newButton.id = 'btnTumKarneleriOlustur';

            newButton.onclick = function() {
                alert('Tüm Karneler oluşturuluyor...');
                isProcessing = true;
                processRows(startRowIndex);
            };

            existingButton.parentNode.insertBefore(newButton, existingButton.nextSibling);
            console.log('Tüm Karneleri Oluştur butonu eklendi.');
        }
    }

    function processRows(index) {
        console.log('processRows fonksiyonu çalıştı. Şu anki index: ' + index + ', isProcessing: ' + isProcessing);

        if (!isProcessing) {
            console.log('İşlem tamamlandı. Döngü durduruluyor.');
            isObserverActive = false;
            return;
        }

        var currentRowId = 'rgSinifList_ctl00__' + index;
        var currentRow = document.getElementById(currentRowId);

        if (currentRow) {
            currentRow.scrollIntoView({ behavior: 'smooth', block: 'center' });
            currentRow.click();
            console.log('Satır ' + currentRowId + ' tıklandı ve görünürlüğü ayarlandı.');
            waitForKarneOlusturButton(index);
        } else {
            console.log('Satır ' + currentRowId + ' bulunamadı. İşlem tamamlandı.');
            isProcessing = false;
            isObserverActive = false;
        }
    }

    function waitForKarneOlusturButton(rowIndex) {
        var checkInterval = setInterval(function() {
            var karneOlusturButton = document.getElementById('btnKarneOlustur');
            if (karneOlusturButton && !karneOlusturButton.disabled) {
                clearInterval(checkInterval);
                karneOlusturButton.click();
                console.log('Karne Oluştur butonuna tıklandı.');
                waitForPopupClose(rowIndex);
            }
        }, 1000);

        setTimeout(function() {
            console.log('Satır ' + 'rgSinifList_ctl00__' + rowIndex + ' işlemi tamamlanamadı. İşlem tamamlandı popupı gösterilecek.');
            clearInterval(checkInterval);
            failedRows.push('rgSinifList_ctl00__' + rowIndex);
            showFinishMessage();
        }, 30000);
    }

    function waitForPopupClose(rowIndex) {
        var popupCheckInterval = setInterval(function() {
            var popup = document.getElementById('wucPageAlert1_NotificationOk_popup');
            if (popup && popup.style.display !== 'none') {
                clearInterval(popupCheckInterval);
                var closeIcon = document.getElementById('wucPageAlert1_NotificationOk_rnCloseIcon');
                if (closeIcon) {
                    closeIcon.click();
                    console.log('Pop-up kapatıldı.');
                    startRowIndex++;
                    if (startRowIndex <= endRowIndex) {
                        setTimeout(function() {
                            processRows(startRowIndex);
                        }, 2000);
                    } else {
                        isProcessing = false;
                        showFinishMessage();
                    }
                }
            }
        }, 100);
    }

    function showFinishMessage() {
        var finishMessage = document.createElement('div');
        finishMessage.innerHTML = 'Tüm satırlar işlendi. İşlem tamamlandı.';
        finishMessage.style.position = 'fixed';
        finishMessage.style.top = '10px';
        finishMessage.style.right = '10px';
        finishMessage.style.backgroundColor = 'lightgreen';
        finishMessage.style.padding = '10px';
        finishMessage.style.border = '2px solid green';
        finishMessage.style.zIndex = '9999';
        document.body.appendChild(finishMessage);
    }

    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (isObserverActive) {
                addKarneButton();
            }
        });
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    window.addEventListener('load', function() {
        addKarneButton();

        var interval = setInterval(function() {
            if (isObserverActive) {
                addKarneButton();
                console.log('Interval çalışıyor. İşlem tamamlandı: ' + !isProcessing);
                if (!isProcessing) {
                    clearInterval(interval);
                }
            }
        }, 1000);

        setTimeout(function() {
            clearInterval(interval);
        }, 10000);
    });

})();