-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
44 lines (40 loc) · 1.68 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
document.addEventListener('DOMContentLoaded', () => {
chrome.storage.sync.get(['wordList', 'caseSensitive'], function(result) {
const wordList = result.wordList || [];
const caseSensitive = result.caseSensitive || false;
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.scripting.executeScript(
{
target: { tabId: tabs[0].id },
function: countWordsOnPage,
args: [wordList, caseSensitive]
},
(results) => {
const wordListElement = document.getElementById('wordList');
wordListElement.innerHTML = '';
if (chrome.runtime.lastError || !results || !results[0]) {
const errorMessage = 'Cannot run on this webpage';
const li = document.createElement('li');
li.textContent = errorMessage;
wordListElement.appendChild(li);
console.error('Script execution failed: ', chrome.runtime.lastError ? chrome.runtime.lastError.message : 'No results returned');
return;
}
const counts = results[0].result;
wordList.forEach((word, index) => {
const li = document.createElement('li');
li.textContent = `${word}: ${counts[index]}`;
wordListElement.appendChild(li);
});
}
);
});
});
});
function countWordsOnPage(wordList, caseSensitive) {
const bodyText = document.body.innerText;
return wordList.map(word => {
const regex = new RegExp(`\\b${word}\\b`, caseSensitive ? 'g' : 'gi');
return (bodyText.match(regex) || []).length;
});
}