-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathduolingo_fluencytuneup.user.js
120 lines (109 loc) · 3.56 KB
/
duolingo_fluencytuneup.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// ==UserScript==
// @name DuoFluencyTuneup
// @namespace https://github.com/liuch/duolingo-scripts
// @include https://www.duolingo.com/*
// @version 0.1.6
// @grant none
// @description Fluency score tune up
// @description:ru Настройка отображения fluency score
// @updateURL https://github.com/liuch/duolingo-scripts/raw/master/duolingo_fluencytuneup.meta.js
// @downloadURL https://github.com/liuch/duolingo-scripts/raw/master/duolingo_fluencytuneup.user.js
// @author FieryCat aka liuch
// @license MIT License
// ==/UserScript==
function inject(f) { //Inject the script into the document
var script;
script = document.createElement('script');
script.type = 'text/javascript';
script.setAttribute('name', 'duofluencytuneup');
script.textContent = '(' + f.toString() + ')(jQuery)';
document.head.appendChild(script);
}
inject(f);
function f($) {
var fluency_score = -1;
var digits = 2;
if (duo && duo.getCookie) {
digits = duo.getCookie().fluency_tuneup_digits;
if (typeof digits == "string" && digits.match("^-?[0-9]+$")) {
digits = parseInt(digits);
if (digits > 5)
digits = 5;
} else
digits = 2;
}
var menu_data = [
/*{ val: -1, text: "Disable fluency" },*/ { val: 0 }, { val: 1 }, { val: 2 }, { val: 3 }, { val: 4 }, { val: 5 }
];
var fluency_split = function(f, d) {
var factor = Math.pow(10, d);
var res = {};
var i = Math.floor(f * 100 * factor) / factor;
res.left = "" + Math.floor(i);
res.right = i.toFixed(d).replace(/^[0-9]+/, "");
return res;
};
function fluency_str(f_s) {
var res = f_s.left;
if (f_s.right)
res += f_s.right;
return res;
}
function update_shield() {
if (!duo || duo.view != "home")
return;
var shs = $(".fluency-score-shield-silver");
if (shs.length) {
shs.empty();
var d = fluency_split(fluency_score, digits);
shs.text(d.left);
shs.append('<span class="fluency-score-percent">%</span>');
if (digits > 0)
shs.append('<span class="fluency-fraction" style="font-size:50%;display:block;margin-top:0.2em;" title="' + (fluency_score * 100) + '">' + d.right + '</span>');
}
}
var fluency_switch = function(d) {
var i = d.data.val;
if (duo && duo.setCookie) {
digits = i;
duo.setCookie("fluency_tuneup_digits", i);
update_shield();
}
};
function make_menu() {
var b = $(".fluency-container").eq(0).parent().children("h2").eq(0);
if (b.length && !b.find("#fluency-settings").length) {
var g = $('<span id="fluency-settings" data-toggle="tooltip" title="DuoFluencyTuneup" class="icon icon-gear-small right"><ul class="dropdown-menu hidden" style="position:relative;"></ul></span>');
b.append(g);
var menu = g.find(".dropdown-menu");
g.click(function(){menu.toggle();});
menu_data.forEach(function(d) {
var t = (d.text) ? d.text : fluency_str(fluency_split(fluency_score, d.val));
var item = $('<li><a href="javascript:;">' + t + '</a></li>');
item.click({val: d.val}, fluency_switch);
menu.append(item);
});
}
}
var fix_displaying = function() {
if (fluency_score > 0 && digits >= 0) {
make_menu();
if (digits > 0)
update_shield();
}
};
var fluency_f = function(fs) {
var res = 0;
if (digits >= 0) {
res = Math.floor(fs*100);
fluency_score = fs;
setTimeout(fix_displaying, 200);
}
return res;
};
duo.formatFluencyScore = fluency_f;
if (duo && duo.user && duo.user.attributes.learning_language) {
fluency_score = duo.user.attributes.language_data[duo.user.attributes.learning_language].fluency_score;
fix_displaying();
}
}