Created
October 12, 2012 05:14
-
-
Save ken47/3877453 to your computer and use it in GitHub Desktop.
copy and paste this into dev console while on the course listings page for performance gains
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #1 I do not have access to the 'added' timestamp (for the purposes of sorting), so I just used the first class date as a substitute. | |
// #2 I disabled the image loading (inView()) functionality, because using $(element).hide() caused significant lag when scrolling. | |
// This issue could probably be corrected by adding an additional filter on the $('img') search triggered by inView() | |
jQuery.ajax({ | |
url: 'https://www.coursera.org/maestro/api/topic/list?full=1', | |
success: function(catalog) { | |
var compressed = [], | |
course_names = [], | |
instructors = []; | |
// comment out the following block and uncomment the block in showMatches() | |
// to re-enable dynamic loading of images | |
jQuery(window).off('scroll'); | |
jQuery('img').each(function(index,el) { | |
if (jQuery(el).attr('data-src')) { | |
el.src = jQuery(el).attr('data-src'); | |
jQuery(el).removeAttr('data-src'); | |
} | |
}); | |
jQuery('.coursera-course-listing-box').each(function(i1,el) { | |
course_names.push(jQuery(el).find('.coursera-course-listing-name')); | |
instructors.push(jQuery(el).find('.coursera-course-listing-instructor')); | |
}); | |
var bindIdsToDom = function() { | |
for(var i1 in catalog) { | |
if (catalog[i1].display != true) | |
continue; | |
for(var i2 in course_names) { | |
if (jQuery(course_names[i2]).text() === catalog[i1].name && | |
jQuery(instructors[i2]).text() === catalog[i1].instructor) { | |
jQuery(course_names[i2]).closest('.coursera-course-listing-box').attr('data-id',catalog[i1].id); | |
break; | |
} | |
} | |
} | |
var startingKey = -1; | |
jQuery('.coursera-course-listing-box:not([data-id])').each(function(index,el) { | |
var $el = jQuery(el), | |
timeMeta, | |
tmp = { | |
id: startingKey, | |
text: '', | |
name: $el.find('.coursera-course-listing-name').text(), | |
universities: [] | |
}; | |
$el.attr('data-id',startingKey--); | |
timeMeta = $el.find('.coursera-course-listing-meta').find('span'); | |
tmp.start = Date.parse(timeMeta[0]); | |
tmp.added = Date.parse(timeMeta[0]); | |
if (typeof timeMeta[1] != 'undefined') | |
tmp.duration = parseInt(timeMeta[1]); | |
else | |
tmp.duration = null; | |
$el.find('.coursera-course-listing-university div').each(function(index,el2) { | |
tmp.universities.push(jQuery(el2).text()); | |
}); | |
tmp.text += " " + tmp.name; | |
tmp.text += " " + $el.find('.coursera-course-listing-instructor').text(); | |
compressed.push(tmp); | |
}); | |
}; | |
var compressData = function() { | |
for(var i1 in catalog) { | |
if (catalog[i1].display != true) | |
continue; | |
var start_date, | |
tmp = { | |
id: catalog[i1].id, | |
text:'', | |
name: catalog[i1].name, | |
categories: [], | |
universities: [] | |
}; | |
tmp.text += " " + catalog[i1].name; | |
tmp.text += " " + catalog[i1].instructor; | |
tmp.text += " " + catalog[i1].short_description; | |
tmp.categories = catalog[i1]['category-ids']; | |
for(var i2 in catalog[i1].categories) { | |
tmp.text += " " + catalog[i1].categories[i2].name; | |
} | |
tmp.universities = catalog[i1]['university-ids']; | |
for(var i2 in catalog[i1].universities) { | |
tmp.text += " " + catalog[i1].universities[i2].name; | |
} | |
tmp.text = tmp.text.toLowerCase(); | |
start_date = catalog[i1].courses[(catalog[i1].courses.length) - 1].start_month + '-'; | |
start_date += (catalog[i1].courses[(catalog[i1].courses.length) - 1].start_day != null) ? catalog[i1].courses[(catalog[i1].courses.length) - 1].day : 1; | |
start_date += '-' + catalog[i1].courses[(catalog[i1].courses.length) - 1].start_year; | |
tmp.added = Date.parse(); | |
tmp.duration = parseInt(catalog[i1].courses[(catalog[i1].courses.length) - 1].duration_string); | |
if (isNaN(tmp.duration)) | |
tmp.duration = null; | |
tmp.start = Date.parse(catalog[i1].courses[(catalog[i1].courses.length) - 1].start_date_string); | |
compressed.push(tmp); | |
} | |
}; | |
var getCriteria = function() { | |
var text, category, university, sorting; | |
text = jQuery(".coursera-catalog-search").val().toLowerCase(); | |
category = jQuery('input.coursera-catalog-filter-category').val() | |
university = jQuery('input.coursera-catalog-filter-university').val() | |
return { | |
text: text, | |
category: category, | |
university: university | |
} | |
} | |
var findMatchIds = function() { | |
var criteria, | |
matchKeys = [], | |
matchIds = [], | |
tmp = []; | |
criteria = getCriteria(); | |
for(var i1 in compressed) { | |
matchKeys.push(i1); | |
} | |
if (criteria.category != 'all') { | |
for(var i1 in matchKeys) { | |
if (jQuery.inArray(criteria.category,compressed[matchKeys[i1]].categories) > -1) { | |
tmp.push(matchKeys[i1]); | |
} | |
} | |
matchKeys = tmp; | |
tmp = []; | |
} | |
if (criteria.university != 'all') { | |
for(var i1 in matchKeys) { | |
if (jQuery.inArray(criteria.university,compressed[matchKeys[i1]].universities) > -1) { | |
tmp.push(matchKeys[i1]); | |
} | |
} | |
matchKeys = tmp; | |
tmp = []; | |
} | |
if (criteria.text.length > 1) { | |
for(var i1 in matchKeys) { | |
if (compressed[matchKeys[i1]].text.search(criteria.text) > -1) { | |
tmp.push(matchKeys[i1]); | |
} | |
} | |
matchKeys = tmp; | |
} | |
for (var i1 in matchKeys) { | |
matchIds.push(compressed[matchKeys[i1]].id); | |
} | |
showMatches(matchIds); | |
}; | |
var showMatches = function(matchIds) { | |
jQuery('.coursera-catalog-results-none').hide(); | |
jQuery('.coursera-course-listing-box').hide(); | |
if (matchIds.length === 0) { | |
jQuery('.coursera-catalog-results-none').show(); | |
} else { | |
for (var i1 in matchIds) { | |
jQuery('.coursera-course-listing-box[data-id="' + matchIds[i1] + '"]').show(); | |
/* | |
* uncomment this for dynamic loading of images | |
jQuery('.coursera-course-listing-box[data-id="' + matchIds[i1] + '"]').show().find('img').each(function(i1,el) { | |
el.src = jQuery(el).attr('data-src'); | |
}); | |
*/ | |
} | |
} | |
}; | |
var alterDropdownCss = function(ctx) { | |
var $this = jQuery(ctx), | |
criteria = $this.text(), | |
criteriaCode = $this.attr('data-value'), | |
$dropdown = $this.closest(".btn-group"), | |
label = $dropdown.find("span.dropdown-label"), | |
maxHeight = 0, | |
thisHeight, currentHeight; | |
$dropdown.find('input').val(criteriaCode); | |
label.empty(); | |
label.css("font-size", Math.max(14, 24 - Math.ceil(criteria.length / 3))); | |
label.append(criteria); | |
jQuery(".coursera-catalog-filters .btn-group").each(function() { | |
thisHeight = $dropdown.find(".btn").height(); | |
thisHeight > maxHeight && (maxHeight = thisHeight); | |
}); | |
currentHeight = Number(jQuery(".coursera-catalog-filters").css("height")); | |
(isNaN(currentHeight) || Math.abs(maxHeight - currentHeight) > 10) && jQuery(".coursera-catalog-filters").height(maxHeight + 20); | |
}; | |
bindIdsToDom(); | |
compressData(); | |
jQuery(".coursera-catalog-filters ul li a").off('click'); | |
jQuery(".coursera-catalog-filters ul li a").click(function() { | |
alterDropdownCss(this); | |
findMatchIds(); | |
}); | |
jQuery(".coursera-catalog-sort").off('change'); | |
jQuery(".coursera-catalog-sort").on('change', function() { | |
var sortKey = jQuery(this).val(); | |
compressed.sort(function(a,b) { | |
if (a[sortKey] == null && b[sortKey] == null) | |
return 0; | |
else if (a[sortKey] == null) | |
return 1; | |
else if (b[sortKey] == null) | |
return -1; | |
else if (a[sortKey] < b[sortKey]) | |
return -1; | |
else if (a[sortKey] === b[sortKey]) | |
return 0; | |
else | |
return 1; | |
}); | |
for(var i1 in compressed) { | |
jQuery('.coursera-catalog-listings').append(jQuery('.coursera-course-listing-box[data-id="' + compressed[i1].id + '"]')); | |
} | |
}); | |
jQuery(".coursera-catalog-search").unbind(); | |
jQuery(".coursera-catalog-search").on("keyup input textinput change", findMatchIds); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment