-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMemrise_Auto_Learn.user.js
171 lines (158 loc) · 7.18 KB
/
Memrise_Auto_Learn.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// ==UserScript==
// @name Memrise Auto Learn
// @namespace https://github.com/cooljingle
// @description Fast-track the growth level of words you are planting
// @match https://www.memrise.com/course/*/garden/learn*
// @match https://app.memrise.com/course/*/garden/learn*
// @version 0.0.15
// @updateURL https://github.com/cooljingle/memrise-auto-learn/raw/master/Memrise_Auto_Learn.user.js
// @downloadURL https://github.com/cooljingle/memrise-auto-learn/raw/master/Memrise_Auto_Learn.user.js
// @grant none
// ==/UserScript==
$(document).ready(function() {
var localStorageKeyIdentifier = "memrise-auto-learn-key",
localStorageDefaultIdentifier = "memrise-auto-learn-default",
autoLearnedBoxes = [],
flashLoadCount = 0,
shortcutKeyCode = JSON.parse(localStorage.getItem(localStorageKeyIdentifier)) || 113, //corresponds to F2 but you can replace this with your own shortcut key; see http://keycode.info/,
autoLearnByDefault = localStorage.getItem(localStorageDefaultIdentifier) === "true",
linkHtml = `<a data-toggle='modal' data-target='#auto-learn-modal'>Auto Learn</a>`,
modalHtml =
`
<div class='modal fade' id='auto-learn-modal' tabindex='-1' role='dialog'>
<div class='modal-dialog' role='document'>
<div class='modal-content'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'><span >×</span></button>
<h1 class='modal-title' id='all-typing-modal-label'>Auto Learn Settings</h1>
</div>
<div class='modal-body'>
<div>
<h4>Shortcut key code:</h4>
<input id='auto-learn-key' type='text' style="width:60px;height:20px" maxlength="3">
<em style='font-size:85%'>Default is 113 (F2); see http://keycode.info/ for other codes</em>
</div>
<br>
<div>
<span>Auto learn by default: </span>
<input id='auto-learn-default' type="checkbox" style='vertical-align: text-top; margin-left: 5px'>
</div>
</div>
</div>
</div>
</div>
`;
$("body").append(modalHtml);
$('#left-area').append(linkHtml);
$('#auto-learn-modal').on('shown.bs.modal', function() {
$(document).off('focusin.modal'); //enable focus events on modal
});
$('#auto-learn-key')
.val(shortcutKeyCode)
.change(function () {
shortcutKeyCode = Number($(this).val());
localStorage.setItem(localStorageKeyIdentifier, $(this).val());
});
$('#auto-learn-default').prop('checked', autoLearnByDefault);
$('#auto-learn-default').change(function () {
var checked = $(this).is(':checked');
autoLearnByDefault = checked;
if(MEMRISE.garden.box.learnable.autoLearn !== autoLearnByDefault)
toggleAutoLearn();
localStorage.setItem(localStorageDefaultIdentifier, checked);
});
$(window).keydown(function(e) {
if(e.which === shortcutKeyCode) {
toggleAutoLearn();
}
});
function insertAutoLearn() {
$('.garden-box').prepend(`<p id='autoLearn'></p>`);
$('#autoLearn').css({
'position': 'absolute',
'top': '-20px',
'cursor': 'pointer',
})
.click(toggleAutoLearn);
setAutoLearnStyles(MEMRISE.garden.box.learnable.autoLearn);
}
function toggleAutoLearn() {
var autoLearn = MEMRISE.garden.box.learnable.autoLearn = !MEMRISE.garden.box.learnable.autoLearn;
setAutoLearnStyles(autoLearn);
}
function setAutoLearnStyles(autoLearn) {
$('#autoLearn')
.css('color', autoLearn ? 'limegreen' : 'darkgrey')
.css('right', autoLearn ? '-100px' : '-90px')
.text(autoLearn ? 'Auto learn ON 🌼' : 'Auto learn OFF');
}
function getValue(formData, name) {
var regex = new RegExp(name + "=([^&]+)");
var match = (formData || "").match(regex);
return match && match[1];
}
MEMRISE.garden._events.start.push(() => {
//clears future boxes for us
MEMRISE.garden.shouldFullyGrow = function(thinguser, learnable) {
return learnable.autoLearn;
};
MEMRISE.garden.register = (function() {
var cached_function = MEMRISE.garden.register;
return function() {
var box = arguments[0];
if(box.learnable.autoLearn){
if(arguments[1] === 1) {
box.initialGrowthLevel = box.thinguser?.growth_level || 0;
autoLearnedBoxes.push(box);
} else {
box.learnable.autoLearn = false;
}
}
return cached_function.apply(this, arguments);
};
}());
MEMRISE.garden.session.make_box = (function() {
var cached_function = MEMRISE.garden.session.make_box
return function() {
var result = cached_function.apply(this, arguments);
var canAutoLearn = arguments[0].learn_session_level < 6;
if(canAutoLearn) {
result.learnable.autoLearn = autoLearnByDefault;
window.setTimeout(insertAutoLearn, 0);
}
return result;
};
}());
});
$(document).ajaxSuccess(
function(event, request, settings) {
var thinguser = request.responseJSON && request.responseJSON.thinguser,
correctAnswer = getValue(settings.data, "score") === "1",
canUpdate = getValue(settings.data, "update_scheduling") !== "false",
box = thinguser && _.find(autoLearnedBoxes, b => b.learnable_id === thinguser.learnable_id),
isValidRequest = !!(thinguser && correctAnswer && canUpdate && box && thinguser.growth_level < 6);
if (isValidRequest) {
var hasGrown = getValue(settings.data, "growth_level") != thinguser.growth_level;
settings.data = settings.data.replace(/points=\d+(&growth_level=\d+){0,1}/, "points=0&growth_level=" + thinguser.growth_level);
if(hasGrown){
var autoLearnCount = thinguser.growth_level - box.initialGrowthLevel + 1;
var element = $('#right-area').show().find('.message').show();
var message = $.parseHTML(`<div>Auto Learn +${autoLearnCount}</div>`);
element.hide().removeClass("animated");
$.doTimeout(100, function() {
element.html(message).show().addClass("animated");
flashLoadCount++;
});
$.doTimeout(1000, function() {
if(--flashLoadCount === 0) {
element.hide().removeClass("animated");
}
});
}
setTimeout(function(){
$.post(settings.url, settings.data);
}, 300);
}
}
);
});