-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
155 lines (116 loc) · 4.12 KB
/
popup.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
var definition='hmm';
var pageText = 'hi';
chrome.storage.local.get(['content'], function(items) {
//need to replace all the <> tags from the text and replace all the next line characters of all types
var content = items['content'].replace(/<[^>]+>/g, '').replace(/(\r\n|\n|\r)/gm,"");
superImportantText = content;
pageText = content;
// this function will initate the api call and other callback functions
start();
});
function selectRandomWord ( words ) {
//selecting a random word from the words array , using Math.floor to get an integer
randomIndex = Math.floor((Math.random()*1000)%words.length) ;
console.log(words);
console.log(randomIndex);
console.log(words[randomIndex]);
return words[randomIndex];
}
function start(){
//split words by space
words = pageText.split(" ") ;
word='-'
//using regex to check if the current word only consists of alphabets
var letters = /^[A-Za-z]+$/
console.log(word);
while(word.length<=2 || word.match(/^[A-Za-z]+$/)==null ){
word = selectRandomWord(words);
console.log(word);
}
//setting the global word selected
chrome.storage.local.set({'word_selected': word }, function() {
console.log('word has been Selected');
});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id,{Message: "word has been selected"}, function (response) {
// this is to trigger the chrome api to content.js;
})
}) ;
console.log('beforeAPICALL')
//the api call
$.ajax({
url: 'https://owlbot.info/api/v4/dictionary/'+word,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Token e4df540ec3454d5faa14ccf851905a339615dbb3")
}, success: function(data){
if(data['definitions']===undefined)
{
start();
return;
}
definition = data['definitions'][0]['definition'];
console.log(definition);
setDefinition();
},error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Status: " + textStatus); console.log("Error: " + errorThrown);
start();
}
})
console.log('afterAPIcALL')
}
function setDefinition(){
document.getElementById('defitnition').innerHTML = 'Definition : <br>' + definition;
}
var distance = document.getElementById('Guess') ;
distance.addEventListener("keyup", calcDist);
//driver function to calculate edit distance
function calcDist(){
userInput = distance.value ;
var editDist = levenshteinDist(userInput.toLowerCase() , word.toLowerCase() , 0, 0)
document.getElementById('levisteinDist').innerHTML = 'You are distance away : <b>'+editDist+'</b>' ;
if(editDist==0){
alert('CORRECT');
distance.value='';
chrome.storage.local.set({'word_selected': 'F' }, function()
{
console.log('word has reset');
});
//after correct guess close and reload window
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id,{Message: "word has been de-selected"}, function (response) {
;
})
}) ;
window.close();
}
}
function levenshteinDist(str1,str2){
//dp array to cache values
dp = new Array(str1.length);
for (var i = 0; i < str1.length; i++) {
dp[i]=new Array(str2.length);
for (var j = 0; j < str2.length; j++) {
dp[i][j]=-1;
}
}
return levenshteinDistHelper(str1,str2,0,0);
}
//edit distance helper function , checked from leetcode too for complete authenticity
function levenshteinDistHelper(str1,str2,i,j){
if(i==str1.length){
return str2.length - j ;
}else if(j==str2.length){
return str1.length - i ;
}
console.log(str1+ ' '+ str2 + ' '+i+ ' '+j)
if(dp[i][j]!=-1)
return dp[i][j];
var op1 = Number.MAX_SAFE_INTEGER , op2 = Number.MAX_SAFE_INTEGER , op3 = Number.MAX_SAFE_INTEGER , op4 = Number.MAX_SAFE_INTEGER ;
if(str1[i]==str2[j])
op1 = levenshteinDistHelper(str1,str2,i+1,j+1);
op2 = 1 + levenshteinDistHelper(str1,str2,i+1,j);
op3 = 1 + levenshteinDistHelper(str1,str2,i,j+1);
op4 = 1 + levenshteinDistHelper(str1,str2,i+1,j+1);
dp[i][j] = Math.min(op1,Math.min(op2,Math.min(op3,op4))) ;
return dp[i][j];
}