-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
309 lines (259 loc) · 9.12 KB
/
content.js
File metadata and controls
309 lines (259 loc) · 9.12 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Mock data for testing
const mockSuggestions = {
"bug": [
"Check if the error occurs in the latest version",
"Try reproducing the issue in a different browser",
"Look for similar issues in the closed issues"
],
"feature": [
"Review existing feature requests",
"Check if this aligns with project roadmap",
"Consider backward compatibility"
],
"documentation": [
"Update README.md",
"Add code examples",
"Include troubleshooting steps"
]
};
function createSuggestionsPopup(suggestions) {
const popup = document.createElement('div');
popup.className = 'ai-suggestions-popup';
popup.dataset.state = 'expanded'; // Track popup state
// Add header div for title and close button
const header = document.createElement('div');
header.className = 'popup-header';
const title = document.createElement('h3');
title.textContent = 'AI Suggestions';
const buttonContainer = document.createElement('div');
buttonContainer.className = 'popup-buttons';
// Add minimize button
const minimizeButton = document.createElement('button');
minimizeButton.innerHTML = '−';
minimizeButton.className = 'minimize-button';
minimizeButton.title = 'Minimize';
minimizeButton.onclick = () => togglePopupState(popup);
// Add close button
const closeButton = document.createElement('button');
closeButton.innerHTML = '×';
closeButton.className = 'close-button';
closeButton.title = 'Close';
closeButton.onclick = () => popup.remove();
buttonContainer.appendChild(minimizeButton);
buttonContainer.appendChild(closeButton);
header.appendChild(title);
header.appendChild(buttonContainer);
popup.appendChild(header);
// Create content container
const content = document.createElement('div');
content.className = 'popup-content';
// Create sections for different types of suggestions
suggestions.forEach((suggestion, index) => {
if (suggestion.startsWith('📊') || suggestion.startsWith('🛠️') ||
suggestion.startsWith('💻') || suggestion.startsWith('🧪')) {
// Create section header
const sectionHeader = document.createElement('div');
sectionHeader.className = 'section-header';
sectionHeader.textContent = suggestion;
content.appendChild(sectionHeader);
} else if (suggestion.startsWith('•')) {
// Create animated list item
const item = document.createElement('div');
item.className = 'suggestion-item';
item.style.animationDelay = `${index * 0.1}s`;
const bullet = document.createElement('span');
bullet.className = 'bullet';
bullet.textContent = '•';
const text = document.createElement('span');
text.className = 'suggestion-text';
text.textContent = suggestion.substring(2);
item.appendChild(bullet);
item.appendChild(text);
content.appendChild(item);
} else if (suggestion.trim() !== '') {
// Regular text
const text = document.createElement('div');
text.className = 'suggestion-text';
text.textContent = suggestion;
content.appendChild(text);
}
});
popup.appendChild(content);
// Make popup draggable
makeDraggable(popup);
return popup;
}
function togglePopupState(popup) {
const currentState = popup.dataset.state;
const content = popup.querySelector('.popup-content');
const minimizeButton = popup.querySelector('.minimize-button');
if (currentState === 'expanded') {
// Minimize
popup.dataset.state = 'minimized';
content.style.display = 'none';
minimizeButton.innerHTML = '+';
minimizeButton.title = 'Expand';
popup.classList.add('minimized');
} else {
// Expand
popup.dataset.state = 'expanded';
content.style.display = 'block';
minimizeButton.innerHTML = '−';
minimizeButton.title = 'Minimize';
popup.classList.remove('minimized');
}
}
function makeDraggable(element) {
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
element.querySelector('.popup-header').onmousedown = dragMouseDown;
function dragMouseDown(e) {
e.preventDefault();
// Get mouse position at startup
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e.preventDefault();
// Calculate new position
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// Set element's new position
element.style.top = (element.offsetTop - pos2) + "px";
element.style.left = (element.offsetLeft - pos1) + "px";
}
function closeDragElement() {
document.onmouseup = null;
document.onmousemove = null;
}
}
async function waitForElement(selector, timeout = 10000) {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const element = document.querySelector(selector);
if (element) {
return element;
}
await new Promise(resolve => setTimeout(resolve, 100));
}
return null;
}
async function getRepositoryInfo() {
const pathParts = window.location.pathname.split('/');
const repoName = `${pathParts[1]}/${pathParts[2]}`;
// Wait for language element to be available
const languageElement = await waitForElement('[itemprop="programmingLanguage"]');
const primaryLanguage = languageElement ? languageElement.textContent.trim() : 'Unknown';
return {
repoName,
primaryLanguage
};
}
async function analyzePage() {
let loadingPopup = null; // Declare once at the top
try {
console.log('Starting analysis...');
// Wait for issue elements to be available
console.log('Waiting for title element...');
const titleElement = await waitForElement('[data-testid="issue-header"]');
console.log('Waiting for body element...');
const bodyElement = await waitForElement('[data-testid="markdown-body"]');
if (!titleElement || !bodyElement) {
throw new Error('Could not find issue elements. Please make sure you are on a GitHub issue page.');
}
console.log('Elements found, extracting content...');
const title = titleElement.textContent.trim();
const description = bodyElement.textContent.trim();
console.log('Title:', title.substring(0, 50) + '...');
console.log('Description:', description.substring(0, 50) + '...');
// Remove existing popup if any
const existingPopup = document.querySelector('.ai-suggestions-popup');
if (existingPopup) {
existingPopup.remove();
}
// Create and show loading popup
loadingPopup = createLoadingPopup(); // Use existing variable
document.body.appendChild(loadingPopup);
// Get repository context
console.log('Getting repository info...');
const repoInfo = await getRepositoryInfo();
console.log('Repository info:', repoInfo);
// Send data to background script for analysis
console.log('Sending message to background script...');
const response = await chrome.runtime.sendMessage({
action: 'analyzeIssue',
data: {
title: title || 'No title provided',
description: description || 'No description provided',
...repoInfo
}
});
console.log('Received response:', response);
if (response.error) {
throw new Error(response.error);
}
// Remove loading popup
if (loadingPopup) {
loadingPopup.remove();
}
// Changed: Handle response.suggestions directly
if (typeof response.suggestions === 'string') {
const suggestions = response.suggestions
.split('\n')
.filter(line => line.trim().length > 0);
const popup = createSuggestionsPopup(suggestions);
document.body.appendChild(popup);
} else {
throw new Error('Invalid response format from API');
}
} catch (error) {
console.error('Error in analyzePage:', error);
// Show error in popup
if (loadingPopup) {
loadingPopup.remove();
}
const errorPopup = createSuggestionsPopup([
'Error analyzing issue.',
'Please try again later.',
`Details: ${error.message}`
]);
document.body.appendChild(errorPopup);
}
}
function createLoadingPopup() {
const popup = document.createElement('div');
popup.className = 'ai-suggestions-popup';
const header = document.createElement('div');
header.className = 'popup-header';
const title = document.createElement('h3');
title.textContent = 'Analyzing Issue...';
header.appendChild(title);
popup.appendChild(header);
const loader = document.createElement('div');
loader.className = 'loader';
popup.appendChild(loader);
return popup;
}
// Wait for page load and then run analysis
if (window.location.pathname.includes('/issues/')) {
// Wait for DOM content to be loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', analyzePage);
} else {
analyzePage();
}
// Also handle dynamic navigation (GitHub's SPA navigation)
let lastPath = window.location.pathname;
setInterval(() => {
const currentPath = window.location.pathname;
if (currentPath !== lastPath) {
lastPath = currentPath;
if (currentPath.includes('/issues/')) {
analyzePage();
}
}
}, 1000);
}