1+ // Helper function to get or create user ID
2+ function getRandomToken ( ) : string {
3+ const randomPool = new Uint8Array ( 32 ) ;
4+ crypto . getRandomValues ( randomPool ) ;
5+ return Array . from ( randomPool )
6+ . map ( b => b . toString ( 16 ) . padStart ( 2 , '0' ) )
7+ . join ( '' ) ;
8+ }
9+ 10+ // Load problem data & default settings on install
11+ chrome . runtime . onInstalled . addListener ( ( ) => {
12+ // Generate and store user ID if it doesn't exist
13+ chrome . storage . sync . get ( 'userId' , function ( items ) {
14+ if ( ! items . userId ) {
15+ const userId = getRandomToken ( ) ;
16+ chrome . storage . sync . set ( { userId : userId } ) ;
17+ }
18+ } ) ;
19+ 20+ // Load JSON file of problem data into storage
21+ const leetcodeProblems = chrome . runtime . getURL ( 'src/assets/data/problem_data.json' ) ;
22+ fetch ( leetcodeProblems )
23+ . then ( ( response ) => response . json ( ) )
24+ . then ( ( data ) => {
25+ chrome . storage . local . set ( { leetcodeProblems : data } ) ;
26+ } )
27+ . catch ( ( error ) => {
28+ console . error ( error ) ;
29+ } ) ;
30+ 31+ // Load problems by company JSON file into storage
32+ const companyProblems = chrome . runtime . getURL ( 'src/assets/data/problems_by_company.json' ) ;
33+ fetch ( companyProblems )
34+ . then ( ( response ) => response . json ( ) )
35+ . then ( ( data ) => {
36+ chrome . storage . local . set ( { companyProblems : data } ) ;
37+ } )
38+ . catch ( ( error ) => {
39+ console . error ( error ) ;
40+ } ) ;
41+ 42+ // Load default settings
43+ chrome . storage . local . set ( { fontSize : 12 } ) ; // Default to small display size
44+ chrome . storage . local . set ( { showExamples : true } ) ;
45+ chrome . storage . local . set ( { showDifficulty : true } ) ;
46+ chrome . storage . local . set ( { showRating : true } ) ;
47+ chrome . storage . local . set ( { showCompanyTags : true } ) ;
48+ // Set default theme to auto mode and default to dark
49+ chrome . storage . local . set ( { isDarkTheme : true } ) ; // Default to dark theme
50+ chrome . storage . local . set ( { themeMode : 'auto' } ) ;
51+ } ) ;
52+ 53+ chrome . runtime . onMessage . addListener ( ( request ) => {
54+ if ( request . action === 'openCompanyPage' ) {
55+ chrome . storage . local . set ( { clickedCompany : request . company } ) ;
56+ chrome . tabs . create ( {
57+ url : chrome . runtime . getURL ( 'src/problems-by-company/company.html' ) ,
58+ active : true ,
59+ } , function ( tab ) {
60+ // Remove the listener once the tab is loaded
61+ const listener = function ( tabId : number , changedProps : any ) {
62+ if ( tabId == tab . id && changedProps . status == 'complete' ) {
63+ chrome . tabs . sendMessage ( tabId , request ) ;
64+ chrome . tabs . onUpdated . removeListener ( listener ) ;
65+ }
66+ } ;
67+ // Attach the listener
68+ chrome . tabs . onUpdated . addListener ( listener ) ;
69+ } ) ;
70+ }
71+ } ) ;
72+ 173chrome . tabs . onUpdated . addListener ( ( tabId , changeInfo , tab ) => {
274 if ( changeInfo . status === 'complete' && tab . url ) {
375 const url = tab . url ;
476 let problemUrl = / ^ h t t p s : \/ \/ l e e t c o d e \. c o m \/ p r o b l e m s \/ .* \/ ? / ;
577 if ( url . match ( problemUrl ) ) {
6- chrome . storage . local . get ( [ 'currentLeetCodeProblemTitle' ] , ( result ) => {
78+ chrome . storage . local . get ( [ 'currentLeetCodeProblemTitle' , 'descriptionTabUpdated' , 'solutionsTabUpdated' ] , ( result ) => {
779 let lastTitle = result . currentLeetCodeProblemTitle || '' ;
8- 9- // If the title has changed, we need to update both tabs
80+ let descriptionTabUpdated = result . descriptionTabUpdated || false ;
81+ let solutionsTabUpdated = result . solutionsTabUpdated || false ;
1082 if ( tab . title !== lastTitle ) {
1183 chrome . storage . local . set ( {
1284 'currentLeetCodeProblemTitle' : tab . title ,
1385 'descriptionTabUpdated' : false ,
1486 'solutionsTabUpdated' : false
1587 } ) ;
88+ // If the title has changed, we reset both flags
89+ descriptionTabUpdated = false ;
90+ solutionsTabUpdated = false ;
91+ }
1692
17- // Force update both tabs when problem changes
18- chrome . tabs . sendMessage ( tabId , {
19- action : 'updateDescription' ,
20- title : tab . title || 'title' ,
21- forceUpdate : true
22- } ) ;
23- chrome . tabs . sendMessage ( tabId , {
24- action : 'updateSolutions' ,
25- title : tab . title || 'title' ,
26- forceUpdate : true
27- } ) ;
28- } else {
29- // If we're on the same problem but URL changed, update appropriate tab
30- let descriptionUrl = / ^ h t t p s : \/ \/ l e e t c o d e \. c o m \/ p r o b l e m s \/ .* \/ ( d e s c r i p t i o n \/ ) ? / ;
31- let solutionsUrl = / ^ h t t p s : \/ \/ l e e t c o d e \. c o m \/ p r o b l e m s \/ .* \/ s o l u t i o n s \/ ? / ;
32- 33- if ( url . match ( descriptionUrl ) ) {
34- chrome . tabs . sendMessage ( tabId , {
35- action : 'updateDescription' ,
36- title : tab . title || 'title'
37- } ) ;
38- }
39- 40- if ( url . match ( solutionsUrl ) ) {
41- chrome . tabs . sendMessage ( tabId , {
42- action : 'updateSolutions' ,
43- title : tab . title || 'title'
44- } ) ;
45- }
93+ let descriptionUrl = / ^ h t t p s : \/ \/ l e e t c o d e \. c o m \/ p r o b l e m s \/ .* \/ ( d e s c r i p t i o n \/ ) ? / ;
94+ if ( ! descriptionTabUpdated && url . match ( descriptionUrl ) ) {
95+ chrome . storage . local . set ( { 'descriptionTabUpdated' : true } ) ;
96+ chrome . tabs . sendMessage ( tabId , { action : 'updateDescription' , title : tab . title || 'title' } ) ;
97+ }
98+ 99+ let solutionsUrl = / ^ h t t p s : \/ \/ l e e t c o d e \. c o m \/ p r o b l e m s \/ .* \/ s o l u t i o n s \/ ? / ;
100+ if ( url . match ( solutionsUrl ) ) {
101+ chrome . storage . local . set ( { 'solutionsTabUpdated' : true } ) ;
102+ chrome . tabs . sendMessage ( tabId , { action : 'updateSolutions' , title : tab . title || 'title' } ) ;
46103 }
47104 } ) ;
48105 }
49106 }
50- } ) ;
51- 107+ } ) ;
0 commit comments