-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
97 lines (89 loc) · 2.97 KB
/
content.js
File metadata and controls
97 lines (89 loc) · 2.97 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
let logoDiv = null;
document.addEventListener('mouseup', (event) => {
const selectedText = window.getSelection().toString().trim();
if (selectedText.length > 0) {
showLogo(event.pageX, event.pageY);
} else {
hideLogo();
}
});
document.addEventListener('mousedown', (event) => {
if (logoDiv && !logoDiv.contains(event.target)) {
hideLogo();
}
});
function showLogo(x, y) {
if (!logoDiv) {
logoDiv = document.createElement('div');
logoDiv.style.position = 'absolute';
logoDiv.style.zIndex = '99999';
logoDiv.style.cursor = 'pointer';
logoDiv.innerHTML = `<img src="${chrome.runtime.getURL('icons/icon48.png')}" style="width: 28px; height: 28px;">`;
logoDiv.style.animation = 'jelly 0.5s ease';
const style = document.createElement('style');
style.textContent = `
@keyframes jelly {
0% {
transform: scale(0);
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(1);
}
}
`;
document.head.appendChild(style);
logoDiv.addEventListener('click', (e) => {
e.stopPropagation(); // Prevent document.mousedown from closing immediately
const selectedText = window.getSelection().toString().trim();
if (selectedText.length > 0) {
if (chrome.runtime?.id) {
chrome.runtime.sendMessage({
action: "openSaveWindow",
text: selectedText,
x: x,
y: y
}, (response) => {
let lastError = chrome.runtime.lastError
if (lastError) {
console.log(`Message send error: ${JSON.stringify(lastError)}`);
}
hideLogo();
});
} else {
console.error('Extension context invalidated, cannot send message.');
hideLogo();
}
}
});
document.body.appendChild(logoDiv);
}
const logoWidth = 16;
const logoHeight = 16;
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
let left = x + 5;
let top = y + 5;
if (left + logoWidth > windowWidth) {
left = windowWidth - logoWidth - 5;
}
if (top + logoHeight > windowHeight) {
top = windowHeight - logoHeight - 5;
}
logoDiv.style.left = `${left}px`;
logoDiv.style.top = `${top}px`;
logoDiv.style.display = 'block';
}
function hideLogo() {
if (logoDiv) {
logoDiv.style.display = 'none';
}
}
// Listen for messages from background script to close popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "closePopup") {
hideLogo();
}
});