@@ -74,7 +74,9 @@ chrome.runtime.onMessage.addListener((request) => {
7474let lastState = {
7575 problemPath : '' ,
7676 view : '' , // 'problem' or 'solutions'
77- lastPathname : '' // Track full pathname to detect real navigation
77+ lastPathname : '' , // Track full pathname to detect real navigation
78+ lastUrl : '' , // Track full URL to detect refreshes
79+ lastUpdateTime : 0 // Track time of last update to prevent rapid re-triggers
7880} ;
7981
8082chrome . tabs . onUpdated . addListener ( ( tabId , changeInfo , tab ) => {
@@ -92,55 +94,75 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
9294 let currentView = url . includes ( '/solutions' ) ? 'solutions' : 'problem' ;
9395
9496 // Only trigger updates on actual page loads or problem changes
95- const isPageLoad = changeInfo . status === 'complete' && ! changeInfo . url ;
97+ const isPageLoad = changeInfo . status === 'complete' ;
9698 const isProblemChange = problemPath !== lastState . problemPath ;
9799 const isViewChange = currentView !== lastState . view ;
98- const isRealNavigation = pathname !== lastState . lastPathname &&
99- ! pathname . includes ( 'playground' ) &&
100- ! pathname . includes ( 'editor' ) &&
101- ! pathname . includes ( 'interpret-solution' ) &&
102- ! pathname . includes ( 'submissions' ) ;
100+ 101+ // Check if this is a video navigation within solutions
102+ const isInternalSolutionsNavigation =
103+ currentView === 'solutions' &&
104+ lastState . view === 'solutions' &&
105+ problemPath === lastState . problemPath ;
106+ 107+ // Detect actual page refresh vs internal navigation
108+ const isActualRefresh =
109+ url === lastState . lastUrl &&
110+ isPageLoad &&
111+ changeInfo . url === undefined &&
112+ ! isInternalSolutionsNavigation &&
113+ Date . now ( ) - lastState . lastUpdateTime > 1000 ;
114+ 115+ const isRealNavigation =
116+ ! isInternalSolutionsNavigation &&
117+ ( ( pathname !== lastState . lastPathname || isViewChange ) &&
118+ ! pathname . includes ( 'playground' ) &&
119+ ! pathname . includes ( 'editor' ) &&
120+ ! pathname . includes ( 'interpret-solution' ) &&
121+ ! pathname . includes ( 'submissions' ) ) ;
103122
104- // Only update if there's a real navigation or problem change
105- if ( ( isProblemChange || ( isViewChange && isRealNavigation ) || isPageLoad ) && problemPath ) {
123+ // Update last URL and time
124+ if ( ! isInternalSolutionsNavigation ) {
125+ lastState . lastUrl = url ;
126+ }
127+ 128+ // Only update if there's a real navigation, problem change, or actual refresh
129+ if ( ( isProblemChange || ( isViewChange && isRealNavigation ) || isActualRefresh ) && problemPath ) {
106130 console . log ( `State change detected - ${
107131 isProblemChange ? 'New Problem' :
108132 isViewChange ? 'View Changed' :
133+ isActualRefresh ? 'Page Refresh' :
109134 'Page Load'
110135 } `) ;
111136
112137 // Update last state
113138 lastState . problemPath = problemPath ;
114139 lastState . view = currentView ;
115140 lastState . lastPathname = pathname ;
141+ lastState . lastUpdateTime = Date . now ( ) ;
116142
117- chrome . storage . local . get ( [ 'currentLeetCodeProblem' , 'currentLeetCodeProblemTitle' , 'descriptionTabUpdated' , 'solutionsTabUpdated' ] , ( result ) => {
118- let lastProblem = result . currentLeetCodeProblem || '' ;
119- let lastTitle = result . currentLeetCodeProblemTitle || '' ;
143+ // Reset flags only on problem change or actual refresh
144+ if ( isProblemChange || isActualRefresh ) {
145+ chrome . storage . local . set ( {
146+ 'currentLeetCodeProblem' : problemPath ,
147+ 'currentLeetCodeProblemTitle' : tab . title ,
148+ 'descriptionTabUpdated' : false ,
149+ 'solutionsTabUpdated' : false
150+ } ) ;
151+ }
152+ 153+ // Get current state
154+ chrome . storage . local . get ( [ 'descriptionTabUpdated' , 'solutionsTabUpdated' ] , ( result ) => {
120155 let descriptionTabUpdated = result . descriptionTabUpdated || false ;
121156 let solutionsTabUpdated = result . solutionsTabUpdated || false ;
122- 123- // Reset flags on problem change or page load
124- if ( isProblemChange || ( isPageLoad && ! descriptionTabUpdated && ! solutionsTabUpdated ) ) {
125- console . log ( 'Updating problem state:' , problemPath ) ;
126- chrome . storage . local . set ( {
127- 'currentLeetCodeProblem' : problemPath ,
128- 'currentLeetCodeProblemTitle' : tab . title ,
129- 'descriptionTabUpdated' : false ,
130- 'solutionsTabUpdated' : false
131- } ) ;
132- descriptionTabUpdated = false ;
133- solutionsTabUpdated = false ;
134- }
135157
136158 // Only update description if we're in problem view and not updated
137159 if ( ! descriptionTabUpdated && currentView === 'problem' ) {
138160 chrome . storage . local . set ( { 'descriptionTabUpdated' : true } ) ;
139161 chrome . tabs . sendMessage ( tabId , { action : 'updateDescription' , title : tab . title || 'title' } ) ;
140162 }
141163
142- // Only update solutions if we're in solutions view and not updated
143- if ( ! solutionsTabUpdated && currentView === 'solutions' ) {
164+ // Only update solutions if we're in solutions view and not updated or if view just changed to solutions
165+ if ( ( ! solutionsTabUpdated || ( isViewChange && currentView === 'solutions' ) ) && currentView === 'solutions' ) {
144166 chrome . storage . local . set ( { 'solutionsTabUpdated' : true } ) ;
145167 chrome . tabs . sendMessage ( tabId , { action : 'updateSolutions' , title : tab . title || 'title' } ) ;
146168 }
0 commit comments