-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhoverdict.js
More file actions
319 lines (267 loc) · 10.5 KB
/
hoverdict.js
File metadata and controls
319 lines (267 loc) · 10.5 KB
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
314
315
316
317
318
319
var DEBUG = false;
var pressedKeys = {};
var mousePagePosition = {};
var mouseClientPosition = {};
var popup = null;
var POPUP_WIDTH = 564;
var POPUP_HEIGHT = 284;
var POPUP_WAITING_WIDTH = 220;
var POPUP_WAITING_HEIGHT = 57;
function documentMouseMove(event) {
// Store mouse position
mousePagePosition = {top:event.pageY, left:event.pageX};
mouseClientPosition = {top:event.clientY, left:event.clientX};
}
function openInNewTab(url) {
window.open(url, '_blank').focus();
}
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function lookupWord(word, initialPosition) {
// Make sure popup is empty
popup.empty();
// Show "Please wait" while parsing API results
popup.append($('<span class="hd_ext_language_header">Please wait...</span>'));
// Resize and move popup so it is inside the viewport
var position = { top: initialPosition.top, left: initialPosition.left };
position.left = Math.min(position.left + 10, document.body.scrollWidth - POPUP_WAITING_WIDTH - 40);
position.top = Math.min(position.top + 10, document.body.scrollHeight - POPUP_WAITING_HEIGHT - 40);
popup.css({"top": Math.round(position.top), "left": Math.round(position.left), "max-width": POPUP_WAITING_WIDTH, "min-width": POPUP_WAITING_WIDTH, "max-height": POPUP_WAITING_HEIGHT, "min-height": POPUP_WAITING_HEIGHT});
// Get available languages.
var languages = null;
chrome.storage.local.get('lang_list', function(properties) {
if (DEBUG == true) console.log('lang_list: ' + properties['lang_list']);
languages = properties['lang_list'];
});
// Make API call
word = word.toLowerCase();
var apiUrl = 'http://en.wiktionary.org/w/api.php?action=parse&format=json&prop=text&disabletoc=true&page=' + word;
chrome.runtime.sendMessage({action:'getJSON', url:apiUrl},
function(json) {
if (!json.hasOwnProperty("error")) {
// Create dummy element with result from API
var wikiEntry = $('<div></div>');
wikiEntry.html(json.parse.text['*']);
var wikiInfo = wikiEntry[0].children[0]; // Get the wiki HTML
// Result map
var results = {};
// Word entry
var entry = null;
function Entry(type) {
this.type = type;
this.definitions = [];
}
// Search state enum
var SearchState = {
FIND_LANGUAGE: 1,
FIND_NEW_SECTION: 2,
FIND_PRONUNCIATION: 3,
FIND_DEFINITIONS: 4,
}
var state = SearchState.FIND_LANGUAGE;
var language = "";
for (var i = 0; i < wikiInfo.children.length; i++) {
var child = wikiInfo.children[i];
switch(state) {
case SearchState.FIND_LANGUAGE: {
// The next headline we find is the language
if(child.children.length > 0 && child.children[0].className == "mw-headline") {
language = child.textContent.substr(0, child.textContent.length - 6);
state = SearchState.FIND_NEW_SECTION;
// Skip if language is not included
if (languages.length == 0 || languages.includes(language.toLowerCase())) {
// Add language to map if it doesn't exist
if(!(language in results)) {
results[language] = {
pronunciation: null,
entries: []
}
}
}
if(DEBUG == true) console.log("Current language:", language);
}
}
break;
case SearchState.FIND_NEW_SECTION: {
// Add entry
if(entry != null) {
if (language in results)
results[language].entries.push(entry);
if(DEBUG == true) console.log("Entry added:", entry);
entry = null;
}
// Find the next headline
if(child.children.length > 0 && child.children[0].className == "mw-headline") {
// What section is this?
var sectionName = child.children[0].textContent;
if(sectionName == "Pronunciation") {
state = SearchState.FIND_PRONUNCIATION;
}
else if(sectionName == "Noun" || sectionName == "Verb" || sectionName == "Adjective" || sectionName == "Adverb" || sectionName == "Pronoun" || sectionName == "Preposition" || sectionName == "Conjunction" || sectionName == "Determiner") {
state = SearchState.FIND_DEFINITIONS;
entry = new Entry(sectionName);
}
if(state == SearchState.FIND_NEW_SECTION) {
if(DEBUG == true) console.log("Section ignored:", sectionName);
}
else {
if(DEBUG == true) console.log("Section entered:", sectionName);
}
}
else if(child.tagName == "HR") {
// End of entries for this language
state = SearchState.FIND_LANGUAGE;
}
}
break;
case SearchState.FIND_PRONUNCIATION: {
// Find and store pronunciation
if(child.tagName == "UL" && child.children[0].tagName == "LI") {
var ipa = child.children[0].getElementsByClassName("IPA");
if(language in results && ipa.length > 0) {
results[language].pronunciation = ipa[0].textContent;
}
state = SearchState.FIND_NEW_SECTION;
if(language in results && DEBUG == true) console.log("Pronunciation found:", results[language].pronunciation);
}
}
break;
case SearchState.FIND_DEFINITIONS: {
if(child.tagName == "OL") {
for(var j = 0; j < child.children.length; j++) {
var listEntryChild = child.children[j];
// Parse list entry content
for(var k = listEntryChild.children.length - 1; k >= 0; k--) {
var listContentElement = listEntryChild.children[k];
// Remove long text elements
if(listContentElement.className == "HQToggle" || listContentElement.tagName == "UL") {
listEntryChild.removeChild(listContentElement);
}
}
entry.definitions.push(listEntryChild);
}
// Modify hyperlinks
walkTheDOM(child, function(node) {
// Change links so that when clicked, they will display the definition for the word clicked in the popup
if(node.tagName == "A") {
if(node.hasAttribute("href")) {
var redirectWord = node.getAttribute("href").replace("/wiki/", "");
// Remove href parameters
if(redirectWord.indexOf("#") > 0) {
redirectWord = redirectWord.substr(0, redirectWord.indexOf("#"));
}
node.redirectWord = redirectWord;
node.removeAttribute("href");
node.addEventListener('click', function(e) {
lookupWord(e.target.redirectWord, initialPosition);
});
}
}
});
state = SearchState.FIND_NEW_SECTION;
}
}
}
}
// Add entry
if(language in results && entry != null) {
results[language].entries.push(entry);
if(DEBUG == true) console.log("Entry added:", entry);
}
popup.empty();
// Create popup HTML
for(var key in results) {
if(results.hasOwnProperty(key)) {
// Language
$('<span class="hd_ext_language_header">' + key + '</span>').appendTo(popup);
// Create entry header
var header = $('<div class="hd_ext_header"></div>').appendTo(popup);
var headerText = $('<a class="hd_ext_header_text">' + word + '</a>').appendTo(header);
headerText.click(function() {
openInNewTab('http://en.wiktionary.org/wiki/' + word);
});
if(results[key].pronunciation != null) {
header.append($('<span class="hd_ext_header_pronunciation">' + results[key].pronunciation + '</span>'));
}
for(var i = 0; i < results[key].entries.length; i++) {
$('<span class="hd_ext_word_class">' + results[key].entries[i].type + '</span>').appendTo(popup);
var list = $('<ol></ol>').appendTo(popup);
for(var j = 0; j < results[key].entries[i].definitions.length; j++) {
/*var def = results[key].entries[i].definitions[j];
var listEntry = $('<li></li>');
if(def.charAt(0) == "(" && def.indexOf(")") > 0) {
listEntry.append($('<span class="hd_ext_glossary_text">' + def.substr(0, def.indexOf(")") + 1) + '</span>'));
def = def.substr(def.indexOf(")") + 1);
}
listEntry.append($('<span class="hd_ext_bullet_list_entry_text">' + def + '</span>'));
list.append(listEntry);*/
list.append(results[key].entries[i].definitions[j]);
}
}
}
}
// Resize and move popup so it is inside the viewport
var position = { top: initialPosition.top, left: initialPosition.left };
position.left = Math.min(position.left + 10, document.body.scrollWidth - POPUP_WIDTH - 40);
position.top = Math.min(position.top + 10, document.body.scrollHeight - POPUP_HEIGHT - 40);
popup.css({"top": Math.round(position.top), "left": Math.round(position.left), "max-width": POPUP_WIDTH, "min-width": POPUP_WIDTH, "max-height": POPUP_HEIGHT, "min-height": POPUP_HEIGHT});
} else {
popup.html($('<span class="hd_ext_language_header">Definition for <i>' + word + '</i> not found</span>'));
}
});
}
function documentKeyDown(event) {
pressedKeys[event.keyCode] = true;
if(pressedKeys[16] && pressedKeys[17]) {
// close existing popup
closePopup();
// Get selected text
var word = getSelectionText();
// If no text is selected, use word under cursor
if(word == "") {
word = getFullWord(mouseClientPosition);
}
// Return the word the cursor is over
if(word != "") {
// Create popup
popup = $('<div id="hoverdict"></div>').appendTo(document.body);
// Get word definition
lookupWord(word, { top:mousePagePosition.top, left:mousePagePosition.left });
// Show popup
popup.stop(true, true).fadeTo(100, 1.0);
}
}
}
function documentKeyUp(event) {
pressedKeys[event.keyCode] = false;
}
function closePopup() {
if(popup != null && !popup[0].contains(event.target)) {
popup.stop(true, true).fadeOut(0,
function() {
// When animation is done, remove the popup
popup.remove();
popup = null;
}
);
}
}
function documentMouseDown(event) {
closePopup();
}
$(document).mousemove(documentMouseMove).keydown(documentKeyDown).keyup(documentKeyUp).mousedown(documentMouseDown);