summaryrefslogtreecommitdiff
path: root/crsq1.js
blob: c392c14b0c809670cba484453cb2a8cf2ec1c30e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(function () {
"use strict";

const BAD_LINK_RE = /\/Apps\/WebObjects\/cdm\.woa\/[0-9]+\/wo\/[A-Za-z0-9]+\/[0-9.]+/;
const COURSE_ID_RE = /^([A-Z]{2})[\/ ]+([A-Z]{2,4})\s+([0-9]{4})\s+([0-9.]+)$/;
const LINK_YEAR_RE = /^[A-Za-z\/]+\s+([0-9]+)/

let scheduleLinks = document.evaluate('//a[contains(text(), " Course Schedule")]', document.body);
if (!scheduleLinks)
    return;

let scheduleLink, scheduleLinksa = [];
while ((scheduleLink = scheduleLinks.iterateNext())) {
    scheduleLinksa.push(scheduleLink);
}

scheduleLinksa.forEach(function (scheduleLink) {
    if (BAD_LINK_RE.test(scheduleLink.pathname)) {
        let courseId = COURSE_ID_RE.exec(scheduleLink.parentNode.parentNode.children[0].textContent);
        if (!courseId)
            return;
        let sess;
        if (scheduleLink.textContent.startsWith("Fall/Winter")) {
            sess = "FW";
        } else if (scheduleLink.href.startsWith("Summer")) {
            sess = "SU";
        }
        let rematch = LINK_YEAR_RE.exec(scheduleLink.textContent);
        if (rematch && sess) {
            scheduleLink.pathname = "/Apps/WebObjects/cdm.woa/wa/crsq";
            scheduleLink.search = `fa=${courseId[1]}&sj=${courseId[2]}&cn=${courseId[3]}&cr=${courseId[4]}&ay=${rematch[1]}&ss=${sess}`;
        }
    }
});
}());