Skip to content

Instantly share code, notes, and snippets.

@JanPokorny
Created February 26, 2018 14:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JanPokorny/b06122018c50032ccf203017029407b8 to your computer and use it in GitHub Desktop.
Save JanPokorny/b06122018c50032ccf203017029407b8 to your computer and use it in GitHub Desktop.
IS MU Fix disabled assignment button
// ==UserScript==
// @name IS course assignment
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Fix disabled button for course assignment
// @author Jan Pokorný
// @match https://is.muni.cz/auth/student/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @connect muni.cz
// ==/UserScript==
(function() {
'use strict';
function GM_fetch(url) {
return new Promise((resolve, reject) => GM_xmlhttpRequest({
url: url,
method: "GET",
onload: resolve,
onerror: reject
}));
}
const urlFinders = [
async function cached(courseURL) {
return GM_getValue("courseUrl:" + courseURL, null);
},
async function courseDescription(courseURL) {
const {responseText} = await GM_fetch(courseURL.replace("/el/", "/predmet/") + "?lang=en");
const descRegex = /<DT><B>Teacher's information<\/B><\/DT>\s*<DD>(.*?)<\/DD>/;
const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/;
try {
return responseText.match(descRegex)[1].match(urlRegex)[0];
} catch {
return null;
}
},
async function materials(courseURL) {
return courseURL + "um/";
}
];
async function fixButton(button, courseURL) {
for(const urlFinder of urlFinders) {
const url = await urlFinder(courseURL);
if(url !== null) {
button.classList.remove("disabled");
button.href = url;
button.childNodes[2].nodeValue = "Materiály";
GM_setValue("courseUrl:" + courseURL, url);
break;
}
}
}
function domChange(mutationsList) {
for(const mutation of mutationsList) {
const button = mutation.target.querySelector(".student_row_c > .column:first-child > .disabled.button.large");
const courseURL = mutation.target.querySelector('.student_row_b a[href^="/auth/el"]').href;
if(button != null) fixButton(button, courseURL);
}
}
const observer = new MutationObserver(domChange);
const courseBlocks = document.querySelectorAll(".predmet_conteiner"); // yep, "conteiner"
for(const courseBlock of courseBlocks) {
observer.observe(courseBlock.children[1], { childList: true });
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment