-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentences.js
166 lines (152 loc) · 5.6 KB
/
sentences.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
// ==UserScript==
// @name WaniKani Vocab Sentences
// @namespace http://mozartpetter.com/wanikani
// @version 0.2
// @description Loads sentences from jisho.org and displays it into the vocab screen of the WaniKani website.
// @match http://www.wanikani.com/review/session
// @copyright 2013, Mozart Petter
// @license http://creativecommons.org/licenses/by/3.0/deed.en_US
// ==/UserScript==
// --- YOU CAN CHANGE THOSE VALUES IF YOU WANT --- //
// The number of sentences you want to view at once.
var TOTAL_SENTENCES = 3;
// This will filter sentences that contains more than X chars on it.
var MAX_CHARS_INTO_SENTENCE = 30;
// If you don't want to see the translations, set it to false.
var SHOW_TRANSLATIONS = true;
// --- YOU SHOULDN'T CHANGE THOSE VALUES --- //
// Sets debug mode on. You can play with it if you want't, but it will break the webpage. ;)e
var DEBUG = false;
// --- DON'T TOUCH HERE UNLESS YOU KNOW WHAT YOU'RE DOING --- //
/**
* Checks if the current screen is a vocab screen.
*/
function checkIfVocab() {
var header = document.getElementsByTagName("h1")[0];
var isVocab = header.className == "vocabulary";
if (isVocab) {
var vocab = header.childNodes[0].innerHTML;
getSentencesForVocab(vocab);
}
}
/**
* Parses the given html code.
*
* @param html The HTML code to be parsed.
* @parma vocab The current vocab word.
*/
function parseContent(html, vocab) {
var wrapper = document.createElement('div');
wrapper.innerHTML = html;
var sentenceNode = wrapper.getElementsByClassName('japanese');
var translationNode = wrapper.getElementsByClassName('english');
var content = getContentFromDom(sentenceNode, translationNode, vocab);
showContent(content);
}
/**
* Extracts the sentences and translations from the given DOM object.
*
* @param sentenceNodes A collection of the HTML elements containing the sentences.
* @param translationNodes A collection of the HTML elements containing the translations.
* @param vocab The current vocab word.
*/
function getContentFromDom(sentenceNodes, translationNodes, vocab) {
var excludedIndexes = [];
var content = [];
for (var i = 0, c = 0; c < TOTAL_SENTENCES && i < sentenceNodes.length; i++) {
var randomIndex = getRandomNumber(sentenceNodes.length, excludedIndexes);
excludedIndexes.push(randomIndex);
var sentenceNode = sentenceNodes[randomIndex];
var translationNode = translationNodes[randomIndex];
var sentence = sentenceNode.innerText;
if (sentence.length > MAX_CHARS_INTO_SENTENCE) continue;
var translation = translationNode.innerText;
var hightlightedVocab = '<strong class="kanji-highlight" style="padding-left: 2px; padding-right: 2px;color:#666;background-color:#EEE">' + vocab + '</strong>';
sentence = sentence.replace(vocab, hightlightedVocab, "gi");
content[c] = [];
content[c].push(sentence);
content[c].push(translation);
c++;
}
return content;
}
/**
* Generates a random number using max as a limit and excluding
* any numbers from exclude collection.
*
* @param max The limit for the random number.
* @param exclude A list with numbers to be excluded from the randomization.
*/
function getRandomNumber(max, exclude) {
var result = Math.floor(Math.random() * max);
for (var i = 0; i < exclude.length; i++) {
if (exclude[i] == result) {
return getRandomNumber(max, exclude);
}
}
return result;
}
/**
* Show the sentences from the given collection.
*
* @param sentences A collection of sentences to be displayed.
*/
function showContent(sentences) {
var container = document.getElementById('item-info-main');
var htmlCode = '<h3>Usage Examples</h3>';
htmlCode += '<ul style="padding: 0px;line-height: 1.8em;list-style: decimal inside;">';
for (var i = 0; i < sentences.length; i++) {
htmlCode += '<li style="font-weight: bold;color: #888;">';
htmlCode += '<span style="font-weight: normal; color: #333;padding-left: 5px;display:block;">' + sentences[i][0] + '</span>';
if (SHOW_TRANSLATIONS) {
htmlCode += '<span style="display: block;margin-bottom: 10px;font-weight: normal;color: #888;padding-left: 20px;">' + sentences[i][1] + '</span>';
}
htmlCode += '</li>';
}
htmlCode += '<ul>';
htmlCode += '</ul>';
var node = document.createElement('div');
node.style.display = 'block';
node.style.marginBottom = '40px';
node.innerHTML = htmlCode;
container.insertBefore(node, container.firstChild);
}
/**
* Loads the sentences for the given vocab.
*
* @param vocab The current vocab word.
*/
function getSentencesForVocab(vocab) {
GM_xmlhttpRequest ({
method: "GET",
url: "http://www.jisho.org/sentences?jap=" + vocab,
onload: function (response) {
parseContent(response.responseText, vocab);
}
});
}
/**
* Do a setup to handle events from the question form.
*/
function setupForm() {
var form = document.getElementById('question-form');
form.addEventListener("submit", function() {
checkIfVocab();
});
}
/**
* Called when the document is ready.
*/
document.onreadystatechange = function () {
var state = document.readyState;
if (state == 'complete') {
setupForm();
}
if (DEBUG) {
var infoElem = document.getElementById('information');
var itemInfoElem = document.getElementById('item-info');
infoElem.style.display = itemInfoElem.style.display = 'block';
checkIfVocab();
}
}
// --- I TOLD YOU SO! NOW THE CODE IS BROKEN... :( --- //