-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
29 lines (25 loc) · 906 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
// content.js
let lastSentText = '';
const throttleDuration = 300; // 减少节流时间到300毫秒
function sendSelectedText() {
const selection = window.getSelection();
if (selection) {
const selectedText = selection.toString().trim();
if (selectedText.length > 0 && selectedText !== lastSentText) {
lastSentText = selectedText;
chrome.runtime.sendMessage({
type: 'sendSelectedText',
text: selectedText
});
}
}
}
document.addEventListener('mouseup', () => {
setTimeout(sendSelectedText, 10); // 稍微延迟执行,以确保选择已完成
});
document.addEventListener('keyup', (e) => {
// 检查是否按下了可能影响文本选择的键
if (e.key === 'Shift' || e.key === 'Control' || e.key === 'Meta' || e.key === 'Alt') {
setTimeout(sendSelectedText, 10);
}
});