-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMemrise_All_Typing.user.js
146 lines (135 loc) · 8.36 KB
/
Memrise_All_Typing.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
// ==UserScript==
// @name Memrise All Typing
// @namespace https://github.com/cooljingle
// @description All typing / no multiple choice when doing Memrise typing courses
// @match https://www.memrise.com/course/*/garden/*
// @match https://www.memrise.com/garden/review/*
// @match https://app.memrise.com/course/*/garden/*
// @match https://app.memrise.com/garden/review/*
// @match https://decks.memrise.com/course/*/garden/*
// @match https://decks.memrise.com/garden/review/*
// @version 0.2.1
// @updateURL https://github.com/cooljingle/memrise-all-typing/raw/master/Memrise_All_Typing.user.js
// @downloadURL https://github.com/cooljingle/memrise-all-typing/raw/master/Memrise_All_Typing.user.js
// @grant none
// ==/UserScript==
$(document).ready(function() {
//*****************************************************************************************************************************************************
//MEMRISE ALL TYPING
//-----------------------------------------------------------------------------------------------------------------------------------------------------
// This userscript prevents multiple choice from occuring, replacing it with a typing prompt.
// You can configure the extent to which this happens by clicking the 'All Typing Settings' link on the left when in a learning session.
// Changes you make will be saved locally on your machine.
//*****************************************************************************************************************************************************
var localStorageIdentifier = "memrise-all-typing",
localStorageObject = JSON.parse(localStorage.getItem(localStorageIdentifier)) || {};
$("body").append(
`
<div class='modal fade' id='all-typing-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'>All Typing Settings</h1>
</div>
<div class='modal-body'>
<div>
<input class='all-typing-setting' id='include-reviews' type='checkbox'>
<label for='include-reviews'>Include Reviews</label>
<em style='font-size:85%'>only typing on old items you are reviewing</em>
</div>
<div>
<input class='all-typing-setting' id='include-learning' type='checkbox'>
<label for='include-learning'>Include Learning</label>
<em style='font-size:85%'>only typing on new items you are learning</em>
</div>
<div>
<input class='all-typing-setting' id='replace-tapping' type='checkbox'>
<label for='replace-tapping'>Replace Tapping</label>
<em style='font-size:85%'>tapping tests will be converted to typing (along with multiple choice)</em>
</div>
<div>
<input class='all-typing-setting' id='replace-audio-multiple-choice' type='checkbox'>
<label for='replace-audio-multiple-choice'>Replace Audio Multiple Choice</label>
<em style='font-size:85%'>audio multiple choice tests will be converted to typing (along with multiple choice)</em>
</div>
</div>
</div>
</div>
</div>
`);
$('#left-area').append("<a data-toggle='modal' data-target='#all-typing-modal'>All Typing Settings</a>");
$('#all-typing-modal').on('shown.bs.modal', function() {
$(document).off('focusin.modal'); //enable focus events on modal
});
$('.all-typing-setting')
.prop('checked', function(){ return localStorageObject[$(this).attr('id')] !== false; }) //all options true by default
.css({
"vertical-align": "top",
"float": "left",
"margin-right": "5px"
})
.change(function(e){
localStorageObject[e.target.id] = $(this).is(':checked');
localStorage.setItem(localStorageIdentifier, JSON.stringify(localStorageObject));
console.log(localStorageObject);
});
MEMRISE.garden._events.start.push(() => {
addTypingOverrides();
MEMRISE.garden.populateScreens();
});
function addTypingOverrides() {
MEMRISE.garden.session.box_factory.make = (function() {
var cached_function = MEMRISE.garden.session.box_factory.make;
return function() {
var result = cached_function.apply(this, arguments);
if (arguments[0].learn_session_level) {
if(localStorageObject["include-learning"] !== false) {
makeMaybeTyping(result);
}
} else if(localStorageObject["include-reviews"] !== false) {
makeMaybeTyping(result);
}
return result;
};
}());
}
function makeMaybeTyping(box) {
if (box.template === "multiple_choice" ||
box.template === "reversed_multiple_choice" ||
(localStorageObject["replace-tapping"] !== false && box.template === "tapping") ||
(localStorageObject["replace-audio-multiple-choice"] !== false && box.template === "audio-multiple-choice")) {
var boxCopy = jQuery.extend({}, box);
box.template = MEMRISE.garden.session.box_factory.makeMaybeTyping(boxCopy).template;
}
}
MEMRISE.garden.populateScreens = function() {
_.each(MEMRISE.garden.learnables || _.indexBy(MEMRISE.garden.session_data.learnables, 'learnable_id'), function(v, k) {
var learnableScreensNew = (MEMRISE.garden.screens || MEMRISE.garden.session_data.screens)[k];
var learnableScreens = _.reduce(learnableScreensNew, (x, v, k) => {x[v.template] = v; return x;}, {});
if(learnableScreens && !_.contains(Object.keys(learnableScreens), "typing")) {
var screenBase = _.find([learnableScreens.multiple_choice, learnableScreens.reversed_multiple_choice], s => s.answer.kind === "text");
var column = _.find([v.item, v.definition], c => c.kind === "text");
if(screenBase) {
var typingScreen = $.extend({}, screenBase, {
template: "typing",
choices: "",
correct: _.uniq(_.flatten(_.map([column.value, ...column.alternatives, ...(learnableScreens.tapping ? _.map(learnableScreens.tapping.correct, t => t.join(" ")) : [])], function(y) {
return _.isArray(y) ? y : [y, _.map([..._.compact(y.split(/[();]+/)), y], function(x) { //bracket/semicolon delimitation
return x.replace(/\./g, " ") //full stops are treated like spaces to memrise
.replace(/[\u00a0\u00A0]/g, " ") //sinister no-break spaces!
.replace(XRegExp('[\\p{P}\\p{S}\\p{C}\u064B-\u065B]+', 'g'), "") //strip punctuation
.replace(/\s{2,}/g, " ") //clear multiple spaces (e.g. from "a ... b")
.trim() // trim spaces at beginning and end
.toLowerCase(); //lowercase required
})];
})))
});
var screenIndex = Number(_.last(Object.keys(learnableScreensNew))) + 1;
learnableScreensNew[screenIndex] = typingScreen;
MEMRISE.garden.screen_template_map[k].typing = [typingScreen];
}
}
});
};
});