Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eff3227

Browse files
authored
Merge pull request #60 from osamja/resolve-type-script-errors
fix tsc errors
2 parents 9d86870 + 5283baf commit eff3227

File tree

6 files changed

+88
-77
lines changed

6 files changed

+88
-77
lines changed

‎src/background/background.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
7070

7171
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
7272
if (changeInfo.status === 'complete' && tab.url) {
73+
const url = tab.url;
7374
let problemUrl = /^https:\/\/leetcode\.com\/problems\/.*\/?/;
74-
if (tab.url.match(problemUrl)) {
75+
if (url.match(problemUrl)) {
7576
chrome.storage.local.get(['currentLeetCodeProblemTitle', 'descriptionTabUpdated', 'solutionsTabUpdated'], (result) => {
7677
let lastTitle = result.currentLeetCodeProblemTitle || '';
7778
let descriptionTabUpdated = result.descriptionTabUpdated || false;
@@ -88,18 +89,18 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
8889
}
8990

9091
let descriptionUrl = /^https:\/\/leetcode\.com\/problems\/.*\/(description\/)?/;
91-
if (!descriptionTabUpdated && tab.url.match(descriptionUrl)) {
92+
if (!descriptionTabUpdated && url.match(descriptionUrl)) {
9293
chrome.storage.local.set({ 'descriptionTabUpdated': true });
9394
chrome.tabs.sendMessage(tabId, { action: 'updateDescription', title: tab.title || 'title' });
9495
}
9596

9697
let solutionsUrl = /^https:\/\/leetcode\.com\/problems\/.*\/solutions\/?/;
97-
if (tab.url.match(solutionsUrl)) {
98+
if (url.match(solutionsUrl)) {
9899
chrome.storage.local.set({ 'solutionsTabUpdated': true });
99-
// No need for a timeout if the tab is already loaded completely.
100100
chrome.tabs.sendMessage(tabId, { action: 'updateSolutions', title: tab.title || 'title' });
101101
}
102102
});
103103
}
104104
}
105105
});
106+

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ function getProblem() {
3030
return collectedData;
3131
}
3232

