-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
32 lines (31 loc) · 1.11 KB
/
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
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.keyCode === 53) { // 53 is the key code for '5'
const selection = window.getSelection();
const selectedText = selection.toString().trim();
if (selectedText.length > 0) {
saveHighlight(selectedText, 'text');
}
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
const container = range.commonAncestorContainer;
container.querySelectorAll('img').forEach(img => {
if (selection.containsNode(img, true)) {
saveHighlight(img.src, 'image');
}
});
}
}
});
function saveHighlight(content, type) {
const highlight = {
type: type,
content: content,
url: window.location.href,
timestamp: new Date().toISOString()
};
chrome.storage.local.get({highlights: []}, function(result) {
const highlights = result.highlights;
highlights.push(highlight);
chrome.storage.local.set({highlights: highlights});
});
}