Skip to content

Commit adbda5b

Browse files
authored
Minor fixes
1 parent 5320a04 commit adbda5b

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

static/app_script.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,43 +187,64 @@ function monitorIframeBackgroundColor() {
187187
return;
188188
}
189189

190-
// --- Prevent re-initialization if this gets called again ---
190+
// Prevent re-init if this is called more than once
191191
if (iframe.dataset.themeSyncInit === "1") {
192192
return;
193193
}
194194
iframe.dataset.themeSyncInit = "1";
195195

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+
196211
const onLoad = function () {
197212
// Update immediately on each navigation
198213
updateThemeColorFromIframe();
199214

200215
try {
201-
// Clean up a previous observer (from a previous document)
216+
// Disconnect previous observer (for prior iframe document)
202217
if (iframeBodyObserver) {
203218
iframeBodyObserver.disconnect();
204219
iframeBodyObserver = null;
205220
}
206221

207-
const iframeBody = iframe.contentWindow.document.body;
222+
const doc = iframe.contentWindow.document;
223+
const body = doc.body;
208224

209-
// Watch for inline style changes on the body
225+
// Watch for style/class changes on the body
210226
iframeBodyObserver = new MutationObserver((mutationsList) => {
211227
for (const mutation of mutationsList) {
212228
if (
213229
mutation.type === "attributes" &&
214-
mutation.attributeName === "style"
230+
(mutation.attributeName === "style" ||
231+
mutation.attributeName === "class")
215232
) {
216-
requestAnimationFrame(updateThemeColorFromIframe);
233+
// Run AFTER any click handler mutations in the iframe
234+
scheduleUpdate();
217235
}
218236
}
219237
});
220238

221-
iframeBodyObserver.observe(iframeBody, {
239+
iframeBodyObserver.observe(body, {
222240
attributes: true,
223-
attributeFilter: ["style"],
241+
attributeFilter: ["style", "class"],
224242
subtree: false,
225243
});
226244

245+
doc.addEventListener("click", scheduleUpdate, false);
246+
body.addEventListener("transitionend", scheduleUpdate, false);
247+
227248
console.log("MutationObserver started on iframe body.");
228249
} catch (e) {
229250
console.error(
@@ -233,18 +254,18 @@ function monitorIframeBackgroundColor() {
233254
}
234255
};
235256

236-
// add this as listener to iframe
257+
// Use addEventListener to avoid overwriting onload
237258
iframe.addEventListener("load", onLoad);
238259

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
240261
try {
241262
if (
242263
iframe.contentDocument &&
243264
iframe.contentDocument.readyState === "complete"
244265
) {
245266
onLoad();
246267
}
247-
} catch (e) {
268+
} catch {
248269
// Ignore if no iframe content
249270
}
250271
}
@@ -301,6 +322,7 @@ function initializeAppUI() {
301322

302323
setTimeout(() => {
303324
iframe.focus();
325+
iframe.dataset.themeSyncInit = 0;
304326
}, 300);
305327
});
306328
});

0 commit comments

Comments
 (0)