-
Notifications
You must be signed in to change notification settings - Fork 8
/
background.js
36 lines (30 loc) · 936 Bytes
/
background.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
const title = chrome.runtime.getManifest().name;
chrome.contextMenus.create({
id: title,
title,
contexts: ['selection']
});
chrome.contextMenus.onClicked.addListener((info) => {
const text = info.selectionText;
const words = text.split(/\s+/);
const wordCount = words.length;
const charCount = text.length;
let totalLength = 0;
let maxLength = 0;
for (let i = 0; i < wordCount; i++) {
const curLength = words[i]
.replace(/[.,?!()<>{}[\]/\\+=~'`|:;]/g, '').length;
totalLength += curLength;
if (curLength > maxLength) {
maxLength = curLength;
}
}
const avgLength = wordCount === 0
? 0
: totalLength / wordCount;
const numAverageDigits = 2;
alert(`Word Count: ${wordCount}
Character Count: ${charCount}
Average Word Length: ${avgLength.toFixed(numAverageDigits)}
Longest Word Length: ${maxLength}`);
});