-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
28 lines (24 loc) · 831 Bytes
/
content.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
const UPDATE_BATCH_TIME = 500;
var updateHandle = null;
function updateCount() {
var wordCount = document.body.innerText.split(' ').length;
chrome.runtime.sendMessage({ "wordCount": wordCount });
updateHandle = null;
}
// Update on page load
updateCount();
// Register handler to update on page mutations
let observer = new MutationObserver(mutations => {
// schedule new update if page has changed and no update is already scheduled
if (updateHandle === null) {
updateHandle = setTimeout(updateCount, UPDATE_BATCH_TIME);
}
});
observer.observe(document, { childList: true, subtree: true });
// Handle message for recounting
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.type === "recount") {
updateCount();
}
sendResponse();
});