File tree Expand file tree Collapse file tree 2 files changed +13
-44
lines changed Expand file tree Collapse file tree 2 files changed +13
-44
lines changed Original file line number Diff line number Diff line change 1
1
async function getProblemData ( ) {
2
- const titleEl = document . querySelector ( 'div[class*="text-title-large"]' ) ;
3
- const title = titleEl ? titleEl . textContent . trim ( ) : "Unknown Title" ;
4
-
5
- const difficultyEl = Array . from ( document . querySelectorAll ( 'div[class*="text-difficulty"]' ) ) . find ( ( el ) =>
6
- el . textContent ?. match ( / E a s y | M e d i u m | H a r d / )
7
- ) ;
8
- const difficulty = difficultyEl
9
- ? difficultyEl . textContent . trim ( )
10
- : "Unknown Difficulty" ;
11
-
12
2
const pathParts = window . location . pathname . split ( "/" ) . filter ( Boolean ) ;
13
3
const problemSlug = pathParts [ 1 ] || "unknown-problem" ;
14
-
15
- // Fetch tags using the API helper
16
- let tags = [ ] ;
4
+ if ( ! problemSlug || problemSlug === "unknown-problem" ) {
5
+ return null ;
6
+ }
7
+ let apiData = null ;
17
8
try {
18
- tags = await fetchLeetCodeTags ( problemSlug ) ;
9
+ apiData = await fetchLeetCodeProblemData ( problemSlug ) ;
19
10
} catch ( e ) {
20
- tags = [ ] ;
11
+ apiData = null ;
12
+ console . log ( e . message ) ;
21
13
}
22
-
23
- // Only return valid data if title and slug are valid
24
- if ( ! title || title === "Unknown Title" || ! problemSlug || problemSlug === "unknown-problem" ) {
14
+ if ( ! apiData ?. title || ! apiData ?. difficulty ) {
25
15
return null ;
26
16
}
27
-
28
17
const problemData = {
29
- title,
30
- difficulty,
18
+ title : apiData . title ,
19
+ difficulty : apiData . difficulty ,
31
20
slug : problemSlug ,
32
21
url : window . location . href ,
33
22
timestamp : Date . now ( ) ,
34
- tags,
23
+ tags : apiData . tags ,
35
24
} ;
36
-
37
- console . log ( "LC Problem Detected:" , problemData ) ;
25
+ console . log ( "LC Problem Detected (API):" , problemData ) ;
38
26
return problemData ;
39
27
}
40
28
Original file line number Diff line number Diff line change 1
- async function fetchLeetCodeTags ( slug ) {
2
- const query = {
3
- query : `query getQuestionDetail($titleSlug: String!) { question(titleSlug: $titleSlug) { topicTags { name slug } } }` ,
4
- variables : { titleSlug : slug } ,
5
- } ;
6
-
7
- const response = await fetch ( "https://leetcode.com/graphql" , {
8
- method : "POST" ,
9
- headers : {
10
- "Content-Type" : "application/json" ,
11
- } ,
12
- body : JSON . stringify ( query ) ,
13
- credentials : "same-origin" ,
14
- } ) ;
15
-
16
- if ( ! response . ok ) return [ ] ;
17
- const data = await response . json ( ) ;
18
- return data . data ?. question ?. topicTags ?. map ( ( tag ) => tag . name ) || [ ] ;
19
- }
20
-
1
+ // Fetches title, difficulty, and tags for a LeetCode problem using the GraphQL API
21
2
async function fetchLeetCodeProblemData ( slug ) {
22
3
const query = {
23
4
query : `query getQuestionDetail($titleSlug: String!) { question(titleSlug: $titleSlug) { title difficulty topicTags { name slug } } }` ,
You can’t perform that action at this time.
0 commit comments