Skip to content

Commit de2d610

Browse files
committed
fix gpt buttons not working
1 parent e6b91ad commit de2d610

File tree

3 files changed

+49
-54
lines changed

3 files changed

+49
-54
lines changed

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 3,
33
"name": "Leetcode Explained",
4-
"version": "2.0.6",
4+
"version": "2.0.7",
55
"description": "Adds company tags, video solutions and GPT code analysis into Leetcode problems.",
66
"icons": {
77
"16": "src/assets/images/logo/icon-16.png",

src/content-script/get-user-code.ts

+27-36
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
1-
// Reads the code from the user's code editor and sends it to the background script
2-
3-
// On get user code request, read & send the code as a response
4-
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
5-
if (request.type === 'getCode') {
6-
sendResponse({ data: getCode() });
7-
}
8-
});
1+
/**
2+
*
3+
* 1. Read the code from the code editor
4+
* 2. Read the test cases & examples from the problem page
5+
* 3. Send the code & test cases to the background script
6+
*
7+
* */
98

109
// Read the users code and examples from the Leetcode problem page
1110
function getCode() {
12-
const viewLines = document.getElementsByClassName('view-line');
13-
const textArray = ['Heres the code'];
1411

15-
for (const viewLine of viewLines) {
16-
textArray.push(viewLine.textContent);
12+
// Get the function definition and users code from the code editor
13+
let textArray = ["heres the function definition and the users code which might be not present or might be incorrect.\n"]
14+
const codeEditor = document.getElementsByClassName('view-line');
15+
for (const viewLine of codeEditor) {
16+
let text = viewLine.textContent;
17+
if (text) textArray.push(text);
1718
}
1819

19-
textArray.push('Heres the problem description, test cases, and constraints\n');
20-
21-
// Add the test cases & examples to the
22-
const descriptionContainer = document.querySelector('div._1l1MA') as Element;
23-
if (!descriptionContainer) {
24-
return;
20+
// Add the test cases & examples
21+
textArray.push('\nHeres the description, examples, and constraints for the problem\n');
22+
const examples = document.getElementsByClassName('xFUwe')[0];
23+
for (const child of examples.children) {
24+
let text = child.textContent;
25+
if (text) textArray.push(text);
2526
}
27+
return textArray;
28+
}
2629

27-
// Add all the text from description container to the text array
28-
for (const child of descriptionContainer.children) {
29-
textArray.push(child.textContent);
30-
}
3130

32-
const examples = descriptionContainer.getElementsByClassName('example');
33-
if (examples && examples.length > 0) {
34-
const parent = examples[0].parentNode as Element;
35-
if (!parent) {
36-
return;
37-
}
38-
const startIndex = Array.from(descriptionContainer.children).indexOf(parent);
39-
for (let i = startIndex; i < descriptionContainer.children.length; i++) {
40-
const child = descriptionContainer.children[i] as HTMLElement;
41-
console.log(child);
42-
child.style.display = showExamples ? 'block' : 'none';
43-
}
31+
// On get user code request, read & send the code as a response
32+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
33+
if (request.type === 'getCode') {
34+
sendResponse({ data: getCode() });
4435
}
45-
return textArray;
46-
}
36+
});
37+

src/popup/popup.ts

