// Plany - ZUT Online Parsing Script

var nextTerm = sessionStorage["nextTerm"] || false;
var isPrevious = sessionStorage["isPrevious"] || false;
var nextData = sessionStorage["nextData"] || null;
var goBack = sessionStorage["goBack"] || false;

function loadPrevious() {
    var previousButton = document.getElementById("ctl00_ctl00_ContentPlaceHolder_RightContentPlaceHolder_butP");
    previousButton.checked = true;
    previousButton.click();
}

function parseContent(){
    var location = window.location.href;

    var user = document.getElementsByClassName("who_is_logged_in")[0];

    if (user != null){
        window.webkit.messageHandlers.canStartLoading.postMessage("");

        if (location.includes("PodzGodz")) {
            if (goBack) {
                sessionStorage["goBack"] = false;
                history.back();
            }

            var term = document.getElementById('ctl00_ctl00_ContentPlaceHolder_RightContentPlaceHolder_rbJak_2');
            var termValue = term.getAttribute('checked');

            if (termValue){
                
                var scheduleTable = document.getElementById("ctl00_ctl00_ContentPlaceHolder_RightContentPlaceHolder_dgDane");
                
                if (scheduleTable) {

                    if (nextTerm) {
                        if (window.webkit.messageHandlers.hasOwnProperty("preprintReport")) {
                            window.webkit.messageHandlers.preprintReport.postMessage(document.documentElement.outerHTML);

                        }
                        
                        if (!isPrevious) {
                            if (scheduleTable.getElementsByTagName("tr").length < 30) {
                                sessionStorage["goBack"] = true;
                                sessionStorage["isPrevious"] = false;
                                
                            }
                            
                        }

                        var printButton = document.getElementById('ctl00_ctl00_ContentPlaceHolder_RightContentPlaceHolder_btDrukuj');
                        var click = printButton.getAttribute('onclick');
                        var beginLocationIndex = click.indexOf('\'');
                        var endLocationIndex = click.lastIndexOf('\'');
                        var printUrl = click.substring(beginLocationIndex + 1,endLocationIndex);
                        window.location.href = printUrl;
                        
                    } else {

                        sessionStorage["nextTerm"] = true;
                        var nextButton = document.getElementById("ctl00_ctl00_ContentPlaceHolder_RightContentPlaceHolder_butN");
                        nextButton.checked = true; 
                        nextButton.click();
                    }
                    
                    
                } else {

                    if (nextTerm) {
                        sessionStorage["isPrevious"] = true;
                        loadPrevious();
                        
                    } else {
                        window.webkit.messageHandlers.noData.postMessage("");
                    }
                }
            }else {

                term.checked = true;
                term.onclick();

            }
        }else {
                window.location.href = 'https://edziekanat.zut.edu.pl/WU/PodzGodzin.aspx';
        }
    }else {

        if (location.includes("PodzGodzDruk")){

            var table = document.getElementsByTagName("table")[0];
            var tbody = table.getElementsByTagName("tbody")[0];
            var rows = tbody.getElementsByTagName("tr");
            rows[0].remove();
            var currentElement = {};
            var currentLessons = [];
            var collectedElements = [];
            for(var i = 0; i < rows.length; i++) {
                var currentRow = rows[i];
                var ths = currentRow.getElementsByTagName("th");
                if (ths.length > 0) {
                    if (currentElement.hasOwnProperty("date")) {
                        
                        currentElement["lessons"] = currentLessons;
                        currentLessons = [];
                        collectedElements.push(currentElement);
                        currentElement = {};
                    }
                    var th = ths[0];
                    var keyValues = th.textContent.split(",");
                    keyValues = keyValues.map(function (element) {
                                              return element.trim();
                                              });
                    if (keyValues.length === 2) {
                        currentElement["date"] = keyValues[0];
                        currentElement["dayName"] = keyValues[1];
                    }
                } else {
                    if (currentElement.hasOwnProperty("date")) {
                        var tds = [].slice.call(currentRow.getElementsByTagName("td"));
                        var currentLesson = {};
                        tds.forEach(function (element, index) {
                                    var content = element.textContent;
                                    switch (index) {
                                    case 2:
                                    currentLesson["fromHour"] = content; break;
                                    case 3:
                                    currentLesson["tillHour"] = content; break;
                                    case 4:
                                    currentLesson["subject"] = content; break;
                                    case 5:
                                    currentLesson["teacher"] = content; break;
                                    case 6:
                                    currentLesson["classroom"] = content; break;
                                    case 8:
                                    currentLesson["type"] = content; break;
                                    default:
                                    break;

                                    }
                                    });
                        currentLessons.push(currentLesson);
                        
                    }
                    
                }
            }
            if (nextData) {
                let next = JSON.parse(nextData);
                for( key in next) {
                    collectedElements.push(next[key]);
                }
                
            }
            let data = JSON.stringify(collectedElements);
            
                
            
            if (goBack) {
                sessionStorage["nextData"] = data;
                history.back();
            }
            
            if (window.webkit.messageHandlers.hasOwnProperty("passDataMessageAndReport")) {
                var prepare = {
                    "raw": document.documentElement.outerHTML,
                    "data": data
                };
                var str = JSON.stringify(prepare);
                window.webkit.messageHandlers.passDataMessageAndReport.postMessage(str);
            } else {
                window.webkit.messageHandlers.passDataMessage.postMessage(data);
            }

        }
    }

}

window.onload = function(){
    parseContent();
}

@@ -46,16 +46,27 @@
 tTerm = 
+JSON.parse(
 sessionS
@@ -88,24 +88,25 @@
 m%22%5D %7C%7C false
+)
 ;%0Avar isPrev
@@ -112,16 +112,27 @@
 vious = 
+JSON.parse(
 sessionS
@@ -160,16 +160,17 @@
 %7C%7C false
+)
 ;%0Avar ne
@@ -227,16 +227,27 @@
 oBack = 
+JSON.parse(
 sessionS
@@ -271,17 +271,75 @@
 %7C%7C false
-;
+);%0A%0Afunction browserGoBack() %7B%0A    window.history.back();%0A%7D
 %0A%0Afuncti
@@ -812,24 +812,37 @@
 dzGodz%22)) %7B%0A
+            %0A
             
@@ -909,40 +909,8 @@
 se;%0A
-                history.back();%0A
     
@@ -1822,79 +1822,8 @@
 rue;
-%0A                                sessionStorage%5B%22isPrevious%22%5D = false;%0A
     
@@ -6111,36 +6111,24 @@
             
-            
 %7D);%0A        
@@ -6571,38 +6571,8 @@
    %0A
-                %0A            %0A
     
@@ -6585,32 +6585,32 @@
   if (goBack) %7B%0A
+
                 
@@ -6660,31 +6660,110 @@
         
-history.back();
+sessionStorage%5B%22isPrevious%22%5D = true;%0A                browserGoBack();%0A                return; 
 %0A       

Update online_parser.js