-
Notifications
You must be signed in to change notification settings - Fork 5
/
duolingo-vocab-exporter.js
74 lines (68 loc) · 2.03 KB
/
duolingo-vocab-exporter.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
/*
* Duolingo Vocab Exporter
* Created by Ruddick Lawrence
* ===========================
* Javascript to create CSV vocab lists from
* Duolingo vocab pages. Assumes jQuery is
* loaded and a certain structure for the
* vocab page (so it will break every
* redesign they do).
*/
(function() {
function scrapeVocab() {
var vocab = [];
// Get words and translations
var wordRows = $('table.vocabulary > tbody').children('tr');
wordRows.each(function(index, row) {
var $row = $(row);
var wordInfo = null;
// A new word
if($row.hasClass('first-lemma-row')) {
var canonicalWord = $row.find('td:first a').text();
wordInfo = {
canonical: canonicalWord,
otherForms: [],
translations: []
};
vocab.push(wordInfo);
}
// Another form of previous word
else {
wordInfo = vocab[vocab.length-1];
}
var lexeme = $row.children('td.lexeme');
var form = lexeme.find('.token').text();
if(form != wordInfo.canonical) {
wordInfo.otherForms.push(form);
}
lexeme.find('tbody tr:not(.hidden) td').each(function(index, td) {
wordInfo.translations.push($(td).text().trim());
});
});
// Format vocab list
var csv = "";
$.each(vocab, function(index, item) {
var words = item.canonical + (item.otherForms.length !== 0 ? ", " + item.otherForms.join(', ') : "");
var row = '"' + words + '","' + item.translations.join(', ') + '"\n';
csv += row;
});
// Display list
var pre = $('<pre>');
pre.text(csv);
$('body').children().remove();
$('body').append(pre);
}
function scrollDown() {
// Indicates all words are loaded
if($('#vocab-spinner').length === 0) {
// Give a little time for last vocab to load
// Should really just check if it's there, but whatever...
setTimeout(scrapeVocab, 1000);
}
else {
$(document).scrollTop($(document).height());
setTimeout(scrollDown, 100);
}
}
scrollDown();
})();