-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocabify.js
67 lines (52 loc) · 1.72 KB
/
vocabify.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
$(document).ready(function () {
var dict = 'http://api.urbandictionary.com/v0/define?term=';
/**
*
* @returns {string} selected text on web page
*/
function getSelectedText() {
var text = '';
if(window.getSelection()){
text = window.getSelection().toString();
}
return text;
}
/**
*
* @param selectedText selected phrase on the wen page
* @returns {string} lowercase-d parsed word, ready for definition
*/
function getSelectedWord(selectedText) {
var word = '';
if (selectedText !== 'undefined' && selectedText !== '' && selectedText !== null){
word = selectedText.trim().toLowerCase();
if (word.match(/^[a-z]+$/)){
return word;
}
}
return 'undefined';
}
$(document).click(function () {
var selectedText = getSelectedText();
var word = getSelectedWord(selectedText);
var response = '';
if (word !== 'undefined'){
$.ajax({
url: dict + word,
success: function (data) {
if (data.result_type === 'exact'){
response += word.bold() + '\n\n';
response += data.list[0].definition;
alert(response);
return;
}
alert("404! No meaning found for " + word.bold() + " :(");
},
error: function (err) {
alert("Couldn't connect to online dictionary. :(" + err.status);
},
type: 'GET'
});
}
});
});