-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
313 lines (263 loc) · 7.74 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// ==UserScript==
// @name WaniKani Upcoming Lessons
// @namespace goldenchrysus.wanikani.upcominglessons
// @description Shows section of upcoming radicals, kanji, and vocabulary on lessons page
// @author GoldenChrysus
// @website https://github.com/GoldenChrysus
// @version 1.1.3
// @include https://www.wanikani.com/lesson
// @copyright 2018+, Patrick Golden
// @license MIT; http://opensource.org/licenses/MIT
// @run-at document-end
// @grant none
// ==/UserScript==
(function() {
"use strict";
// Establish important variables
let wkof = window.wkof;
let modules = "ItemData";
let queue = {
radical : [],
kanji : [],
vocabulary : []
};
let $container = $(`<div id="upcoming-lessons"></div>`);
// Check for WaniKani Open Framework
if (!window.wkof) {
if (confirm("Upcoming Lessons requires WaniKani Open Framework.\nDo you want to be forwarded to the installation instructions?")) {
window.location.href = "https://community.wanikani.com/t/instructions-installing-wanikani-open-framework/28549";
}
return;
}
// Start WaniKani Open Framework
wkof.include(modules);
wkof
.ready(modules)
.then(initialize);
/**
* Insert the "Upcoming Lessons" section into the DOM
*/
function insertContainer() {
$("#lessons-summary")
.find("div.pure-g-r")
.last()
.before($container);
}
/**
* Create radicals section and add to container
*/
function createRadicals() {
let radicals = queue.radical;
let count = radicals.length;
let $radical_container = $(
`<div class="pure-g-r">
<div id="radicals" class="pure-u-1">
<h2><b>${count} <i class="icon-time" style="margin-left:0.25em; margin-right:0.2em;"></i></b> Radicals</h2>
</div>
</div>`
);
if (count) {
$radical_container
.find("#radicals")
.append(
`<div>
<ul id="lesson-radicals">
</ul>
</div>`
);
}
let $radical_list = $radical_container.find("#lesson-radicals");
for (let item of radicals) {
let character = item.characters;
let english = item.meanings[0].meaning;
// Some radicals are custom-made by WaniKani or have no available text character, so they are rendered as images.
if (!character && item.character_images) {
let image = item.character_images[0].url;
for (const tmp_image of item.character_images) {
if (tmp_image.metadata.style_name === "32px") {
image = tmp_image.url;
break;
}
}
character = `<img src="${image}" alt="${english}" style="width: 0.8em; filter: invert(100%);"/>`;
}
let $element = $(
`<li class="radical" data-en="${english}">
<a lang="ja" href="/radicals/${english}">${character}</a>
</li>`
);
$radical_list.append($element);
}
$container.append($radical_container);
}
/**
* Create kanji section and add to container
*/
function createKanji() {
let kanji = queue.kanji;
let count = kanji.length;
let $kanji_container = $(
`<div class="pure-g-r">
<div id="kanji" class="pure-u-1">
<h2><b>${count} <i class="icon-time" style="margin-left:0.25em; margin-right:0.2em;"></i></b> Kanji</h2>
</div>
</div>`
);
if (count) {
$kanji_container
.find("#kanji")
.append(
`<div>
<ul id="lesson-kanji">
</ul>
</div>`
);
}
let $kanji_list = $kanji_container.find("#lesson-kanji");
for (let item of kanji) {
let character = item.characters;
let english = item.meanings[0].meaning;
let readings = [];
for (let reading of item.readings) {
if (reading.primary) {
readings.push(reading.reading);
}
}
readings = readings.join(", ");
let $element = $(
`<li class="kanji" data-en="${english}" data-ja="${readings}">
<a lang="ja" href="/kanji/${character}">${character}</a>
</li>`
);
$kanji_list.append($element);
}
$container.append($kanji_container);
}
/**
* Create vocabulary section and add to container
*/
function createVocabulary() {
let vocabulary = queue.vocabulary;
let count = vocabulary.length;
let $vocabulary_container = $(
`<div class="pure-g-r">
<div id="vocabulary" class="pure-u-1">
<h2><b>${count} <i class="icon-time" style="margin-left:0.25em; margin-right:0.2em;"></i></b> Vocabulary</h2>
</div>
</div>`
);
if (count) {
$vocabulary_container
.find("#vocabulary")
.append(
`<div>
<ul id="lesson-vocabulary">
</ul>
</div>`
);
}
let $vocabulary_list = $vocabulary_container.find("#lesson-vocabulary");
for (let item of vocabulary) {
let character = item.characters;
let english = item.meanings[0].meaning;
let readings = [];
for (let reading of item.readings) {
if (reading.primary) {
readings.push(reading.reading);
}
}
readings = readings.join(", ");
let $element = $(
`<li class="vocabulary" data-en="${english}" data-ja="${readings}">
<a lang="ja" href="/vocabulary/${character}">${character}</a>
</li>`
);
$vocabulary_list.append($element);
}
$container.append($vocabulary_container);
}
/**
* Create header section and add to container
*/
function createHeader() {
let $header = $(
`<div class="pure-g-r">
<header class="pure-u-1">
<h1><i class="icon-time"></i> Upcoming Lessons</h1>
</header>
</div>`
);
$container.append($header);
}
/**
* Handle hovering and clicking on the lesson items.
* Copied from WaniKani source.
*/
function initializeEventHandlers() {
$(document).on("click", "#upcoming-lessons a", function(t) {
let agent = /iPhone|iPod|iPad|Android|BlackBerry/.test(navigator.userAgent);
if (agent) {
return t.preventDefault();
}
});
$(document).on("mouseenter", "#upcoming-lessons ul li", function() {
var e, t, n, i, r, a, o, s;
return r = $(this).height() + 4, a = $(window).width() - $(this).offset().left, i = $(window).height() - $(this).offset().top, n = $("<div></div>", {
"class": "hover"
}).appendTo(this), $("<ul></ul>").appendTo($(this).children("div")), $("<li></li>", {
text: $(this).data("en")
}).appendTo($(this).find("ul")), $("<li></li>", {
text: $(this).data("ja")
}).appendTo($(this).find("ul")), $("<li></li>", {
text: $(this).data("mc")
}).appendTo($(this).find("ul")), $("<li></li>", {
text: $(this).data("rc")
}).appendTo($(this).find("ul")), o = a > 200 ? (e = "left-side", "auto") : (e = "right-side", "0"), s = i < 100 ? (t = "down-arrow", -1 * (n.height() + r / 2)) : (t = "up-arrow", r), n.css({
top: s,
right: o
}).addClass(t + " " + e);
});
$(document).on("mouseleave", "#upcoming-lessons ul li", function() {
$(this)
.children("div")
.remove();
});
}
/**
* Load assignment and item data, then process it.
*/
function initialize() {
wkof.ItemData.get_items("assignments").then(processData);
}
/**
* Filter item data for assigned lessons, then create DOM elements.
*/
function processData(data) {
for (var item of data) {
// If the item has no assignments, then it isn't assigned.
if (!item.assignments) {
continue;
}
// If an assignment isn't started or isn't unlocked, it's not an assigned lesson.
if (item.assignments.started_at || !item.assignments.unlocked_at) {
continue;
}
item.data.sort_date = item.assignments.unlocked_at || item.assignments.started_at;
queue[item.object].push(item.data);
}
for (let type in queue) {
queue[type].sort((a, b) => {
if (a.level !== b.level) {
return (a.level < b.level) ? -1 : 1;
}
return (a.lesson_position < b.lesson_position) ? -1 : 1;
})
}
createHeader();
createRadicals();
createKanji();
createVocabulary();
insertContainer();
initializeEventHandlers();
}
}());