-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
47 lines (39 loc) · 1.44 KB
/
contentScript.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
function transferSelectedTextToTextarea() {
const textArea = document.getElementById('prompt-textarea');
if (textArea) {
// Define the style element
const style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = `
@keyframes fadeBackground {
0% { background-color: yellow; }
100% { background-color: transparent; }
}
.fade-background {
animation: fadeBackground 0.5s ease forwards;
}
`;
// Add the style element to the document's head
document.head.appendChild(style);
document.body.addEventListener('mouseup', (event) => {
if (event.target.id === 'prompt-textarea') {
return;
}
const selectedText = window.getSelection().toString();
if (selectedText) {
textArea.value = selectedText + '\n\n'; // Replaces the existing text with the selected text followed by two new lines
textArea.focus();
textArea.scrollTop = textArea.scrollHeight;
// Add the fade-background class to trigger the animation
textArea.classList.add('fade-background');
// Remove the class after the animation duration to be able to re-trigger it for subsequent text selections
setTimeout(() => {
textArea.classList.remove('fade-background');
}, 500); // Adjust the timeout duration to match the animation duration
}
});
} else {
console.log('Element with id "prompt-textarea" not found.');
}
}
transferSelectedTextToTextarea();