-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
82 lines (74 loc) · 1.85 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
/**
* Get the current URL.
*
* @param {function(string)} callback - called when the URL of the current tab
* is found.
*/
function getCurrentTabUrl(callback) {
var queryInfo = {
active: true,
currentWindow: true,
};
chrome.tabs.query(queryInfo, function (tabs) {
var tab = tabs[0];
var url = tab.url;
console.assert(typeof url == "string", "tab.url should be a string");
callback(url);
});
}
function getNumberOfSyllables(text) {
var n = 0;
var arr = text.split(" ");
for (var i = arr.length - 1; i >= 0; i--) {
var word = arr[i];
word = word.toLowerCase();
if (word.length <= 3) {
n += 1;
continue;
}
word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, "");
word = word.replace(/^y/, "");
n += word.match(/[aeiouy]{1,2}/g).length;
}
return n;
}
function renderText(text) {
var w = text.split(/[^\s]+/).length - 1;
var st = text.split(/[^.!?]+/).length - 1;
var sy = getNumberOfSyllables(text);
var fr = 206.835 - 1.015 * (w / st) - 84.6 * (sy / w);
var readability;
if (fr <= 30) {
readability = "Difficult";
} else if (fr >= 90) {
readability = "Easy";
} else {
readability = "Moderate";
}
var finalText =
"Number of words: " +
w +
"\n" +
"Number of sentences: " +
st +
"\n" +
"Flesch readability score: " +
readability +
" ";
document.getElementById("text").textContent = finalText;
}
document.addEventListener("DOMContentLoaded", function () {
getCurrentTabUrl(function (url) {
chrome.tabs.executeScript(
{
code: "window.getSelection().toString();",
},
function (selection) {
if (!selection || selection == "") {
document.getElementById("text").textContent =
"Please select your text!";
} else renderText(selection[0]);
}
);
});
});