+21-17
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function initActionButton(buttonId: string, action: string, chatGPTProvider: Cha
7878
if (codeText) {
7979
processCode(chatGPTProvider, codeText, action);
8080
} else {
81-
const errorMessage = 'Unable to retrieve code. Please navigate to a Leetcode problem page and refresh the page.';
81+
const errorMessage = "Unable to retrieve code. Please open a Leetcode problem's description page and refresh the page.";
8282
setInfoMessage(errorMessage, 5000);
8383
}
8484
};
@@ -116,8 +116,6 @@ function processCode(
116116
disableAllButtons(true);
117117
clearResponse();
118118

119-
120-
121119
const problemTitle = infoMessage && infoMessage.textContent;
122120

123121
let prompt = '';
@@ -127,24 +125,30 @@ function processCode(
127125
problem titled ${problemTitle} and the accompanying code below. Return only the time
128126
and space complexity of the function in big O notation. The space complexity should not
129127
include the output (return value) of the function. Your analysis should be direct and concise.
130-
You can provide a one sentence explanation of the time and space complexity.`;
128+
You can provide a one sentence explanation of the time and space complexity. The problem
129+
description and code are provided below\n. ${codeText}`;
131130
if (infoMessage) infoMessage.textContent = 'Analyzing code complexity ...';
132131
if (analyzeCodeResponse) analyzeCodeResponse.classList.remove('hidden');
133132
if (fixCodeContainer) fixCodeContainer.classList.add('hidden');
134133
}
135134
else if (action === 'fix') {
135+
136+
// The prompt for fixing the users code
136137
prompt = `
137-
As a coding expert, I require your aid with a specific LeetCode problem
138-
called ${problemTitle}. Generate the best possible, optimized solution
139-
for this problem. Only the function definition and code should be returned
140-
in plain text format with no usage of code blocks. Code comments are optional.
141-
Anything other than the code text is not permitted. I should be able to copy
142-
and paste the code into the LeetCode editor and run it successfully.`;
138+
As a coding professional, I need your expertise with a specific LeetCode problem named ${problemTitle}.
139+
Please follow the instructions:
140+
1. If no code is provided: Generate an efficient and accurate solution for the problem.
141+
2. If code is provided and contains errors: Identify the issues, correct them, and optimize the code if possible.
142+
3. If the provided code is already correct and optimized: Simply return it as-is.
143+
IMPORTANT: Your response should only include the function definition and code solution in plain text format (no backticks, code blocks, or additional formatting).
144+
Here's the problem description and code:\n
145+
${codeText}
146+
`
143147
if (infoMessage) infoMessage.textContent = 'Generating solution code ...';
144148
analyzeCodeResponse && analyzeCodeResponse.classList.add('hidden');
145149
fixCodeContainer && fixCodeContainer.classList.remove('hidden');
146150
}
147-
prompt += 'Heres the problem description, examples, constraints and my code. Note that code might not exist and may be incorrect.\n' + codeText;
151+
console.log('prompt', prompt);
148152

149153
let response = '';
150154
Promise.race([
@@ -170,7 +174,7 @@ function processCode(
170174
}
171175
},
172176
}),
173-
timeout(12000)
177+
timeout(15000)
174178
]).catch((error) => {
175179
infoMessage && (infoMessage.textContent = 'The request timed out. Please try again.');
176180
console.error(error);
@@ -191,17 +195,17 @@ async function main(): Promise<void> {
191195
// Load font size from storage
192196
chrome.storage.local.get('fontSize', function (data) {
193197
if (data.fontSize) {
194-
fontSizeElement.style.setProperty('--dynamic-font-size', `${data.fontSize}px`);
198+
fontSizeElement.style.setProperty('--dynamic-font-size', `${data.fontSize} px`);
195199
if (parseInt(data.fontSize) >= 18) {
196200
const width = (parseInt(data.fontSize) * 24 + 200);
197-
document.body.style.width = `${width + 20}px`;
198-
fixCodeContainer && (fixCodeContainer.style.maxWidth = `${width}px`);
199-
analyzeCodeResponse && (analyzeCodeResponse.style.maxWidth = `${width}px`);
201+
document.body.style.width = `${width + 20} px`;
202+
fixCodeContainer && (fixCodeContainer.style.maxWidth = `${width} px`);
203+
analyzeCodeResponse && (analyzeCodeResponse.style.maxWidth = `${width} px`);
200204
}
201205

202206
const sizes = document.getElementsByClassName('material-button');
203207
for (let i = 0; i < sizes.length; i++) {
204-
(sizes[i] as HTMLElement).style.width = `${data.fontSize * 13}px`;
208+
(sizes[i] as HTMLElement).style.width = `${data.fontSize * 13} px`;
205209
}
206210
}
207211
});

0 commit comments

Comments
 (0)