-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
222 lines (183 loc) · 6.36 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
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
function createFeedbackPanel() {
if (!document.getElementById('ai-feedback-panel')) {
const panel = document.createElement('div');
panel.id = 'ai-feedback-panel';
const closeButton = document.createElement('button');
closeButton.innerHTML = '✕';
const iconContainer = document.createElement('div');
iconContainer.id = 'ai-feedback-icon';
iconContainer.innerHTML = `<img src="${chrome.runtime.getURL("icon48.png")}" alt="icon" style="width: 48px; height: 48px;">`;
let isPanelExpanded = true;
const togglePanel = () => {
isPanelExpanded = !isPanelExpanded;
if (isPanelExpanded) {
panel.style.transform = 'translateX(0)';
iconContainer.style.display = 'none';
} else {
panel.style.transform = 'translateX(100%)';
setTimeout(() => {
iconContainer.style.display = 'flex';
}, 300);
}
};
closeButton.onclick = () => {
isPanelExpanded = false;
panel.style.transform = 'translateX(100%)';
setTimeout(() => {
iconContainer.style.display = 'flex';
}, 300);
};
iconContainer.onclick = () => {
isPanelExpanded = true;
panel.style.transform = 'translateX(0)';
iconContainer.style.display = 'none';
};
const content = document.createElement('div');
content.id = 'ai-feedback-content';
panel.appendChild(closeButton);
panel.appendChild(content);
document.body.appendChild(iconContainer);
document.body.appendChild(panel);
return content;
}
return document.getElementById('ai-feedback-content');
}
function displayFeedback(files) {
const content = document.getElementById('ai-feedback-content');
content.innerHTML = '';
files.forEach(file => {
const fileSection = document.createElement('div');
fileSection.className = 'file-section';
const fileName = document.createElement('h3');
fileName.className = 'file-name';
fileName.textContent = file.fileName;
const feedback = document.createElement('div');
feedback.className = 'feedback-content';
feedback.innerHTML = utils.formatMarkdown(file.feedback);
fileSection.appendChild(fileName);
fileSection.appendChild(feedback);
content.appendChild(fileSection);
});
}
function displayError(message) {
const content = document.getElementById('ai-feedback-content');
if (content) {
const errorDiv = document.createElement('div');
errorDiv.className = 'error-message';
errorDiv.textContent = message;
content.innerHTML = '';
content.appendChild(errorDiv);
}
}
function displayLoading() {
const content = document.getElementById('ai-feedback-content');
if (content) {
const loadingDiv = document.createElement('div');
loadingDiv.className = 'loading-message';
loadingDiv.innerHTML = `<div>The AI helper is analysing, please wait...</div>`;
content.innerHTML = '';
content.appendChild(loadingDiv);
}
}
async function getAIFeedback(originalCode, currentCode) {
const { available } = await ai.languageModel.capabilities();
if (available === "no") {
throw new Error("AI model not available");
}
const session = await ai.languageModel.create();
const prompt = `Please analyze this code change and provide feedback on:
1. Is the current code syntactically correct?
2. Summary of modifications
3. Advice on the modifications
Original code:
${originalCode}
Current code:
${currentCode}`;
const feedback = await session.prompt(prompt);
return feedback;
}
async function extractDiffContent() {
const diffEntries = document.querySelectorAll('copilot-diff-entry');
if (diffEntries.length === 0) {
throw new Error("No code changes found to analyze");
}
const filesContent = [];
for (const entry of diffEntries) {
const filePath = entry.getAttribute('data-file-path');
const codeLines = entry.querySelectorAll('.blob-code-inner.blob-code-marker');
let originalCode = [];
let currentCode = [];
codeLines.forEach((line, index) => {
const lineContent = line.textContent.trimRight();
if (index % 2 === 0) {
originalCode.push(lineContent);
} else {
currentCode.push(lineContent);
}
});
const originalText = originalCode.join('\n');
const currentText = currentCode.join('\n');
try {
const aiFeedback = await getAIFeedback(originalText, currentText);
filesContent.push({
fileName: filePath,
originalCode: originalText,
currentCode: currentText,
feedback: aiFeedback
});
} catch (error) {
throw new Error(`Error analyzing ${filePath}: ${error.message}`);
}
}
return filesContent;
}
function checkSplitMode() {
const splitRadio = document.querySelector('input[type="radio"][value="split"]');
return splitRadio && splitRadio.checked;
}
function addExtractButton() {
const observer = new MutationObserver((mutations, obs) => {
const headerActions = document.querySelector('.ml-2.hide-sm.hide-md');
if (headerActions && !document.getElementById('extract-diff-btn')) {
const button = document.createElement('button');
button.id = 'extract-diff-btn';
button.className = 'btn btn-sm';
button.textContent = 'Get AI Review';
button.onclick = async () => {
const content = createFeedbackPanel();
if (!checkSplitMode()) {
displayError("Please click the setting button on the left side of get AI review button, and choose split mode, this reviewer can only work under split mode");
return;
}
button.textContent = 'Analyzing...';
button.disabled = true;
displayLoading();
try {
const files = await extractDiffContent();
displayFeedback(files);
} catch (error) {
console.error('Error during analysis:', error);
displayError(error.message);
} finally {
button.textContent = 'Get AI Review';
button.disabled = false;
}
};
headerActions.appendChild(button);
obs.disconnect();
}
});
observer.observe(document, { childList: true, subtree: true });
}
function initialize() {
addExtractButton();
}
initialize();
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
initialize();
}
}).observe(document, { subtree: true, childList: true });