forked from valteros1/wordcounter-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
218 lines (171 loc) · 10.1 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
document.addEventListener('DOMContentLoaded', function() {
chrome.runtime.sendMessage({ type: 'requestData' }, function(response) {
console.log('Message sent from popup.js to background.js');
console.log(response);
hideAll()
document.getElementById('text-mistakes-button').style.backgroundColor="#fbcdfc";
document.getElementById('corrected-area').style.display = 'block';
// If there are mistakes, hide the no-mistakes element and show the corrected-words element
if (response && response.sentText && response.recievedWords && response.sentText === response.recievedWords) {
// document.getElementById('no-mistakes').style.display = 'block';
// document.getElementById('corrected-words').style.display = 'block';
let test = 'Vigu ei leitud.';
document.getElementById('corrected-words').innerHTML = test;
// chrome.extension.getBackgroundPage().console.log('Teade, mida soovite kuvada');
} else if (response && response.recievedWords) {
console.log("SEE ON ", response.recievedWords)
const correctedWords = response.recievedWords.split(' ');
const sentText = response.sentText.split(' ');
let highlightedText = '';
for (let i = 0; i < correctedWords.length; i++) {
if (correctedWords[i] !== sentText[i]) {
highlightedText += `<span class="stripes">${sentText[i]}</span> <span style="background-color: #AEF359"> ${correctedWords[i]}</span> `;
} else {
highlightedText += `${correctedWords[i]} `;
}
}
// Display the highlighted text in the corrected-words element and hide the no-mistakes element
document.getElementById('corrected-words').innerHTML = highlightedText;
// document.getElementById('no-mistakes').style.display = 'none';
// document.getElementById('corrected-words').style.display = 'none'; // hide the corrected-words element initially
} else {
let test = 'Tekst pole valitud';
// If there is no response, hide the corrected-words element and show the no-mistakes element
document.getElementById('corrected-words').innerHTML = test;
document.getElementById('corrected-words').style.display = 'block';
// document.getElementById('no-mistakes').style.display = 'block';
}
});
// Add a click event listener to the Mistakes button that shows the corrected-words element
const mistakesButton = document.getElementById('text-mistakes-button');
mistakesButton.addEventListener('click', () => {
hideAll();
document.getElementById('corrected-area').style.display = 'block';
document.getElementById('text-mistakes-button').style.backgroundColor="#fbcdfc";
// const correctedWordsEl = document.getElementById('corrected-words');
// const noMistakesEl = document.getElementById('no-mistakes');
// if (correctedWordsEl.style.display === 'none') {
// correctedWordsEl.style.display = 'block';
// noMistakesEl.style.display = 'none';
// } else {
// correctedWordsEl.style.display = 'none';
// noMistakesEl.style.display = 'block';
// }
});
const closeButton = document.getElementById('close-button');
closeButton.addEventListener('click', () => {
chrome.storage.local.clear();
// hideAll();
window.close();
// window.getSelection().removeAllRanges();
});
// const assassmentButton = document.getElementById('level-assessment-button');
// assassmentButton.addEventListener('click', () => {
// window.close();
// });
});
document.getElementById('level-assessment-button').addEventListener('click', function() {
chrome.storage.local.get(['langLevelFirst', 'langLevelSecond', 'langLevelThird', 'langLevelFourth', 'langComplexityFirst'
, 'langComplexitySecond', 'langComplexityThird', 'langComplexityFourth', 'langvocabularyFirst', 'langformUsageFirst'], function(result) {
hideAll();
document.getElementById('assessment-area').style.display = 'block';
document.getElementById('level-assessment-button').style.backgroundColor="#fbcdfc";
{/* <h2 style="display: inline-block; margin-bottom: 0;">Tekst vastab tasemele:</h2> */}
const probabilityFirst = result.langLevelFirst.toString().split(',');
// const splittedProbabilityFirst = probabilityFirst.split(',');
const probabilitySecond = result.langLevelSecond.toString().split(",");
const probabilityThird = result.langLevelThird.toString().split(",");
const probabilityFourth = result.langLevelFourth.toString().split(",");
const complexityFirst = result.langComplexityFirst.toString().split(",");
const vocabularyFirst = result.langvocabularyFirst.toString().split(",");
const formUsageFirst = result.langformUsageFirst.toString().split(",");
// const complexitySecond = result.langComplexitySecond.toString().split(",");
// const complexityThird = result.langComplexityThird.toString().split(",");
// const complexityFourth = result.langComplexityFourth.toString().split(",");
// const assessmentProbability = probabilityFirst[0];
const assessmentProbability = `
</br>
<h1 style="display: inline-block; margin-top: 0;"><p id="tase" style="display: inline;">${probabilityFirst[1]}</p></h1>
<h1 style="display: inline-block; margin: 0;"> <span id="tasemeprotsent">${Math.round(probabilityFirst[0] * 100)}%</span></h1>
<h3>Teiste tasemete tõenäosus:</h3>
<ul>
<li><b>${probabilitySecond[1]}</b>: ${Math.round(probabilitySecond[0] * 100)}%</li>
<li><b>${probabilityThird[1]}</b>: ${Math.round(probabilityThird[0] * 100)}%</li>
<li><b>${probabilityFourth[1]}</b>: ${Math.round(probabilityFourth[0] * 100)}%</li>
</ul>
</br>
<h3>Täpsem hinnang:</h3>
<ul>
<li><b>Teksti keerukus:</b> ${complexityFirst[1]}</li>
<li><b>Sõnavara:</b> ${vocabularyFirst[1]}</li>
<li><b>Vormikasutus:</b> ${formUsageFirst[1]}</li>
</ul>
`;
;
document.getElementById('assessment-details').innerHTML = assessmentProbability;
})
});
let complexityButton = document.getElementById('text-complexity-button');
document.getElementById('text-complexity-button').addEventListener('click', function() {
chrome.storage.local.get(['wordCount', 'sentencesCount','longSyllables','syllablesCount','longWords','smog',
'fkIndex','lix','sentenceLevel'], function(result) {
const wordCount = result.wordCount
const sentencesCount = result.sentencesCount
const longSyllables = result.longSyllables
const syllablesCount = result.syllablesCount
const longWords = result.longWords
const smog = result.smog
const fkIndex = result.fkIndex
const lix = result.lix
const sentenceLevel = result.sentenceLevel
// Kuna komadega tulevad SMOG ja FK Index, ümardan neid
const roundedSmog = Math.floor(smog * 100) / 100;
const roundedfkIndex = Math.floor(fkIndex * 100) / 100;
hideAll();
document.getElementById('complexity-area').style.display = 'block';
document.getElementById('text-complexity-button').style.backgroundColor="#fbcdfc";
// Keerukuse
document.getElementById('complexity').innerHTML = "Keerukuse andmed" ;
document.getElementById('sentences').innerHTML = '<b>Lauseid:</b> ' + sentencesCount;
document.getElementById('words').innerHTML = '<b>Sõnu:</b> ' + wordCount;
document.getElementById('multisyllables').innerHTML = '<b>Paljusilbilisi sõnu (vähemalt 3 silpi):</b> ' + longSyllables;
document.getElementById('syllables').innerHTML = '<b>Silpe:</b> ' + syllablesCount;
document.getElementById('longwords').innerHTML = '<b>Pikki sõnu (vähemalt 7 tähte):</b> ' + longWords ;
document.getElementById('smog').innerHTML = '<b>SMOG:</b> ' + roundedSmog;
document.getElementById('fkindex').innerHTML = '<b>Flesch-Kincaidi Indeks:</b> ' + roundedfkIndex;
document.getElementById('lix').innerHTML = '<b>LIX:</b> ' + lix;
document.getElementById('sentenceslevel').innerHTML = '<b>Pakutav keerukustase:</b> ' + sentenceLevel;
});
});
let diversityButton = document.getElementById('text-diversity-button');
document.getElementById('text-diversity-button').addEventListener('click', function() {
chrome.storage.local.get(['howManyWords', 'differentWords', 'klss', 'jlss', 'mtld', 'hdd'], function(result) {
const howManyWords = result.howManyWords
const differentWords = result.differentWords
const klss = result.klss
const jlss = result.jlss
const mtld = result.mtld
const hdd = result.hdd
hideAll();
document.getElementById('diversity-area').style.display = 'block';
document.getElementById('text-diversity-button').style.backgroundColor="#fbcdfc";
// Mitmekesisus
document.getElementById('diversity').innerHTML = "Sõnavara mitmekesisuse andmed" ;
document.getElementById('diversitysentence').innerHTML = '<b>Sõnu:</b> ' + howManyWords;
document.getElementById('differentwords').innerHTML = '<b>Erinevaid sõnu:</b> ' + differentWords;
document.getElementById('klss').innerHTML = '<b>Kõigi ja erinevate sõnade korrigeeritud suhtarv <span style="font-style: italic;">(ingl Corrected Type-Token Ratio)</span>:</b> ' + klss;
document.getElementById('jlss').innerHTML = '<b>Kõigi ja erinevate sõnade juuritud suhtarv <span style="font-style: italic;">(ingl Root Type-Token Ratio)</span>:</b> ' + jlss;
document.getElementById('mtld').innerHTML = '<b>MTLD indeks <span style="font-style: italic;">(ingl Measure of Textual Lexical Diversity)</span>:</b> ' + mtld;
document.getElementById('hdd').innerHTML = '<b>HDD indeks <span style="font-style: italic;">(ingl Hypergeometric Distribution D)</span>:</b> ' + hdd;
});
});
function hideAll(){
document.getElementById('complexity-area').style.display = 'none';
document.getElementById('assessment-area').style.display = 'none';
document.getElementById('diversity-area').style.display = 'none';
document.getElementById('corrected-area').style.display = 'none';
document.getElementById('text-complexity-button').style.backgroundColor="aliceblue";
document.getElementById('text-mistakes-button').style.backgroundColor="aliceblue";
document.getElementById('level-assessment-button').style.backgroundColor="aliceblue";
document.getElementById('text-diversity-button').style.backgroundColor="aliceblue";
}