Skip to content

Instantly share code, notes, and snippets.

@Aracturat
Last active May 5, 2017 19:36
Show Gist options
  • Save Aracturat/a42e5304b660ef09a7c73cfbae27a705 to your computer and use it in GitHub Desktop.
Save Aracturat/a42e5304b660ef09a7c73cfbae27a705 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Add statistics to Memrise
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.memrise.com/home/
// @grant none
// ==/UserScript==
(function() {
'use strict';
$.getJSON("https://www.memrise.com/ajax/courses/dashboard/?courses_filter=with_goal&limit=100").then(
function(info) {
var courses_stat = info.courses.map(function(e) { return { id: e.id, learned: e.learned, points: e.goal.points };});
var full_stats = $("div.profile-content > div.content-stats");
var day_stats = full_stats.clone();
var full_words = + full_stats.find(".number")[0].innerText.replace(",","");
var full_points = + full_stats.find(".number")[1].innerText.replace(",","");
var days_stats = localStorage["days_stats"] ? JSON.parse(localStorage["days_stats"]) : null;
var todays_date = new Date();
var yesterday_date = new Date();
yesterday_date.setDate(todays_date.getDate() - 1);
if (days_stats) {
if ((new Date(days_stats[0].date)).setHours(0,0,0,0) == todays_date.setHours(0,0,0,0)) {
days_stats.shift(-1);
}
} else {
days_stats = [
{
date: yesterday_date,
words: full_words,
points: full_points,
courses: courses_stat.map(function(e) { return { id: e.id, learned: e.learned, points: 0 };})
}];
}
days_stats.unshift({
date: todays_date,
words: full_words,
points: full_points,
courses: courses_stat
});
localStorage.setItem("days_stats", JSON.stringify(days_stats));
day_stats.find(".number")[0].innerText = days_stats[0].words - days_stats[1].words;
day_stats.find(".number")[1].innerText = days_stats[0].points - days_stats[1].points;
full_stats.parent().append(day_stats);
addCountsToCourses($(".course-card-container.js-course-card-container").toArray());
$("#content > div > div.tabbed-main.page > section > div").bind("DOMNodeInserted", function(e) {
var newElements = null;
// One element added.
if (e.target.className === "course-card-container js-course-card-container") {
newElements = [ e.target ];
}
// All element added.
if (e.target.childElementCount != 0 && e.target.firstElementChild.className === "course-card-container js-course-card-container") {
newElements = $(e.target.children).map(function(i, el) { return el; }).toArray();
}
addCountsToCourses(newElements);
});
});
function addCountsToCourses(elements) {
if (elements) {
var days_stats = localStorage["days_stats"] ? JSON.parse(localStorage["days_stats"]) : null;
elements.forEach(function(el) {
var courseId = + el.id.split("-")[1];
var today_course_stat = days_stats[0].courses.filter(function(el) { return el.id == courseId;});
var yesterday_course_stat = days_stats[1].courses.filter(function(el) { return el.id == courseId;});
if (today_course_stat.lenght != 0 && yesterday_course_stat.length != 0) {
var field = $(el).find("div.card-main-container > div > div > div.wrapper > div > div.right");
field.text(field.text() + " (" +(today_course_stat[0].learned - yesterday_course_stat[0].learned) +" words, " +
today_course_stat[0].points +" points today)");
}
});
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment