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 11async 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-
122 const pathParts = window . location . pathname . split ( "/" ) . filter ( Boolean ) ;
133 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 ;
178 try {
18- tags = await fetchLeetCodeTags ( problemSlug ) ;
9+ apiData = await fetchLeetCodeProblemData ( problemSlug ) ;
1910 } catch ( e ) {
20- tags = [ ] ;
11+ apiData = null ;
12+ console . log ( e . message ) ;
2113 }
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 ) {
2515 return null ;
2616 }
27-
2817 const problemData = {
29- title,
30- difficulty,
18+ title : apiData . title ,
19+ difficulty : apiData . difficulty ,
3120 slug : problemSlug ,
3221 url : window . location . href ,
3322 timestamp : Date . now ( ) ,
34- tags,
23+ tags : apiData . tags ,
3524 } ;
36-
37- console . log ( "LC Problem Detected:" , problemData ) ;
25+ console . log ( "LC Problem Detected (API):" , problemData ) ;
3826 return problemData ;
3927}
4028
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
212async function fetchLeetCodeProblemData ( slug ) {
223 const query = {
234 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