/* Copyright (c) 2019 Ali Mousavi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* KhayyamJS changes the UI of the presented course list in the student portal of Khayyam university of Mashhad. It might also work in some other universities that might use the same portal. Author: Ali Mousavi (ali.mousavi@gmail.com) Github: https://github.com/tuxitop/khayyamJS */ // ==UserScript== // @name khayyamJS // @namespace http://alimsvi.ir/ // @description changes the UI of the presented course list in the student portal of Khayyam university of Mashhad. // @include https://pooya.khayyam.ac.ir/educ/stu_portal/PresentedCoursesForm.php // @version 0.6 // @author Ali Mousavi // @require https://code.jquery.com/jquery-1.10.2.js // @require https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js // @grant none // ==/UserScript== // define page class. var Page = function() { /* Main page class */ // Public methods. this.infoObj = { courses: [], fields: [], bldgs: [] }; this.courses = this.infoObj.courses; this.fields = this.infoObj.fields; this.bldgs = this.infoObj.bldgs; this.filters = { field: 'all', gender: 'all', bldg: 'all', weight: 'all', days: [0, 1, 2, 3, 4, 5], hours: [0, 1, 2, 3, 4] }; }; Page.prototype.updateInfo = function(doc) { var mkInfo = function(doc) { /* creates and returns an object with different arrays to use in filters, etc. */ var coursesArray = []; var fieldsArray = []; var fieldIDsArray = []; var bldgArray = []; // loop through each table row. $('table:eq(1) tr', $(doc)).each(function(i, row) { // find each column in the row and add it to courseColArray. var courseColArray = []; var columns = $(this).find('td'); columns.each(function() { courseColArray.push($(this).html()); }); // this if is because the header line doesn't have any "td"s if (courseColArray.length) { var course = new Course(i, courseColArray); coursesArray.push(course); // add fields if it's not added before var field = course.degree + ' ' + course.field; if (fieldsArray.indexOf(field) === -1) { fieldsArray.push(field); } var fieldID = course.fieldID; if (fieldIDsArray.indexOf(fieldID) === -1) { fieldIDsArray.push(fieldID); } // Add bldgs the same way as fields if (bldgArray.indexOf(course.bldg) === -1) { bldgArray.push(course.bldg); } } }); return { courses: coursesArray, fields: fieldsArray, fieldIDs: fieldIDsArray, bldgs: bldgArray }; }; this.infoObj = mkInfo(doc); this.courses = this.infoObj.courses; this.fields = this.infoObj.fields; this.fieldIDs = this.infoObj.fieldIDs; this.bldgs = this.infoObj.bldgs; this.updateFilters(); }; Page.prototype.updateFilters = function() { this.filters = { field: 'all', gender: 'all', bldg: 'all', weight: 'all', days: [0, 1, 2, 3, 4, 5], hours: [0, 1, 2, 3, 4] }; // Loop to create options for field select. var fieldOptions = ''; this.fieldIDs.map(function(current) { fieldOptions += ''; }); // Loop to create options for building select. var bldgOptions = ''; this.bldgs.map(function(current) { bldgOptions += ''; }); $('#field-select') .children() .remove(); $('#field-select').append(''); $('#field-select').append(fieldOptions); $('#bldg-select') .children() .remove(); $('#bldg-select').append(''); $('#bldg-select').append(bldgOptions); }; Page.prototype.updateGroups = function(faculty) { $.ajax({ method: 'GET', url: 'EduGrpSelect.php', // url: './updateGroupsResponse.html' data: { FacCode: faculty, SelName: '"EduGrp"' } }).done(function(data) { var grpReg = /option value="(\d+)">(.+?)همه گروه‌های آموزشی']; var matchArray = []; while ((matchArray = grpReg.exec(data)) != null) { optionsArray.push( '' ); } $('#group-select').html(optionsArray.join('')); }); }; // create a prototype (this way I can have private methods) Page.prototype.mkBody = function() { // makes the page ready and creates the body of the page. $('center') .prevAll('script') .remove(); $('center, style, link').remove(); // add necessary styleseets. $('head').append( '' + '' + '' + '' + '' ); //navbar $('body').append( '' ); $('.navbar-nav').data('size', 'big'); $(window).scroll(function() { if ($(document).scrollTop()) { if ($('.navbar-nav').data('size') === 'big') { $('.navbar-nav li a, .navbar-brand').animate({ padding: '3px 15px 0 15px', height: '30px' }); $('.navbar-nav').data('size', 'small'); } } else if ($('.navbar-nav').data('size') === 'small') { $('.navbar-nav li a, .navbar-brand').animate({ padding: '15px 15px 10px 15px', height: '50px' }); $('.navbar-nav').data('size', 'big'); } }); $('#about-link').click(function(event) { event.preventDefault(); $('#about-container').slideToggle(); $('#about-link').toggleClass('clicked'); }); // the jumbotron $('body').append( '
' + '
' + '

فهرست درس‌های ارایه شده

' + '
' + '
' ); $('body').append( '
' + '
' + '
' + '

انتخاب گروه:

' + '
' + '
دانشکده:
' + '' + '
گروه:
' + '' + '' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '

فیلتر کلاس‌ها:

' + '
' + '
کد رشته:
' + '' + '
جنسیت:
' + '' + '
ساختمان:
' + '' + '
واحد:
' + '' + '
' + '
' + '
روز:
' + '' + '' + '' + '' + '' + '' + '
' + '
' + '
ساعت:
' + '' + '' + '' + '' + '' + '
' + '
' + '
' + '
' ); // Change Faculity Action var that = this; $('#faculty-select').change(function() { var faculty = $(this).val(); that.updateGroups(faculty); }); // Show Courses Button $('#update-courses-btn').click(function() { $.ajax({ method: 'POST', data: { FacCode: $('#faculty-select').val(), EducGrp: $('#group-select').val(), ShowBtn: 'نمایش' } // $.ajax({ // method: 'GET', // url: './engineering.html' }).done(function(data) { that.updateInfo(data); $('.course-list') .children() .remove(); if (that.courses.length <= 150) { that.createCourseTable(); } else { $('.course-list').append( '
' + '

با توجه به تعداد زیاد کلاس‌ها، بهتر است نتایج را از طریق فیلترها محدود کنید.

' + '
برای نمایش کلیه کلاس‌ها روی دکمه‌ی زیر کلیک کنید و توجه کنید که این کار ممکن است زمان‌بر باشد.
' + 'نمایش کلاس‌ها بدون فیلتر' + '
' ); // course table button action: $('#course-table-btn').click(function() { that.createCourseTable(); }); } }); }); // filter actions: $('#field-select').change(function() { that.filters.field = $(this).val(); that.createCourseTable(); }); $('#gender-select').change(function() { that.filters.gender = $(this).val(); that.createCourseTable(); }); $('#bldg-select').change(function() { that.filters.bldg = $(this).val(); that.createCourseTable(); }); $('#weight-select').change(function() { that.filters.weight = $(this).val(); that.createCourseTable(); }); $('input:checkbox[name="hour"]').change(function() { var selected_hours = []; $('input:checkbox[name="hour"]:checked').each(function() { selected_hours.push(parseInt($(this).val())); }); that.filters.hours = selected_hours; that.createCourseTable(); }); $('input:checkbox[name="day"]').change(function() { var selected_days = []; $('input:checkbox[name="day"]:checked').each(function() { selected_days.push(parseInt($(this).val())); }); that.filters.days = selected_days; that.createCourseTable(); }); // footer $('body').append( '
' + '' ); }; Page.prototype.isValid = function(course) { // function to check if course is valid according to the selected filters. var valid = true; // for some weired reason it doesn't work with returning false. // test field if (this.filters.field !== 'all') { // var current_field = course.degree + ' ' + course.field; var current_field = course.fieldID; if (current_field !== this.filters.field) { valid = false; } } // test gender if (this.filters.gender !== 'all') { if (course.getGender()[0] != this.filters.gender) { valid = false; } } // test building if (this.filters.bldg !== 'all') { if (course.bldg != this.filters.bldg) { valid = false; } } // test building if (this.filters.weight !== 'all') { if (course.weight[0] != this.filters.weight) { valid = false; } } // test hours if (this.filters.hours.length < 5) { var sessions = course.getSessionsArray(); var that = this; sessions.forEach(function(current_session) { if (that.filters.hours.indexOf(current_session.hourCode) === -1) { valid = false; } }); } // test days if (this.filters.days.length < 6) { var sessions = course.getSessionsArray(); var that = this; sessions.forEach(function(current_session) { if (this.filters.days.indexOf(current_session.dayCode) === -1) { valid = false; } }); } return valid; }; Page.prototype.createCourseTable = function() { // Removes the old table and creates a new one, applying the filters. $('.course-list') .children() .remove(); var that = this; this.courses.forEach(function(current_course) { if (that.isValid(current_course) === true) { current_course.addToPage(); } }); $('.course-header').click(function() { $(this) .next() .slideToggle(); $(this) .find('.fa-chevron-left') .toggleClass('fa-rotate-270'); }); }; // define a course class. var Course = function(index, courseColArray) { /* the first element is the row index in html class and the second element is the array made by looping in Jquery selector $("tr") and finding $("td") elements. */ this.index = index; this.id = 'course-' + this.index; this.courseID = $(courseColArray[1]).text(); this.stGroupID = courseColArray[2]; this.title = courseColArray[3].replace(/ي/g, 'ی'); this.weight = courseColArray[4]; this.stSigned = courseColArray[5]; this.stCapacity = courseColArray[6]; this.stVacancy = this.stCapacity - this.stSigned; this.stVacancyPercent = (this.stVacancy / this.stCapacity) * 100; this.faculty = courseColArray[7]; // this.teacherID = courseColArray[8]; this.teacherID = '-'; this.teacher = courseColArray[8].replace(/ي/g, 'ی'); this.reserved = courseColArray[9]; // courseColArray 10 consists of specs that should be extracted differently. // the regular expressions to extract different parts of specs. var re_specs = / body=\[(.+)\] /g; // var re_courseID = /(\d+)<\/a>/g; var re_field = /رشته (مهندسی)? *?(.*?)دوره/; var re_degree = /مقطع:<\/b> (.*?)
/g; var re_bldg = /ساختمان ?(\d)\)/g; var re_exam = /امتحان روز:<\/b> ?(.*?) ?ساعت ?(\d+)/g; this.specs = re_specs.exec(courseColArray[10]); this.specs = this.specs ? this.specs[1] : courseColArray[10]; // this.courseID = re_courseID.exec(courseColArray[1])[1]; this.field = re_field.exec(this.specs) ? re_field.exec(this.specs)[2].trim() : this.faculty; this.degree = re_degree.exec(this.specs)[1].trim(); this.examDay = '-'; this.examHour = '-'; var examMatch = []; if ((examMatch = re_exam.exec(this.specs)) !== null) { this.examDay = examMatch[1]; this.examHour = examMatch[2]; } var bldgMatch = re_bldg.exec(this.specs); this.bldg = '-'; if (bldgMatch !== null) { this.bldg = bldgMatch[1]; } this.fieldID = this.courseID.substr(0, 4); this.sessionsArray = this.getSessionsArray(); this.reqsArray = this.getReqsArray(); }; Course.prototype.addToPage = function() { // make the list. $('.course-list').append( '
' + '
' + '
' + '
' ); // Add Course Headings: $('#' + this.id + ' > .course-header').append( '
' + '
' + ' ' + this.index + '
' + '
' + ' ' + this.title + '
' + '
' + ' ' + this.teacher + '
' + '
' + ' ' + this.degree + ' ' + this.field + '
' + '
' + ' ' + this.stSigned + ' از ' + this.stCapacity + '
' + '
' + this.getGender()[1] + '
' + '
' + ' ' + this.bldg + '
' + '
' ); // Add Course Specs $('#' + this.id + ' > .course-specs').append( '
' + '
' + '
' + '
جلسه‌ها:
' + '
' + '
' + '
' + '
ظرفیت:
' + '
' + this.stCapacity + ' نفر (فضای باقی‌مانده: ' + this.stVacancy + ' نفر)' + '
' + '
' + '
' + '
جنسیت:
' + '
' + this.getGender()[2] + '
' + '
ساختمان:
' + '
' + this.bldg + '
' + '
' + '
' + '
امتحان:
' + '
' + this.examDay + ' ساعت ' + this.examHour + '
' + '
وزن:
' + '
' + this.weight[0] + ' واحد
' + '
' + '
' + '
گروه:
' + '
' + this.stGroupID + ' (این کد را برای انتخاب واحد یادداشت کنید.)' + '
' + '
' + '
' + '
کد درس:
' + '
' + this.courseID + '
' + '
کد استاد:
' + '
' + this.teacherID + '
' + '
' + '
' + '
وابستگی‌ها:
' + '
' + '
' + '
' + '
' + this.getTable() + '
' + '
' ); // Change style if courses are getting full. if (this.stVacancyPercent <= 14) { if (this.stVacancy === 0) { $('#' + this.id).addClass('no-vacancy'); } else { $('#' + this.id).addClass('low-vacancy'); } } // change style of session table for each session, and add sessions to specs. for (var i = 0; i < this.sessionsArray.length; i++) { var session = this.sessionsArray[i]; if (session.repeatCode === 1) { $('#' + this.id + ' #table-item-' + session.tableID).addClass( 'session-eow' ); $('#' + this.id + ' #table-item-' + session.tableID).html( session.startWeek[0] ); // add session html $('#' + this.id + ' .course-sessions').append( '
' + '
' + session.day + ' ساعت ' + session.hour + ' (' + session.repeat + ' - هفته‌های ' + session.startWeek + ')' + '
' + '
' ); } else { $('#' + this.id + ' #table-item-' + session.tableID).addClass( 'session-ew' ); // add session html $('#' + this.id + ' .course-sessions').append( '
' + '
' + session.day + ' ساعت ' + session.hour + '
' + '
' ); } } // Add Requirements to the specs. if (!this.reqsArray.length) { $('#' + this.id + ' .course-reqs').text('-'); } else { for (var i = 0; i < this.reqsArray.length; i++) { var req = this.reqsArray[i]; $('#' + this.id + ' .course-reqs').append( '
' + '
' + req.title + ' (' + req.type + ')' + '
' + '
' ); } } }; Course.prototype.getTable = function() { var rowsHTML = ''; var days = ['', 'ش', '۱ش', '۲ش', '۳ش', '۴ش', '۵ش']; var hours = ['', '۸', '۱۰', '۱۲', '۱۴', '۱۶']; // make header rowsHTML += '
'; hours.forEach(function(CurrentHour) { rowsHTML += '
' + CurrentHour + '
'; }); rowsHTML += '
'; // make the rest for (var i = 1; i < days.length; i++) { rowsHTML += '
'; rowsHTML += '
' + days[i] + '
'; for (var j = 1; j < hours.length; j++) { rowsHTML += '
'; } rowsHTML += '
'; } return rowsHTML; }; Course.prototype.getSessionsArray = function() { // returns an array of Session objecs. var sessionsArray = []; var re_session = /جلسه? (.+?)? روز:<\/b> ?(.+?) ?ساعت ?(\d+) ?\( ?(هر ?هفته|هفته در ?میان) به مدت ?(\d+) ?دقیقه.+?شروع (فرد|زوج)
/g; var reMatch; var hoursArray = ['8', '10', '12', '14', '16', '18']; var daysArray = [ 'شنبه', 'یکشنبه', 'دوشنبه', 'سه شنبه', 'چهارشنبه', 'پنجشنبه' ]; while ((reMatch = re_session.exec(this.specs)) !== null) { var Session = {}; Session.day = reMatch[2]; Session.hour = reMatch[3]; Session.repeat = reMatch[4]; Session.duration = reMatch[5]; Session.startWeek = reMatch[6]; Session.hourCode = hoursArray.indexOf(Session.hour); Session.dayCode = daysArray.indexOf(Session.day); Session.repeatCode = ['هر هفته', 'هفته در میان'].indexOf(Session.repeat); Session.tableID = Session.dayCode.toString() + Session.hourCode.toString(); sessionsArray.push(Session); } return sessionsArray; }; Course.prototype.getReqsArray = function() { var reqsArray = []; var re_reqs = /کد درس ?: ?(\d+?) نام درس: (.*?) تعداد واحد: ?(\d) ?\( ?(پیشنیاز|همنیاز)\) ?کد ?معادل ?: ?(-?\d+?)<\/li>/g; var reMatch; while ((reMatch = re_reqs.exec(this.specs)) !== null) { var Req = {}; Req.id = reMatch[1]; Req.title = reMatch[2]; Req.weight = reMatch[3]; Req.type = reMatch[4]; Req.altId = reMatch[5]; reqsArray.push(Req); } return reqsArray; }; Course.prototype.getGender = function() { /* returns array of [genderCode, genderIcon, genderName] code is 0 for male, 1 for female, 2 for both. */ var re_gender = /قابل انتخاب برای دانشجویان:<\/b> ((مرد)?( و )?(زن)?)( | | )/g; var reMatch = re_gender.exec(this.specs); var code, icon; if (reMatch[2]) { if (reMatch[4]) { code = 2; icon = '|'; } else { code = 0; icon = ''; } } else { code = 1; icon = ''; } return [code, icon, reMatch[1]]; }; Course.prototype.bldgHTML = function() { // returns HTML code if there is building or "" if there isn't return ( '
' + ' ' + this.bldg + '
' ); }; $(document).ready(function() { var app = new Page(); // var coursesArray = app.courses; app.mkBody(); app.updateGroups('54'); });