@@ -74,7 +74,9 @@ chrome.runtime.onMessage.addListener((request) => {
74
74
let lastState = {
75
75
problemPath : '' ,
76
76
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
78
80
} ;
79
81
80
82
chrome . tabs . onUpdated . addListener ( ( tabId , changeInfo , tab ) => {
@@ -92,55 +94,75 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
92
94
let currentView = url . includes ( '/solutions' ) ? 'solutions' : 'problem' ;
93
95
94
96
// Only trigger updates on actual page loads or problem changes
95
- const isPageLoad = changeInfo . status === 'complete' && ! changeInfo . url ;
97
+ const isPageLoad = changeInfo . status === 'complete' ;
96
98
const isProblemChange = problemPath !== lastState . problemPath ;
97
99
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' ) ) ;
103
122
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 ) {
106
130
console . log ( `State change detected - ${
107
131
isProblemChange ? 'New Problem' :
108
132
isViewChange ? 'View Changed' :
133
+ isActualRefresh ? 'Page Refresh' :
109
134
'Page Load'
110
135
} `) ;
111
136
112
137
// Update last state
113
138
lastState . problemPath = problemPath ;
114
139
lastState . view = currentView ;
115
140
lastState . lastPathname = pathname ;
141
+ lastState . lastUpdateTime = Date . now ( ) ;
116
142
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 ) => {
120
155
let descriptionTabUpdated = result . descriptionTabUpdated || false ;
121
156
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
- }
135
157
136
158
// Only update description if we're in problem view and not updated
137
159
if ( ! descriptionTabUpdated && currentView === 'problem' ) {
138
160
chrome . storage . local . set ( { 'descriptionTabUpdated' : true } ) ;
139
161
chrome . tabs . sendMessage ( tabId , { action : 'updateDescription' , title : tab . title || 'title' } ) ;
140
162
}
141
163
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' ) {
144
166
chrome . storage . local . set ( { 'solutionsTabUpdated' : true } ) ;
145
167
chrome . tabs . sendMessage ( tabId , { action : 'updateSolutions' , title : tab . title || 'title' } ) ;
146
168
}
0 commit comments