Lee1997 / Moodle Fixer

// ==UserScript==
// @name         Moodle Fixer
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Fixes stupid moodle
// @author       You
// @match        https://2017.moodle.maynoothuniversity.ie/my/
// @match        https://2017.moodle.maynoothuniversity.ie/course/*
// @grant        none
// ==/UserScript==

addCourseTitle = function(){
	//var inStr = "CS265[A] - Software Testing (2017: S1)";
    //var inStr = "MT212A[A] - Linear Algebra 2 (A) (2017:S2)";

    //Selects the courses
    var courses = document.getElementsByClassName("course_title");
    for(var j = 1; j < courses.length; j++){
        //Selects the link title
        var anchor = courses[j].getElementsByTagName('a');
        var inStr = anchor.item(1).title;
        var fullCourse = false;
        var fullName = "";

        for (var i = 0; i < inStr.length - 3; i++) {
            if (inStr.substring(i, i + 3) == "[A]") {

                //Adds in the Module Code
                fullName += inStr.substring(0, 5);
                fullName += " - ";

                //Adds in the Module Name
                fullName += inStr.substring(i + 6, inStr.length - 10);
                fullName += " - ";

                //Adds in the Semester the module is covered
                fullName += inStr.substring(inStr.length - 3, inStr.length - 1);

                //Enables you to replace the code
                fullCourse = true;

                break;
            }
        }
        //Changes the code if it's a proper module
        if(fullCourse){
            anchor[1].innerHTML = fullName;
        }
    }
};
showExamPapers = function(){
    var course = document.getElementsByClassName("courselink");
    var str = (course[0].innerHTML);
    var foo = 500;

    for(var i = 0; i < str.length; i++)
    {
        if(str.charAt(i) == '[')
        {
            foo = i;
        }
    }

    if(foo != 500)
    {
        str = str.substring(0, foo);

        var examLink = "https://www.maynoothuniversity.ie/library/exam-papers?code_value_1=";
        examLink += str;

        var navID = document.getElementById("inst10");
        var cont = navID.getElementsByClassName("content");
        var list = cont[0].getElementsByTagName('ul');
        var link = document.createElement('a');
        var image = document.createElement("img");

        image.className = "smallicon navicon";
        image.setAttribute("src", "https://2017.moodle.maynoothuniversity.ie/theme/image.php/nuim/core/1480954821/i/navigationitem");

        link.appendChild(image);
        link.appendChild(document.createTextNode(" Exam Papers"));
        link.title = "Exam Papers";
        link.href = examLink;
        console.log(examLink);

        link.className = "type_setting depth_2 contains_branch";
        list[1].appendChild(link);
    }

};
main = function(){
    'use strict';
	var pageTitle = window.location.href;
	if(pageTitle == "https://2017.moodle.maynoothuniversity.ie/my/"){
		addCourseTitle();
	}
    else if(pageTitle.substring(0, 61) == "https://2017.moodle.maynoothuniversity.ie/course/view.php?id="){
        showExamPapers();
    }
}();