33+
function getCodeComplexity() {
34+
const codeEditor = document.querySelector('[data-track-load="code_editor"]');
35+
if (!codeEditor) {
36+
return {
37+
code: '',
38+
language: '',
39+
error: 'Code editor not found'
40+
};
41+
}
42+
43+
const code = (codeEditor as HTMLElement).innerText;
44+
const languageSelect = document.querySelector('[data-cy="lang-select"]') as HTMLElement;
45+
const language = languageSelect ? languageSelect.innerText : '';
46+
47+
return {
48+
code: code,
49+
language: language
50+
};
51+
}
52+
3353
// On get user code request, read & send the code as a response
3454
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
3555
if (request.type === 'getProblem') {

‎src/content-script/update-description-tab.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// shows the examples if the user has enabled it in the settings
32
function showExamples() {
43
chrome.storage.local.get(['showExamples'], (result) => {
@@ -14,11 +13,18 @@ function showExamples() {
1413
});
1514
}
1615

16+
// Define the Problem interface
17+
interface Problem {
18+
title: string;
19+
rating?: string;
20+
// Add other properties as needed
21+
}
22+
1723
// show the leetcode difficulty if the user has enabled it in the settings
1824
function showDifficulty() {
1925
chrome.storage.local.get(['showDifficulty'], (result) => {
2026
const showDifficulty = result.showDifficulty;
21-
const difficultyContainer = document.querySelectorAll('div.relative.inline-flex')[0];
27+
const difficultyContainer = document.querySelectorAll('div.relative.inline-flex')[0]asHTMLDivElement;
2228
if (!difficultyContainer) return;
2329
if (showDifficulty) {
2430
// hide the first child of the difficulty container
@@ -36,7 +42,7 @@ function showRating(problemTitle: string) {
3642
const showRating = result.showRating;
3743
if (showRating) {
3844
chrome.storage.local.get(['leetcodeProblems'], (result) => {
39-
const problem = result.leetcodeProblems.questions.find((problem: problem) => problem.title === problemTitle);
45+
const problem = result.leetcodeProblems.questions.find((problem: Problem) => problem.title === problemTitle);
4046

4147
let ratingElement = document.getElementById('rating');
4248

@@ -65,7 +71,7 @@ function showRating(problemTitle: string) {
6571
ratingElement.style.color = 'lightcyan';
6672
}
6773

68-
const difficultyContainer = document.querySelectorAll('div.relative.inline-flex')[0];
74+
const difficultyContainer = document.querySelectorAll('div.relative.inline-flex')[0]asHTMLDivElement;
6975
if (difficultyContainer) {
7076
// insert the rating element after the first child of the difficulty container
7177
let parent = difficultyContainer.parentElement;

‎src/content-script/update-solutions-tab.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,19 +215,28 @@ function titleToGitHubFormat(title: string, frontend_id: number): string {
215215
return `${idStr}-${formattedTitle}`;
216216
}
217217

218+
// Define the language map type
219+
type SupportedLanguage = 'python' | 'java' | 'javascript' | 'cpp';
220+
218221
// Fetches the solution code from Neetcode's github repo
219-
async function getCodeSolution(title: string, frontend_id: number, language: string,) {
222+
async function getCodeSolution(title: string, frontend_id: number, language: string): Promise<string|null> {
220223
// map the language names to their extensions
221-
const languageMap = {
224+
const languageMap: Record<SupportedLanguage,string> = {
222225
'python': 'py',
223226
'java': 'java',
224227
'javascript': 'js',
225228
'cpp': 'cpp',
229+
};
230+
231+
// Type guard to check if the language is supported
232+
if (!isLanguageSupported(language)) {
233+
console.error('Unsupported language:', language);
234+
return null;
226235
}
227236

228237
// Convert frontend_id and title to the GitHub-compatible format
229238
const formattedTitle = titleToGitHubFormat(title, frontend_id);
230-
const filePath = `${language}/${formattedTitle}.${languageMap[language]}`;// Change 'other_extension' accordingly
239+
const filePath = `${language}/${formattedTitle}.${languageMap[languageasSupportedLanguage]}`;
231240

232241
// Construct the URL to fetch the file content from GitHub
233242
const url = `https://api.github.com/repos/neetcode-gh/leetcode/contents/${filePath}`;
@@ -242,9 +251,15 @@ async function getCodeSolution(title: string, frontend_id: number, language: str
242251
return code;
243252
} catch (error) {
244253
console.error('Failed to fetch code:', error);
254+
return null;
245255
}
246256
}
247257

258+
// Type guard function to check if a language is supported
259+
function isLanguageSupported(language: string): language is SupportedLanguage {
260+
return ['python', 'java', 'javascript', 'cpp'].includes(language);
261+
}
262+
248263
function createLanguageButtons(problem: any) {
249264
const container = createStyledElement('div', {
250265
paddingTop: '20px',
@@ -282,20 +297,22 @@ function createLanguageButtons(problem: any) {
282297
langName.style.paddingLeft = '15px';
283298
langButton.appendChild(langName);
284299

285-
langButton.addEventListener('click', () => {
286-
let code = getCodeSolution(problem.title, problem.frontend_id, language);
287-
code.then((code) => {
288-
let codeContainer = document.getElementsByClassName('code-container')[0] as HTMLDivElement;
289-
if (codeContainer) {
290-
codeContainer.style.display = 'flex';
291-
codeContainer.textContent = code;
292-
addCopyIconToElement(codeContainer);
293-
}
294-
});
300+
langButton.addEventListener('click', async () => {
301+
const code = await getCodeSolution(problem.title, problem.frontend_id, language);
302+
let codeContainer = document.getElementsByClassName('code-container')[0] as HTMLDivElement;
303+
if (codeContainer && code) {
304+
codeContainer.style.display = 'flex';
305+
codeContainer.textContent = code;
306+
addCopyIconToElement(codeContainer);
307+
} else if (codeContainer) {
308+
codeContainer.style.display = 'flex';
309+
codeContainer.textContent = 'Code not available';
310+
}
295311
});
296312
container.append(langButton);
297313
});
298314
return container;
315+
299316
}
300317

301318
function addCopyIconToElement(element: HTMLElement) {
@@ -370,14 +387,6 @@ chrome.runtime.onMessage.addListener((request) => {
370387
if (!document.querySelector('.code-container') && problem.languages.length > 0) {
371388
let codeContainer = createCodeContainer();
372389
if (searchBar) searchBar.insertBefore(codeContainer, searchBar.children[1]);
373-
// let code = getCodeSolution(problem.title, problem.frontend_id, 'python');
374-
// code.then((code) => {
375-
// let codeContainer = document.getElementsByClassName('code-container')[0] as HTMLDivElement;
376-
// if (codeContainer) {
377-
// codeContainer.textContent = code;
378-
// addCopyIconToElement(codeContainer);
379-
// }
380-
// });
381390
}
382391

383392
// Check if the language buttons container already exists before adding

‎src/popup/popup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ const fixCodeContainer = elements['fixCodeContainer'];
4949

5050
/* Helper functions */
5151
function disableAllButtons(disabled: boolean): void {
52-
let fixCodeButton = elements['fixCodeBtn'];
53-
let getComplexityButton = elements['getComplexityBtn'];
52+
let fixCodeButton = elements['fixCodeBtn']asHTMLButtonElement;
53+
let getComplexityButton = elements['getComplexityBtn']asHTMLButtonElement;
5454

5555
// Use the arguments to determine if a specific button should be disabled
5656
fixCodeButton && (fixCodeButton.disabled = disabled);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /