omgtu-auto-schedule

Автоматически загружает последнее открытое расписание на сайте www.omgtu.ru

As of 2016-09-06. See the latest version.

// ==UserScript==
// @name omgtu-auto-schedule
// @description Автоматически загружает последнее открытое расписание на сайте www.omgtu.ru
// @author Савкин Владимир
// @license MIT
// @version 1.9.6.8
// @include http://www.omgtu.ru/students/temp/
// @namespace https://greasyfork.org/users/64313
// ==/UserScript==

(function (window, undefined) {
    var w;
    if (typeof unsafeWindow !== undefined) {
        w = unsafeWindow;
    } else {
        w = window;
    }

    if (w.self != w.top) {
        return;
    }

    if (/http:\/\/www.omgtu.ru\/students\/temp/.test(w.location.href)) {
        if(localStorage.getItem('ScheduleData')) {
            var data = localStorage.ScheduleData;
            //Загрузка предыдущего открытого расписания 
            update_schedule(data);
            console.log(localStorage.ScheduleData);
            set_values();
        }
    }
})(window);

//Сохранение значения поля 'Факультет'
$('#faculty_list').change(function(){
    localStorage.ScheduleData = $('#schedule').serialize();
    console.log("Сохранение");
});

//Сохранение значения поля 'Курс'
$("input[name='filter[course]']").change(function(){
    localStorage.ScheduleData = $('#schedule').serialize();
    console.log("Сохранение");
});

//Сохранение значения поля 'Группа'
$('#group_list').change(function(){
    localStorage.ScheduleData = $('#schedule').serialize();
    console.log("Сохранение");
});

//Загрузка расписания по нажатию на "ФАКУЛЬТЕТЫ И ГРУППЫ"
$('#skey_g').click(function (){
    if(localStorage.getItem('ScheduleData')) {
        var data = localStorage.ScheduleData;
        //Загрузка предыдущего открытого расписания 
        update_schedule(data);
        set_values();
    }
 });

function update_schedule(data) {
    var url = $('#schedule').attr("action");
    $.ajax({
        url: url,
        type: 'post',
        data: data,
        dataType: 'json',
        success: function(data) {
            if (data.success) {
                $('#weekpicker-container').show();
                $('#schedule-list').html($($.trim(data.html)).find("#schedule-list").html());
            }
        },
    }); 
}

function set_values()
{   var url = decodeURIComponent(localStorage.ScheduleData);
    console.log(values);
   var faculty = getParameterByName('filter[faculty]', url),
       course  = getParameterByName('filter[course]', url),
       group   = getParameterByName('filter[group]', url);

   $('#faculty_list').val(faculty);   
   var position = course - 1;
   $('input:radio[name=filter[course]]:nth('+ position +')').attr('checked',true);
   $('#group_list').val(group);
}

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}