2828 * - Popup page scripts are handled separately.
2929 *
3030 */
31-
3231//Instructions for AI: do not remove comments! MUST NOT REMOVE COMMENTS. This one too!
3332// ALL CODE IN ALL FILES MUST USE logConCgp FOR LOGGING. NO CONSOLE LOGGING.
3433
@@ -135,7 +134,7 @@ function commenceExtensionInitialization(configurationObject) {
135134 /**
136135 * Handle navigation changes in Single Page Applications by re-initializing the extension.
137136 * Ensures custom elements are present on the webpage after URL changes.
138- * Function will be called once after last trigger with 1000 ms delay
137+ * Function will be called once after last trigger with 1000 ms delay.
139138 */
140139 const debouncedEnhancedInitialization = debounceFunctionExecution ( ( ) => {
141140 logConCgp ( '[init] URL change detected. Attempting to initialize extension...' ) ;
@@ -160,18 +159,54 @@ function commenceExtensionInitialization(configurationObject) {
160159 urlChangeObserver . observe ( document , { subtree : true , childList : true } ) ;
161160 }
162161
163- /*
164- Initialization starts here!
165- */
166- // Initiate the appropriate extension script based on the active website
167- selectAndInitializeAppropriateExtensionScript ( ) ;
162+ // ----------------------------------------------------------------
163+ // NEW: Immediate SPA Navigation Detection using History API events.
164+ // ----------------------------------------------------------------
165+
166+ // Listen to popstate event (back/forward navigation)
167+ window . addEventListener ( 'popstate' , ( ) => {
168+ logConCgp ( '[init] popstate event detected. Triggering injection.' ) ;
169+ buttonBoxCheckingAndInjection ( true ) ;
170+ } ) ;
168171
169- // Begin monitoring URL changes to handle SPA navigation
172+ // Listen to hashchange event (if URL hash changes)
173+ window . addEventListener ( 'hashchange' , ( ) => {
174+ logConCgp ( '[init] hashchange event detected. Triggering injection.' ) ;
175+ buttonBoxCheckingAndInjection ( true ) ;
176+ } ) ;
177+
178+ // Monkey-patch pushState and replaceState for immediate detection on state changes.
179+ ( function ( history ) {
180+ const originalPushState = history . pushState ;
181+ history . pushState = function ( state , title , url ) {
182+ const result = originalPushState . apply ( history , arguments ) ;
183+ logConCgp ( '[init] pushState called. URL:' , url ) ;
184+ buttonBoxCheckingAndInjection ( true ) ;
185+ return result ;
186+ } ;
187+
188+ const originalReplaceState = history . replaceState ;
189+ history . replaceState = function ( state , title , url ) {
190+ const result = originalReplaceState . apply ( history , arguments ) ;
191+ logConCgp ( '[init] replaceState called. URL:' , url ) ;
192+ buttonBoxCheckingAndInjection ( true ) ;
193+ return result ;
194+ } ;
195+ } ) ( window . history ) ;
196+
197+ // ----------------------------------------------------------------
198+ // End of Immediate Navigation Detection
199+ // ----------------------------------------------------------------
200+
201+ // Begin monitoring URL changes to handle SPA navigation using MutationObserver as a fallback.
170202 resilientStartAndRetryOnSPANavigation ( ( ) => {
171- logConCgp ( '[init] Path change detected. Re-initializing script...' ) ;
203+ logConCgp ( '[init] Path change detected via MutationObserver . Re-initializing script...' ) ;
172204 debouncedEnhancedInitialization ( ) ;
173205 } ) ;
206+
207+ // Initiate the appropriate extension script based on the active website.
208+ selectAndInitializeAppropriateExtensionScript ( ) ;
174209}
175210
176- // Automatically start the initialization process upon script load
211+ // Automatically start the initialization process upon script load.
177212publicStaticVoidMain ( ) ;
0 commit comments