-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
345 lines (246 loc) · 10.6 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
let highlightedText = ""
let background = chrome.extension.getBackgroundPage();
// imageNumber specifies which image to show when you get image
let imageNumber = 0;
// all definitions variables. make this here to use later
let fullDefinitions = "";
let shortDefinitions = "";
let definitionArray = [];
// localize the extension UI based on system language
function localize() {
//supporting spanish as a second language
document.getElementById("detectedLanguage").innerHTML = chrome.i18n.getMessage("detectedLanguage") + ' <span id="detectedText">None</span>'
const detectedT = chrome.i18n.getMessage("detectedText")
document.getElementById("detectedText").innerHTML = detectedT
const definitionB = chrome.i18n.getMessage("definitionBtn")
document.getElementById("definitionBtn").innerHTML = definitionB
const synonymB = chrome.i18n.getMessage("synonymsBtn")
document.getElementById("synonymsBtn").innerHTML = synonymB
const exampleB = chrome.i18n.getMessage("examplesBtn")
document.getElementById("examplesBtn").innerHTML = exampleB
const imageB = chrome.i18n.getMessage("imageBtn")
document.getElementById("imageBtn").innerHTML = imageB
const noDefinition = chrome.i18n.getMessage("no_definition")
document.getElementById("en_definition").innerHTML = noDefinition
const moreB = chrome.i18n.getMessage("moreBtn")
document.getElementById("moreDefinition").innerHTML = moreB
const nextImage = chrome.i18n.getMessage("newImage")
document.getElementById("newImage").innerHTML = nextImage
}
// translateInput compiles all the initial commands for easier calling through events later
async function getTranslation(originalText, L1_code){
var translation = new Translation;
if (originalText.length == 0) {
return ;
}
let translatedText = await translation.translateText(originalText, L1_code);
let detectedLanguage = await translation.detectLanguage(originalText, L1_code);
// Get the element by id (of popup.html) and change that to the translated text.
// The span element in the middle is the HTML code for the double arrow.
document.getElementById('translatedText').innerHTML = originalText + " <span>»</span> " + translatedText;
// only shows the language if it's a valid input, otherwise show "None"
if (typeof(detectedLanguage) != 'undefined' && !Meta.removeSpecialCharacters(originalText) == "") {
// Get the element by id (of popup.html) and change that to the detected source language.
detectedLanguageName = isoLangs[detectedLanguage].name;
if (detectedLanguageName == 'English') {
document.getElementById('detectedText').textContent = chrome.i18n.getMessage("englishLanguage");
} else {
document.getElementById('detectedText').textContent = detectedLanguageName;
}
}
}
//function that populates the different text tabs in order
async function populateTextContent(text) {
definitionArray = await Meta.getShortDefinition(text);
let definitionHTML = document.getElementById('en_definition');
if(definitionArray && definitionArray[0]) {
//get definition of input text
shortDefinitions = definitionArray[0];
}
//if definition is returned. Element 0 is the full definition string. 1 is the first definition for saving
if (shortDefinitions && shortDefinitions != "") {
definitionHTML.innerHTML = shortDefinitions;
}
// get all definitions for when you click on more button
const allDefinitions = await Meta.getAllDefinitions(text);
if (allDefinitions) {
fullDefinitions = allDefinitions;
}
let filteredDefs = [Meta.removeSpecialCharacters(shortDefinitions), Meta.removeSpecialCharacters(allDefinitions)];
// if both long and short def are not the same, or if there's definition, show more btn
if (filteredDefs[0].length != filteredDefs[1].length
&& !definitionHTML.innerHTML != "No definition available"
&& shortDefinitions != null) {
document.getElementById("moreDefinition").style.display = 'inline-block';
}
// checks if alphanum, implemented to grey out all buttons
if (!(text.match("^[a-zA-Z0-9]*$"))){
return;
}
//get synonyms for input text
const synonyms = await Meta.getSynonyms(text);
//if synonyms are returned:
if (synonyms) {
//fill in content
document.getElementById('en_synonym').innerHTML = synonyms;
//make button clickable
document.getElementById('synonymsBtn').disabled = false;
}
//get usage examples for input text
const examples = await Meta.getUsageExamples(text);
//if examples are returned:
if (examples) {
//fill in content
document.getElementById('usageExamples').innerHTML = examples;
//make button clickable
document.getElementById('examplesBtn').disabled = false;
}
}
async function populateImages(text, imageNumber) {
// display and load image button depending on the browseractions toggles
background.getChrome().storage.sync.get(background.featuresState, async function(items) {
// dont display and load image button if setting is set to false
if (background.featuresState.includePictures == false){
document.getElementById("imageBtn").style.display = "none";
// if true, get the images.
} else {
// image is stored in an array, element 0 = URL, element 1 = photo credit, element 3 = number of photos returned
const resultArray = await Meta.getImage(text, imageNumber);
const imageURL = resultArray[0], photoCredit = resultArray[1], numberOfImages = resultArray[3];
// if only one image, hide "Next Image" button
if (numberOfImages <= 1) {
document.getElementById("newImage").display = none
}
//if an image is returned:
if (imageURL) {
//set the searchedImage element to returned image
var image = document.getElementById("searchedImage");
image.src = imageURL;
//make it visible
image.style.display = 'block';
//show photo credit
document.getElementById("photoCredit").innerHTML = "@" + photoCredit + " - pixabay.com";
//make button clickable
document.getElementById('imageBtn').disabled = false;
}
}
});
}
function displayGeneratedContent(selection) {
// valid queries only, fix property by 0 error.
if (typeof(selection) == "undefined" || selection.length == 0) {
return;
}
//selection is an array. value 0 is the highlighted portion, value 1 is the string length.
highlightedText = String(selection).trim();
chrome.storage.sync.get({L1_code : 'es'}, function(result){
getTranslation(highlightedText, result.L1_code);
});
//after displaying the text, if it's some wacky characters, return and turn off more button.
if (Meta.removeSpecialCharacters(selection) == "") {
document.getElementById("moreDefinition").style.display = 'none'
return;
}
populateTextContent(highlightedText);
// default image number = 0, show the first one first, then show others later.
populateImages(highlightedText, imageNumber);
chrome.storage.sync.get('userSavedWords', function(data){
const wordDict = data.userSavedWords;
let wordToSave = highlightedText.toLowerCase();
wordToSave.replace(/[^0-9a-z]/gi, '')
// add class "saved" if the word is saved already
if (wordDict && wordDict[wordToSave]) {
document.getElementById("saveButton").className += "saved";
}
});
}
// get access to background page, assign the page handler to bg
var bg = chrome.extension.getBackgroundPage();
bg.getChrome().storage.sync.get(bg.featuresState, function(items) {
const themeStylesheet = document.getElementById('popup-theme');
if (items["dark"] == true) {
themeStylesheet.href = 'popup-dark.css';
}
else {
themeStylesheet.href = 'popup.css';
}
});
// apply the functions to the buttons
onload = async function() {
var speechSynth = window.speechSynthesis;
localize();
const buttons = ["definitionBtn", "synonymsBtn", "examplesBtn", "imageBtn"];
const boxes = ["definitionBox", "synonymsBox", "examplesBox", "imageBox"];
// let is used here as a block-scoped variable
for (let i = 0; i < buttons.length; i++) {
document.getElementById(buttons[i]).addEventListener("click",
function() {
// switch to correct tab
Utility.changeTab(boxes[i], boxes);
Utility.makeOnlyActiveElement(buttons[i], buttons);
}, true);
}
// event listener for clicking on Save button
document.getElementById("saveButton").addEventListener("click",
function() {
let wordToSave = ""
wordToSave = highlightedText.trim().toLowerCase().replace(/[^A-Za-z0-9]/g, '');
// change button style after being clicked. If word is saved, turn to unsave mode.
if (this.className == "saved") {
Saving.deleteSavedWord(wordToSave);
var current = document.getElementsByClassName("saved");
current[0].className = current[0].className.replace("saved", "");
// fill in the star
} else {
// don't save if the word is not definable/doesn't make sense
if (typeof(definitionArray[0]) == undefined || definitionArray[0] == null) {
console.log('Cannot save a non-word');
return;
}
Saving.saveWordInfo(wordToSave);
this.className += "saved";
}
}, true);
// event listener for clicking on Pronounce button
document.getElementById("pronounceWord").addEventListener("click",
function() {
if (highlightedText.length > 0) {
Meta.pronounceWord(speechSynth, highlightedText);
}
}, true);
document.getElementById("newImage").addEventListener("click",
function() {
// imageNumber specifies which image to show. Will loop back to the
// beginning if exceeds total number of imgs.
imageNumber += 1
populateImages(highlightedText, imageNumber)
}, true);
//add the event listener for clicking to enlarge the image. not sure why but only works correctly if put here.
SyntacsImage.imageEventListener(document.getElementById("searchedImage"));
const moreButton = document.getElementById("moreDefinition");
//bool to keep track of the state of the button
let moreIsOn = false;
moreButton.addEventListener("click", function() {
moreIsOn = !moreIsOn;
// get original coordinate of the scroll view
let yCoordinate = window.pageYOffset;
if(moreIsOn){
// when press more, show full unfiltered definitions
document.getElementById('en_definition').innerHTML = fullDefinitions;
//change more to less
this.innerText = "less";
// scroll to original view
scroll(0, yCoordinate);
} else {
// when press less, show short definitions
document.getElementById('en_definition').innerHTML = shortDefinitions;
//change less to more
this.innerText = "more";
// scroll to original view
scroll(0, yCoordinate);
}
});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {getText: true}, displayGeneratedContent);
});
}