@@ -187,43 +187,64 @@ function monitorIframeBackgroundColor() {
187
187
return ;
188
188
}
189
189
190
- // --- Prevent re-initialization if this gets called again ---
190
+ // Prevent re-init if this is called more than once
191
191
if ( iframe . dataset . themeSyncInit === "1" ) {
192
192
return ;
193
193
}
194
194
iframe . dataset . themeSyncInit = "1" ;
195
195
196
+ // Schedule update AFTER iframe click handlers complete (next tick)
197
+ const scheduleUpdate = ( ( ) => {
198
+ let scheduled = false ;
199
+ return ( ) => {
200
+ if ( scheduled ) {
201
+ return ;
202
+ }
203
+ scheduled = true ;
204
+ setTimeout ( ( ) => {
205
+ scheduled = false ;
206
+ updateThemeColorFromIframe ( ) ;
207
+ } , 0 ) ;
208
+ } ;
209
+ } ) ( ) ;
210
+
196
211
const onLoad = function ( ) {
197
212
// Update immediately on each navigation
198
213
updateThemeColorFromIframe ( ) ;
199
214
200
215
try {
201
- // Clean up a previous observer (from a previous document)
216
+ // Disconnect previous observer (for prior iframe document)
202
217
if ( iframeBodyObserver ) {
203
218
iframeBodyObserver . disconnect ( ) ;
204
219
iframeBodyObserver = null ;
205
220
}
206
221
207
- const iframeBody = iframe . contentWindow . document . body ;
222
+ const doc = iframe . contentWindow . document ;
223
+ const body = doc . body ;
208
224
209
- // Watch for inline style changes on the body
225
+ // Watch for style/class changes on the body
210
226
iframeBodyObserver = new MutationObserver ( ( mutationsList ) => {
211
227
for ( const mutation of mutationsList ) {
212
228
if (
213
229
mutation . type === "attributes" &&
214
- mutation . attributeName === "style"
230
+ ( mutation . attributeName === "style" ||
231
+ mutation . attributeName === "class" )
215
232
) {
216
- requestAnimationFrame ( updateThemeColorFromIframe ) ;
233
+ // Run AFTER any click handler mutations in the iframe
234
+ scheduleUpdate ( ) ;
217
235
}
218
236
}
219
237
} ) ;
220
238
221
- iframeBodyObserver . observe ( iframeBody , {
239
+ iframeBodyObserver . observe ( body , {
222
240
attributes : true ,
223
- attributeFilter : [ "style" ] ,
241
+ attributeFilter : [ "style" , "class" ] ,
224
242
subtree : false ,
225
243
} ) ;
226
244
245
+ doc . addEventListener ( "click" , scheduleUpdate , false ) ;
246
+ body . addEventListener ( "transitionend" , scheduleUpdate , false ) ;
247
+
227
248
console . log ( "MutationObserver started on iframe body." ) ;
228
249
} catch ( e ) {
229
250
console . error (
@@ -233,18 +254,18 @@ function monitorIframeBackgroundColor() {
233
254
}
234
255
} ;
235
256
236
- // add this as listener to iframe
257
+ // Use addEventListener to avoid overwriting onload
237
258
iframe . addEventListener ( "load" , onLoad ) ;
238
259
239
- // If the iframe is already loaded when this runs, run once now
260
+ // If the iframe is already loaded when this runs, fire once now
240
261
try {
241
262
if (
242
263
iframe . contentDocument &&
243
264
iframe . contentDocument . readyState === "complete"
244
265
) {
245
266
onLoad ( ) ;
246
267
}
247
- } catch ( e ) {
268
+ } catch {
248
269
// Ignore if no iframe content
249
270
}
250
271
}
@@ -301,6 +322,7 @@ function initializeAppUI() {
301
322
302
323
setTimeout ( ( ) => {
303
324
iframe . focus ( ) ;
325
+ iframe . dataset . themeSyncInit = 0 ;
304
326
} , 300 ) ;
305
327
} ) ;
306
328
} ) ;
0 commit comments