-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
133 lines (118 loc) · 3.27 KB
/
Copy pathcontent-script.js
File metadata and controls
133 lines (118 loc) · 3.27 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Content script for managing the floating panel
let panel = null;
let iframe = null;
let isVisible = false;
// Function to create the floating panel
function createPanel() {
// Create the panel container
panel = document.createElement('div');
panel.id = 'floatingAgentPanel';
panel.style.cssText = `
position: fixed;
top: 20px;
right: -600px;
width: 500px;
height: 95vh;
background: transparent;
z-index: 10000;
display: block;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: right 0.3s ease-in-out;
`;
// Create the iframe
iframe = document.createElement('iframe');
iframe.src = chrome.runtime.getURL('content.html');
iframe.style.cssText = `
width: 100%;
height: 100%;
border: none;
border-radius: 8px;
background: white;
`;
panel.appendChild(iframe);
document.body.appendChild(panel);
}
// Function to toggle panel visibility
function togglePanel() {
if (!panel) {
createPanel();
// Add a small delay to allow the initial CSS to be applied
setTimeout(() => {
isVisible = true;
panel.style.right = '20px';
// Send DOM data after panel is visible
sendDOMData();
}, 50);
return;
}
isVisible = !isVisible;
panel.style.right = isVisible ? '20px' : '-600px';
// Send DOM data when panel becomes visible
if (isVisible) {
sendDOMData();
}
}
// Function to send DOM data to iframe
function sendDOMData() {
const domData = {
url: window.location.href,
title: document.title,
content: document.body.innerText
};
// Wait for iframe to load before sending message
if (!iframe.contentDocument || iframe.contentDocument.readyState !== 'complete') {
iframe.onload = () => {
iframe.contentWindow.postMessage({
type: 'setDOMData',
data: domData
}, '*');
console.log('Sent DOM data after iframe load');
};
} else {
// If iframe is already loaded, send message immediately
iframe.contentWindow.postMessage({
type: 'setDOMData',
data: domData
}, '*');
console.log('Sent DOM data immediately');
}
}
// Listen for messages from the extension
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'togglePanel') {
togglePanel();
}
});
// Listen for messages from the iframe
window.addEventListener('message', (event) => {
// Verify the origin
if (event.source !== iframe?.contentWindow) {
return;
}
const { type } = event.data;
if (type === 'closePanel') {
if (panel) {
isVisible = false;
panel.style.right = '-600px';
}
} else if (type === 'saveSelection') {
// Get the current selection
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const fragment = range.cloneContents();
// Create temporary elements to get both HTML and text content
const tempDiv = document.createElement('div');
tempDiv.appendChild(fragment.cloneNode(true));
const data = {
html: tempDiv.innerHTML,
text: tempDiv.textContent,
timestamp: new Date().toISOString()
};
// Send the selection data back to the iframe
iframe.contentWindow.postMessage({
type: 'saveSelection',
data: data
}, '*');
}
});