Skip to content

Instantly share code, notes, and snippets.

@camiloaa
Forked from arekolek/Duolingo_Trim_tree.user.js
Last active October 25, 2017 13:56
Show Gist options
  • Save camiloaa/6b902c11f7ab44a4c3ef to your computer and use it in GitHub Desktop.
Save camiloaa/6b902c11f7ab44a4c3ef to your computer and use it in GitHub Desktop.
Duolingo Trim tree [mirror]
// ==UserScript==
// @name Duolingo Trim tree
// @namespace 9a84a9d7b3fef7de9d2fd7155dcd794c
// @description Hides all golden skills with a button.
// @author Arek Olek
// @match https://www.duolingo.com/*
// @icon https://raw.githubusercontent.com/camiloaa/duolingotreeenhancer/master/duolingo.png
// @grant none
// @updateURL https://gist.github.com/camiloaa/6b902c11f7ab44a4c3ef/raw/Duolingo_Trim_tree.user.js
// @downloadURL https://gist.github.com/camiloaa/6b902c11f7ab44a4c3ef/raw/Duolingo_Trim_tree.user.js
// @version 1.8.5
// ==/UserScript==
// Credit for [the idea](https://www.duolingo.com/comment/7146895) goes to Thomas de Roo.
var DuoState = JSON.parse(localStorage.getItem('duo.state'));
var threshold_class = ["XXX0", "_22Kj3", "XXX2", "rgwrb", "_2SEng", "dYBTa"];
var observer = new MutationObserver(function(mutations){
initialize();
});
function initialize() {
// Without the timeout, only some of the skills get hidden
setTimeout(function() {
if (!document.getElementById("toggleskills"))
{
var tree = document.getElementsByClassName("mAsUf")[0];
var flag = tree.getElementsByClassName("_2XSZu")[0];
var button = document.createElement("button");
button.id = "toggleskills";
button.onclick = updateOnClick;
button.className = "_3LN9C _3QG2_ _1vaUe _3IS_q _1XnsG _1vaUe _3IS_q";
button.style = "margin-left: 5px; width: 130px; height: 42px; "
+ "display: block;"
+ "visibility: visible;";
tree.insertBefore(button, flag);
update();
}
// Handle switching languages
}, 100);
}
function updateOnClick() {
var item_name = "trim_treshold-" + DuoState.user.fromLanguage + "-"
+ DuoState.user.learningLanguage;
var threshold = localStorage.getItem(item_name, 6);
if (threshold == null) threshold = 6;
localStorage.setItem(item_name, strongerThan(threshold > 5 ? 0 : threshold));
update();
}
function update() {
clearTimeout(update.tid);
var item_name = "trim_treshold-" + DuoState.user.fromLanguage + "-"
+ DuoState.user.learningLanguage;
var threshold = localStorage.getItem(item_name, 6);
if (threshold == null) threshold = 6;
var trimmed = threshold < 6;
// Show current level and next available action
var button = document.getElementById("toggleskills");
button.textContent = trimmed ? threshold-1 + ' bars or less' : 'Everything';
update.tid = setTimeout(function() {
button.textContent = trimmed ? 'Grow tree' : 'Trim tree';
}, 2000);
// Show or hide items depending on current state
for(var strength = 0; strength <= 5; ++strength)
{
var tree = document.getElementsByClassName("i12-l")[0];
nodes = tree.getElementsByClassName(threshold_class[strength]);
var new_display = (strength >= threshold) ? "none" : "";
for (var i = 0; i < nodes.length; i++)
nodes[i].parentNode.parentNode.style.display = new_display;
}
var new_display = (trimmed) ? "none" : "";
nodes = document.getElementsByClassName("_2Np2b");
for (var i = 0; i < nodes.length; i++)
nodes[i].parentNode.style.display = new_display;
}
function strongerThan(strength) {
for(++strength; strength <= 5; ++strength) {
var skills = document.getElementsByClassName(threshold_class[strength]);
if (skills.length > 0) {
break;
}
}
return strength;
}
// Handle navigation that does not reload the page
new MutationObserver(function() {
if (window.location.pathname == "/")
initialize();
}).observe(document.body, {childList: true, subtree: true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment