Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This program's bookmarklet, khanacademy.org/cs/i/6347724702416896, ported to TamperMonkey. Still quite buggy and doesn't always work.
// ==UserScript==
// @name Show flags on programs
// @version 1.1
// @description Shows number of flags for a program, even on the hot list (khanacademy.org/cs/i/6347724702416896)
// @author Eytukan (ported by Matthias)
// @match https://www.khanacademy.org/computing/computer-programming/browse
// @match https://www.khanacademy.org/profile/*/projects
// @grant none
// ==/UserScript==
(function() {
'use strict';
console.log("Hello");
var programLinks;
var id, counter, ids, objs, result;
var interval;
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
callback(null, xhr.response);
} else {
callback(status);
}
};
xhr.send();
};
function handleResponse(data) {
ids.push(parseInt(programLinks[counter].href.split("/")[5]));
objs.push(data);
if ( counter === programLinks.length - 1 ) {
result = objs.sort(function(a,b){
return ids.indexOf(a.id) < ids.indexOf(b.id) ? -1 : 1;
});
for (var j = 0; j < result.length; j++) {
programLinks[j].nextSibling.nextSibling.innerHTML += " · " + result[j].flags.length + " Flag" + (result[j].flags.length === 1 ? "" : "s");
}
}
}
function init() {
programLinks = document.getElementsByClassName("link_1uvuyao-o_O-noUnderline_4133r1");
counter = 0;
ids = [];
objs = [];
if (programLinks.length === 0) {
console.log("Failed to find programs");
return;
}
window.clearInterval(interval);
var buttons = document.getElementsByClassName("sorts_1l7kz3k").childNodes;
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function () {
interval = window.setInterval(init, 500);
});
}
for (var i = 0; i < programLinks.length; i++) {
id = programLinks[i].href.split("/")[5];
getJSON("https://www.khanacademy.org/api/internal/scratchpads/" + id, function(err, data) {
handleResponse(data);
counter++;
});
}
}
interval = window.setInterval(init, 500);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment