-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
34 lines (27 loc) · 886 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
const debounce = (func, delay) => {
let timeoutId;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => func(...args), delay);
};
};
const sendSelectedText = debounce((selectedText) => {
console.log('Sending message:', selectedText);
if (chrome.runtime && chrome.runtime.sendMessage) {
chrome.runtime.sendMessage({ message: 'getSelectedText', selectedText }, (response) => {
if (chrome.runtime.lastError) {
console.error('Error in sendMessage:', chrome.runtime.lastError);
} else {
console.log('Response received:', response);
}
});
} else {
console.error('Chrome runtime not available');
}
}, 300);
document.addEventListener('mouseup', () => {
const selectedText = window.getSelection().toString().trim();
if (selectedText.length > 0) {
sendSelectedText(selectedText);
}
});