-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpopup_manager.js
169 lines (158 loc) · 5.64 KB
/
popup_manager.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { generateWithLLM } from './llm_service.js';
import * as prompt from './prompt.js';
export function generateCodeWithPopup(jsonBlob, requestBody, xpathMap, LLM_CONFIG) {
const popupHTML = `
<html>
<head>
<title>Code Generation</title>
<meta charset="utf-8">
<style>
body {
width: 300px;
padding: 30px;
text-align: center;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: #f8f9fa;
margin: 0;
color: #2c3e50;
}
.container {
background: white;
border-radius: 12px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
h2 {
margin: 0 0 15px 0;
font-size: 20px;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
}
.spinner {
width: 24px;
height: 24px;
border: 3px solid #e9ecef;
border-top: 3px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
p {
margin: 15px 0 0 0;
color: #6c757d;
font-size: 14px;
line-height: 1.5;
}
</style>
</head>
<body>
<div class="container">
<h2><div class="spinner"></div>Generating Code</h2>
<p>Please wait while LLM processes your actions...</p>
</div>
</body>
</html>
`;
const errorHTML = `
<html>
<head>
<title>Error</title>
<meta charset="utf-8">
<style>
body {
width: 300px;
padding: 30px;
text-align: center;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: #f8f9fa;
margin: 0;
color: #2c3e50;
}
.container {
background: #fff5f5;
border-radius: 12px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
h2 {
margin: 0 0 15px 0;
color: #dc3545;
font-size: 20px;
}
p {
margin: 15px 0 0 0;
color: #6c757d;
font-size: 14px;
line-height: 1.5;
}
</style>
</head>
<body>
<div class="container">
<h2>❌ Error</h2>
<p>Failed to generate code.<br>Check console for details.</p>
</div>
</body>
</html>
`;
chrome.windows.create({
url: 'data:text/html;charset=utf-8,' + encodeURIComponent(popupHTML),
type: 'popup',
width: 360,
height: 240
}, async (popupWindow) => {
const popupId = popupWindow.id;
// Properly read the blob as text
const jsonStr = await blobToString(jsonBlob);
// Create properly formatted messages for the API
const promptContent = prompt.SELENIUM_PROMPT + JSON.stringify(JSON.parse(jsonStr), null, 2);
const messages = [
{
role: "system",
content: "You are a helpful assistant that generates Selenium code based on browser actions."
},
{
role: "user",
content: promptContent
}
];
generateWithLLM(messages, LLM_CONFIG, jsonStr, popupId, errorHTML)
.then(pythonCode => {
Object.entries(xpathMap).forEach(([placeholder, xpath]) => {
pythonCode = pythonCode.replace(new RegExp(placeholder, 'g'), xpath);
});
// Create data URLs for downloads
const jsonDataUrl = 'data:application/json;base64,' + btoa(unescape(encodeURIComponent(jsonStr)));
const pythonDataUrl = 'data:application/x-python;base64,' + btoa(unescape(encodeURIComponent(pythonCode)));
chrome.downloads.download({
url: jsonDataUrl,
filename: 'tracking_log.json',
saveAs: true
});
chrome.downloads.download({
url: pythonDataUrl,
filename: 'selenium_test.py',
saveAs: true
});
})
.finally(() => {
setTimeout(() => {
chrome.windows.remove(popupId);
}, 2000);
});
});
}
// Helper to convert a Blob to a string
async function blobToString(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsText(blob);
});
}