-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontent.js
More file actions
55 lines (50 loc) · 1.77 KB
/
Copy pathcontent.js
File metadata and controls
55 lines (50 loc) · 1.77 KB
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
45
46
47
48
49
50
51
52
53
54
55
// Cache cờ cấu hình để đỡ await liên tục
let enableCtrlC = true;
chrome.storage.local.get(['enableCtrlC']).then(res => {
if (res.enableCtrlC === false) enableCtrlC = false;
});
chrome.storage.onChanged.addListener((changes, area) => {
if (area === 'local' && 'enableCtrlC' in changes) {
enableCtrlC = changes.enableCtrlC.newValue !== false;
}
});
// Lấy text đang chọn, hỗ trợ input/textarea/contentEditable và selection thường
function getCurrentSelectionText() {
const ae = document.activeElement;
// Input/Textarea
if (ae && (ae.tagName === 'INPUT' || ae.tagName === 'TEXTAREA')) {
const start = ae.selectionStart ?? 0;
const end = ae.selectionEnd ?? 0;
if (typeof start === 'number' && typeof end === 'number' && end > start) {
return ae.value.substring(start, end);
}
return '';
}
// ContentEditable hoặc selection trên trang
const sel = window.getSelection();
return sel ? sel.toString() : '';
}
// Bắt mọi hành vi copy trong PAGE (Ctrl+C, menu, v.v.)
document.addEventListener('copy', () => {
if (!enableCtrlC) return;
const text = getCurrentSelectionText();
if (text && text.trim()) {
chrome.runtime.sendMessage({
action: 'saveTempText',
text: text.trim()
});
}
}, true); // capture=true để ưu tiên trước một số lib chặn sự kiện
// Optional: khi Paste trong PAGE thì cũng lưu (hữu ích khi user paste URL đã copy ở chỗ khác)
document.addEventListener('paste', (event) => {
if (!enableCtrlC) return;
const cd = event.clipboardData || window.clipboardData;
if (!cd) return;
const pasted = cd.getData('text');
if (pasted && pasted.trim()) {
chrome.runtime.sendMessage({
action: 'saveTempText',
text: pasted.trim()
});
}
}, true);