Skip to content

Instantly share code, notes, and snippets.

@nicholastay
Last active March 21, 2018 06:46
Show Gist options
  • Save nicholastay/254f63b90e5a82305701836020af7d25 to your computer and use it in GitHub Desktop.
Save nicholastay/254f63b90e5a82305701836020af7d25 to your computer and use it in GitHub Desktop.
Schoology "Zero Course" userscript - intended for Camberwell Grammar School students (press raw to install)
// ==UserScript==
// @name CGS Schoology ZeroCourse tweak
// @namespace http://nicholastay.github.io/
// @homepage https://gist.github.com/nicholastay/254f63b90e5a82305701836020af7d25
// @version 0.1.6
// @description Reduces Courses tab clutter for CGS students with common YYXXX0 courses on Schoology.
// @author Nicholas Tay <nexerq@gmail.com>
// @license Zlib
// @icon https://i.imgur.com/PkIuFKy.png
// @match *://app.schoology.com/*
// @match *://schoology.cgs.vic.edu.au/*
// @grant none
// @downloadURL https://gist.github.com/nicholastay/254f63b90e5a82305701836020af7d25/raw/zeroth.user.js
// @noframes
// ==/UserScript==
;(function() {
var coursesLoaded = false;
var $coursesElem = $("#primary-courses");
var $courseClick = $(".clickable", $coursesElem);
var $newButton;
function testForCGS() {
var $schoolElem = $("#primary-settings .school a");
return $schoolElem.text() === "Camberwell Grammar School";
}
var TEMPlate = `
<li id="nexerq-zerocourse" class="primary-activities schoology-processed">
<a class="clickable" href="#">
CGS Zero Courses
<span class="dropdown"></span>
</a>
<div class="activities-dropdown-wrapper" id="nexerq-zerocourse-dropdown" style="display: none;">
<div class="s-dropdown nexerq-dropdown">
<img class="ajax-loader" src="/sites/all/themes/schoology_theme/images/ajax-loader.gif" alt="Loading">
<ul class="nexerq-zerocourse-listing listing">
<div class="sections-list"></div>
<li class="no-active-zerocourse">
<div class="no-courses" style="display: none;">You have no ZeroCourses™ - why are you using this plugin?</div>
</li>
</ul>
<div class="courses-action no-active-courses-item" style="display: none;">
<span class="see-all">
<a href="https://github.com/nicholastay" target="_blank">
This plugin was developed by @nicholastay ~ &lt;3
</a>
</span>
</div>
</div>
</div>
</li>
`;
function injectButton() {
$coursesElem.after(TEMPlate);
$newButton = $("#nexerq-zerocourse");
var $clickable = $(".clickable", $newButton);
var $dropdown = $("#nexerq-zerocourse-dropdown");
$clickable.click(function(e) {
e.preventDefault();
var _this = this;
var toggle = function() {
// directly taken from schoology source.
// closes other open dropdowns.
var clickableDropdown = $(_this).siblings('.activities-dropdown-wrapper');
var primaryActivitiesListItem = $(_this).parent('.primary-activities');
if(clickableDropdown.is(':visible')) {
clickableDropdown.hide();
$(_this).removeClass('active');
primaryActivitiesListItem.removeClass('active');
return;
}
$('.activities-dropdown-wrapper').hide();
$('#nav_left .primary-activities .clickable.active, #primary-library #my-apps a.active, #nav_left .primary-activities.active').removeClass('active');
// end block
$dropdown.toggle();
$newButton.addClass("active");
$clickable.addClass("active");
};
if (!coursesLoaded) {
console.log("[nexerq/Zeroth] Zero course info not loaded. Doing that now via course click.");
coursesLoaded = true;
forceLoadCourses();
waitForCourses(loadZeroCourses);
toggle();
} else {
toggle();
}
});
// when click body, close like the others
// based off schoology source
$('body').click(function(e) {
var $target = $(e.target);
var notReorderPopup = !$target.hasClass('dropdown-reorder') && $target.parents('.dropdown-reorder:first').length == 0; // not 100% on this but yeah
if (notReorderPopup && !$target.hasClass('activities-dropdown-wrapper') && $target.parents('.activities-dropdown-wrapper').length == 0 && !$target.hasClass('clickable') && !$target.hasClass('dropdown')) {
$clickable.removeClass("active");
$newButton.removeClass("active");
$dropdown.hide();
}
});
}
function prepToLoad() {
// attach loader wait on new button AND course button
$courseClick.one("click", function() {
if (!coursesLoaded) {
console.log("[nexerq/Zeroth] Course click detected, no zero course info loaded yet. Doing that now.");
coursesLoaded = true;
waitForCourses(loadZeroCourses);
}
});
}
function forceLoadCourses() {
// sorta hacky but whatever - uses clicks to simulate so angular loads the data
// probably find the real function to load the ajax data in future?
$courseClick.click();
$courseClick.click();
}
function waitForCourses(cb) {
// wait for courses to load first, then return cb
console.log("[nexerq/Zeroth] Waiting for courses to load via ajax...");
var courseInterval = setInterval(function() {
if ($("#primary-courses .sections-list li").length > 0) {
console.log("[nexerq/Zeroth] Courses seem to have loaded.");
clearInterval(courseInterval);
cb();
}
}, 250);
}
var zeroCourseRegex = /0 - /;
function loadZeroCourses() {
var zeroCourses = $("#primary-courses .sections-list li").filter(function() {
return zeroCourseRegex.test(this.innerText);
});
$(".ajax-loader", $newButton).hide();
$(".courses-action", $newButton).show();
if (!zeroCourses || zeroCourses.length < 1) {
$(".no-active-zerocourse", $newButton).show();
return console.log("[nexerq/Zeroth] No zero courses found.");
}
zeroCourses.clone().appendTo($(".sections-list", $newButton));
zeroCourses.remove();
$(".no-active-zerocourse", $newButton).hide();
console.log("[nexerq/Zeroth] Zero courses isolated.");
}
if (!testForCGS())
return console.log("[nexerq/Zeroth] Your Schoology edition does not seem to be a Camberwell Grammar edition. Are you sure you should be using this plugin?");
injectButton();
prepToLoad();
console.log("[nexerq/Zeroth] CGS Zeroth v1 loaded.");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment