@@ -31,19 +31,51 @@ function showExamples() {
3131function showDifficulty ( ) {
3232 chrome . storage . local . get ( [ 'showDifficulty' ] , ( result ) => {
3333 const showDifficulty = result . showDifficulty ;
34+ const difficultyContainer = document . querySelectorAll ( 'div.mt-3.flex' ) [ 0 ] ;
35+ if ( ! difficultyContainer ) return ;
36+ if ( showDifficulty ) {
37+ // hide the first child of the difficulty container
38+ difficultyContainer . children [ 0 ] . style . display = 'block' ;
39+ }
40+ else {
41+ difficultyContainer . children [ 0 ] . style . display = 'none' ;
42+ }
43+ } ) ;
44+ }
3445
35- // Finding the difficulty element and then toggling the display.
36- const colors = [ 'bg-olive' , 'bg-yellow' , 'bg-red' ] ;
37- for ( const color in colors ) {
38- if ( ! color ) continue ;
39- const element = document . querySelectorAll ( 'div.' + colors [ color ] ) [ 0 ] ;
40- if ( element instanceof HTMLElement ) {
41- element . style . display = showDifficulty ? 'block' : 'none' ;
42- }
46+ // show the leetcode problem rating if the user has enabled it in the settings
47+ function showRating ( problemTitle : string ) {
48+ chrome . storage . local . get ( [ 'showRating' ] , ( result ) => {
49+ const showRating = result . showRating ;
50+ if ( showRating ) {
51+ chrome . storage . local . get ( [ 'leetcodeProblems' ] , ( result ) => {
52+ const problem = result . leetcodeProblems . questions . find ( ( problem : problem ) => problem . title === problemTitle ) ;
53+
54+ let ratingElement = document . getElementById ( 'ratingElement' ) ;
55+
56+ if ( ratingElement ) {
57+ // update the existing rating element
58+ ratingElement . textContent = problem . rating ;
59+ } else {
60+ // create a new rating element
61+ ratingElement = document . createElement ( 'div' ) ;
62+ ratingElement . id = 'ratingElement' ;
63+ ratingElement . textContent = problem . rating ;
64+ ratingElement . style . fontSize = '12px' ;
65+ ratingElement . style . color = 'lightcyan' ;
66+ }
67+
68+ const difficultyContainer = document . querySelectorAll ( 'div.mt-3.flex' ) [ 0 ] ;
69+ if ( difficultyContainer ) {
70+ // insert the rating element after the first child of the difficulty container
71+ difficultyContainer . insertBefore ( ratingElement , difficultyContainer . children [ 0 ] . nextSibling ) ;
72+ }
73+ } ) ;
4374 }
4475 } ) ;
4576}
4677
78+
4779// show the company tags if the user has enabled it in the settings
4880function showCompanyTags ( problemTitle : string ) {
4981 chrome . storage . local . get ( [ 'showCompanyTags' ] , ( result ) => {
@@ -162,5 +194,6 @@ chrome.runtime.onMessage.addListener((request) => {
162194 showExamples ( ) ;
163195 showCompanyTags ( request . title . split ( '-' ) [ 0 ] . trim ( ) ) ;
164196 showDifficulty ( ) ;
197+ showRating ( request . title . split ( '-' ) [ 0 ] . trim ( ) ) ;
165198 }
166199} ) ;
0 commit comments