-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontent.js
34 lines (31 loc) · 864 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
29
30
31
32
33
34
window.addEventListener('load', autoScan);
window.addEventListener('mouseup', wordSelected);
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.msg == "newTabisReadyToBeScanned"){
autoScan();
}
});
function wordSelected() {
let selectedText = window.getSelection().toString();
if (selectedText.length > 0) {
let message = {
text: selectedText,
};
chrome.runtime.sendMessage(message);
}
}
function autoScan() {
let message = {
text: getWholeWebPageText(),
};
chrome.runtime.sendMessage(message);
}
const getWholeWebPageText = function() {
let wholePageText = '';
document.querySelectorAll('*').forEach(value => {
if (value.textContent.length > 0 && value.textContent.length < 100) {
wholePageText += value.textContent;
}
});
return wholePageText;
};