-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
82 lines (73 loc) · 2.15 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
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
const style = document.createElement("style");
style.textContent = `
.result {
position: absolute;
max-width: 300px;
height: 100px;
background-color: white;
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
z-index: 10000;
font-family: Arial, sans-serif;
font-size: 14px;
line-height: 1.4;
color: black;
}
.result-close {
position: absolute;
top: 5px;
right: 5px;
cursor: pointer;
font-size: 20px;
color: black;
}
`;
document.head.appendChild(style);
// Show the result near the selected text
function showFloatingResult(text) {
// Remove prev result
const existingResult = document.querySelector(".result");
if (existingResult) {
existingResult.remove();
}
const resultDiv = document.createElement("div");
resultDiv.className = "result";
resultDiv.innerHTML = `
<div class="result-close">×</div>
<p>${text}</p>
`;
// Get selected text and position
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
// Position the output near the selected text
resultDiv.style.left = `${rect.left + window.scrollX}px`;
resultDiv.style.top = `${rect.bottom + window.scrollY + 10}px`;
document.body.appendChild(resultDiv);
// Adjust position if the output goes off-screen
const divRect = resultDiv.getBoundingClientRect();
if (divRect.right > window.innerWidth) {
resultDiv.style.left = `${window.innerWidth - divRect.width - 10}px`;
}
if (divRect.bottom > window.innerHeight) {
resultDiv.style.top = `${
rect.top + window.scrollY - divRect.height - 10
}px`;
}
const closeButton = resultDiv.querySelector(".result-close");
closeButton.addEventListener("click", () => {
resultDiv.remove();
});
// Automatically remove the div after 10 seconds
setTimeout(() => {
resultDiv.remove();
}, 10000);
}
// Listen for messages from the background.js
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "showAlert") {
showFloatingResult(request.text);
}
});