-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
213 lines (178 loc) · 6.54 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
// this parses the dictionary entries and parses it all
document.addEventListener('DOMContentLoaded', () => {
getDictAndRestoreInDom();
// document.getElementById("search").addEventListener("onChanged", function() {
// searchDict(document.getElementById("search").value)
// })
});
chrome.storage.onChanged.addListener((changes) => {
Object.values(changes).forEach((key) => {
const storageChange = changes[key];
if (storageChange.newValue.length > storageChange.oldValue.length) {
addNewElement();
}
});
});
function addNewElement() {
chrome.storage.local.get({
dictionary: []
}, (data) => {
dictionary = data.dictionary;
const wordList = document.getElementById('dictionary');
const classList = document.getElementsByClassName('collapsible');
const newNode = dictionary[0];
wordList.insertBefore(addWordToDom(newNode), classList[0]);
});
}
function getDictAndRestoreInDom() {
chrome.storage.local.get({ dictionary: [] }, (data) => {
const wordList = document.getElementById('dictionary');
dictionary = data.dictionary;
dictionary.forEach((word) => {
wordList.appendChild(addWordToDom(word));
});
});
}
function addWordToDom(word) {
const newContainer = document.createElement('li');
newContainer.className = 'collapsible';
newContainer.addEventListener('click', () => {
newContainer.classList.toggle('expand');
});
const newEntry = document.createElement('li');
newEntry.className = 'content';
const removeButton = createRemove();
removeButton.addEventListener('click', () => {
removeWordAndSave(word);
removeButton.parentNode.parentNode.remove();
});
newEntry.appendChild(removeButton);
const t = word.definition.results[0].id;
const wordElem = document.createElement('p');
wordElem.className = 'word';
wordElem.innerText = t;
newEntry.appendChild(wordElem);
newEntry.appendChild(document.createElement('br'));
const p = word.definition.results[0].lexicalEntries[0].pronunciations[0].phoneticSpelling;
const phoneticElem = document.createElement('p');
phoneticElem.className = 'phonetic';
phoneticElem.innerText = `/${p}/`;
newEntry.appendChild(phoneticElem);
newEntry.appendChild(document.createElement('br'));
newEntry.appendChild(document.createElement('br'));
const wordInfo = populateNode(word);
newEntry.appendChild(wordInfo);
newContainer.appendChild(newEntry);
return newContainer;
}
function createRemove() {
const removeButton = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
removeButton.setAttribute('viewBox', '0 0 24 24');
removeButton.setAttribute('class', 'remove');
const newLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
newLine.setAttribute('class', 'feather');
newLine.setAttribute('x1', '18');
newLine.setAttribute('y1', '6');
newLine.setAttribute('x2', '6');
newLine.setAttribute('y2', '18');
removeButton.appendChild(newLine);
const newLine2 = document.createElementNS('http://www.w3.org/2000/svg', 'line');
newLine2.setAttribute('class', 'feather');
newLine2.setAttribute('x1', '6');
newLine2.setAttribute('y1', '6');
newLine2.setAttribute('x2', '18');
newLine2.setAttribute('y2', '18');
removeButton.appendChild(newLine2);
return removeButton;
}
function populateNode(word) {
const newEntry = document.createElement('li');
Object.values(word.definition.results[0].lexicalEntries).forEach((key) => {
const typeBox = document.createElement('p');
typeBox.className = 'type';
typeBox.innerHTML = `<i>${key.lexicalCategory}</i>`;
newEntry.appendChild(typeBox);
for (let i = 0; i < key.entries[0].senses.length; i += 1) {
const value = key.entries[0].senses;
if (value[i].definitions) {
const def = document.createElement('p');
def.className = 'definition';
const t2 = document.createTextNode(`${i + 1}. ${value[i].definitions}`);
newEntry.appendChild(t2);
}
if (value[i].crossReferenceMarkers) {
const referenceBox = document.createElement('p');
referenceBox.className = 'reference';
referenceBox.innerHTML = `${i + 1}. ${value[i].crossReferenceMarkers[0]}`;
newEntry.appendChild(referenceBox);
}
if (value[i].examples) {
const exampleBox = document.createElement('p');
exampleBox.className = 'example';
exampleBox.innerHTML = `<i> "${value[i].examples[0].text}"</i>`;
newEntry.appendChild(exampleBox);
}
if (value[i].thesaurusLinks) {
if (word.synonyms) {
Object.values(word.synonyms.results[0].lexicalEntries[0].entries[0].senses)
.forEach((info) => {
if (info.id === value[i].thesaurusLinks[0].sense_id) {
const synonymBox = document.createElement('p');
synonymBox.innerHTML = '<i>synonyms: </i>';
synonymBox.className = 'synonym';
const arr = [];
Object.values(info.synonyms).forEach((syno) => {
arr.push(syno.text);
});
const f = arr.join(', ');
const g = document.createTextNode(f);
synonymBox.appendChild(g);
newEntry.appendChild(synonymBox);
newEntry.appendChild(document.createElement('br'));
}
});
}
}
newEntry.appendChild(document.createElement('br'));
}
newEntry.appendChild(document.createElement('br'));
});
return newEntry;
}
function removeWordAndSave(wordObject) {
const index = getIndex(wordObject);
if (index !== -1) {
dictionary.splice(index, 1);
saveDict();
}
}
function getIndex(word) {
for (let i = 0; i < dictionary.length; i += 1) {
if (dictionary[i].definition.results[0].id === word.definition.results[0].id) {
return i;
}
}
return -1;
}
function saveDict() {
chrome.runtime.sendMessage({ message: dictionary });
}
function searchDict(searchQuery) {
console.log(searchQuery)
}
// function searchDict() {
// chrome.storage.local.get({
// dictionary: []
// }, (data) => {
// dictionary = data.dictionary;
// // compare the strings here
// // do we even need to get the dict from storage?
// // or is the dictionary
// //
// })
// }
// we know that the popup will be open when a search is performed...
// so we have to change the view depending on the search
// so anything that doesn't match the search criteria should be removed from the popup...
// so first we have to search the array, we'll worry about the display later
// search the array