-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
270 lines (237 loc) · 9.03 KB
/
script.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
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
class WordMeaning{
constructor(){
this.popup = null;
this.speller = new Audio();
this.lang = 'en';
this.openState = false;
this.startMovement = false;
this.synth = window.speechSynthesis;
this.selectedText = null;
this.spellerExist = false;
}
async getSelectionText(){
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
console.log(window.getSelection())
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
this.selectedText = text;
return text;
}
async emptyResults(e, data){
if(e.type === "mouseup"){
this.popup = document.createElement('div');
this.popup.className = 'bn-popup';
var attr = document.createAttribute('style');
attr.value = `top: ${e.pageY + 10}px; left: ${e.pageX}px`;
this.popup.setAttributeNode(attr);
const noResults = await this.getEmptyResults();
this.popup.innerHTML = `<span id="bn-close">X</span>${noResults}`;
document.body.appendChild(this.popup);
document.getElementById('bn-close').addEventListener('click', async (e)=>{
e.stopPropagation();
await this.removePopup();
});
this.openState = true;
}
}
async createPopup(e, data){
try{
if(e.type === "mouseup"){
this.popup = document.createElement('div');
this.popup.className = 'bn-popup';
var attr = document.createAttribute('style');
attr.value = `top: ${e.pageY + 10}px; left: ${e.pageX}px`;
this.popup.setAttributeNode(attr);
const header = await this.getHeader(data);
const main = await this.getMain(data);
const footer = await this.getFooter(data);
this.popup.innerHTML = `<span id="bn-close">X</span><div id="bn-popup-drag">
<h3>Definitions of <span class="word">${data.word}</span></h3></div><div id="scrollable">${header} ${main}</div> ${footer}`;
document.body.appendChild(this.popup);
document.getElementById('spell-it').addEventListener('click', async (e)=>{
e.stopPropagation();
await this.play();
});
document.getElementById('bn-close').addEventListener('click', async (e)=>{
e.stopPropagation();
await this.removePopup();
});
this.move();
this.openState = true;
document.body.style.userSelect = 'none !important';
}
}
catch(err){
throw err;
}
}
async play(){
if(this.spellerExist){
this.speller.play();
}
else{
var utterThis = new SpeechSynthesisUtterance(this.selectedText);
utterThis.lang = this.lang;
this.synth.speak(utterThis);
}
}
async getHeader(data){
this.spellerExist = data.phonetics[0].audio?true:false;
if(this.spellerExist)
this.speller.src = data.phonetics[0].audio;
let volumeIcon = chrome.runtime.getURL("images/volume.svg");
const template = `
<header class="d-flex flex-row space-between">
<div style="margin-bottom: 0.5rem !important;">
<h3 style="font-weight: bold !important;">Meanings</h3>
<small>Source: Google Dictionary</small><br>
</div>
<div>
<span>${this.spellerExist?data.phonetics[0].text:'Spell It'}</span>
<img id="spell-it" src=${volumeIcon} width="18" style="vertical-align: sub;filter: contrast(0.4);"/>
</div>
</header>
`;
return template;
}
async getMain(data){
const template = `
<div class="main">
${
data.meanings.map((item)=>{
return `
<div>
<h3 class="partOfSpeech">${item.partOfSpeech}</h3>
${
item.definitions.map((def, index)=>{
return `
<div class="definitions d-flex flex-row" style="margin-top:0.3rem !important;">
<div class="number-container">
<span class="number">${index + 1}</span>
</div>
<div class="content">
<div class="def">
${def.definition}
</div>
<span class="eg">${def.example?def.example:''}</span>
<br>
<h3 style="font-weight: bold !important; margin: 0.5rem 0 !important;">${def.synonyms?'Synonyms:':''}</h3>
<div class="synonyms">
${
def.synonyms?
def.synonyms.map((synonym) => {return `<span>${synonym}</span>`}).join('')
:''
}
</div>
</div>
</div>
`
}).join("")
}
</div>
`
}).join("")
}
</div>
`;
return template
}
async getFooter(){
const template = `
<footer id="footer">
<small>Developed by Nidhal Bettaibi</small>
</footer>
`;
return template;
}
async getEmptyResults(){
let searchIcon = chrome.runtime.getURL("images/no-result.svg");
const template = `
<div class="empty-popup-result">
<img id="no-res" src=${searchIcon}>
<h4 style="margin-bottom:1rem !important; font-weight: 600 !important">No Definitions Found</h4>
<small class="message">Sorry pal, we couldn't find definitions for the word you were looking for.</small>
</div>
`;
return template;
}
async getGoogleDefinition(word){
try{
const response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/${this.lang}/${word}`);
const data = await response.json();
console.log(data)
return data;
}
catch(err){
throw err;
}
}
async removePopup(){
this.openState = false;
this.speller = new Audio();
this.popup.remove();
this.popup = null;
this.spellerExist = false;
}
set setLang(lang){
this.lang = lang;
}
move(){
const popupHeader = document.getElementById('bn-popup-drag');
popupHeader.onmousedown = (event)=>{
event.stopPropagation();
this.startMovement = true;
}
popupHeader.onmouseup = (event) =>{
event.stopPropagation();
this.startMovement = false;
}
}
updatePosition(x, y){
this.popup.style.left = x +'px';
this.popup.style.top = y + 'px';
}
async getData(event, text){
let googleSource = await this.getGoogleDefinition(text);
if((googleSource instanceof Array) && !this.openState)
await this.createPopup(event, googleSource[0]);
else if(!(googleSource instanceof Array) && !this.openState)
await this.emptyResults(event, googleSource);
else return;
}
}
const wordMeaning = new WordMeaning();
wordMeaning.setLang = document.documentElement.lang;
document.onmouseup = document.onkeyup = async (e)=> {
try{
e.stopPropagation();
wordMeaning.startMovement = false;
if(!wordMeaning.openState){
let text = await wordMeaning.getSelectionText();
text = text.trim();
let trimed = text.match(/\s/gi);
let matches = text.match( /[0-9]|[@,_,/,\,?,*,-,+,=,(,),{,},#,.]/gi );
let test = text && trimed == null && matches == null ;
if(test){
await wordMeaning.getData(e, text.trim());
}
else return;
}
}
catch(err){
throw err;
}
};
document.onmousemove = (event)=>{
try{
if(wordMeaning.openState && wordMeaning.startMovement){
wordMeaning.updatePosition(event.pageX, event.pageY);
}
}
catch(err){
throw err;
}
};