Skip to content

Commit d7ce700

Browse files
committed
Make DebugKit more toolbar robust with JS frameworks like HTMX, Boost
- Persist toolbar iframe by placing it in document.documentElement instead of document.body. - Handle potential error by logging it to the console - Cleanups
1 parent 976cffe commit d7ce700

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

webroot/js/inject-iframe.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,48 @@ if (elem) {
1010
let bodyOverflow;
1111

1212
const onMessage = (event) => {
13-
if (event.data === 'collapse') {
14-
iframe.height = 40;
15-
iframe.width = 40;
16-
doc.body.style.overflow = bodyOverflow;
17-
return;
18-
}
19-
if (event.data === 'toolbar') {
20-
iframe.height = 40;
21-
iframe.width = '100%';
22-
doc.body.style.overflow = bodyOverflow;
23-
return;
24-
}
25-
if (event.data === 'expand') {
26-
iframe.width = '100%';
27-
iframe.height = '100%';
28-
doc.body.style.overflow = 'hidden';
29-
}
30-
if (event.data === 'error') {
31-
iframe.width = '40%';
32-
iframe.height = '40%';
33-
doc.body.style.overflow = bodyOverflow;
13+
const actions = {
14+
collapse: { height: 40, width: 40, overflow: bodyOverflow },
15+
toolbar: { height: 40, width: '100%', overflow: bodyOverflow },
16+
expand: { height: '100%', width: '100%', overflow: 'hidden' },
17+
error: { height: '40%', width: '40%', overflow: bodyOverflow }
18+
};
19+
20+
const action = actions[event.data];
21+
if (action) {
22+
iframe.height = action.height;
23+
iframe.width = action.width;
24+
doc.body.style.overflow = action.overflow;
3425
}
3526
};
3627

3728
const onReady = () => {
3829
if (!win.debugKitId) {
3930
return;
4031
}
41-
const { body } = doc;
4232

43-
// Cannot use css text, because of CSP compatibility.
44-
iframe = doc.createElement('iframe');
45-
iframe.style.position = 'fixed';
46-
iframe.style.bottom = 0;
47-
iframe.style.right = 0;
48-
iframe.style.border = 0;
49-
iframe.style.outline = 0;
50-
iframe.style.overflow = 'hidden';
51-
iframe.style.zIndex = 99999;
52-
iframe.height = 40;
53-
iframe.width = 40;
54-
iframe.src = `${window.debugKitBaseUrl}debug-kit/toolbar/${window.debugKitId}`;
33+
// Store body overflow before creating iframe
34+
bodyOverflow = doc.body.style.overflow;
35+
36+
// Create and configure iframe in one go
37+
iframe = Object.assign(doc.createElement('iframe'), {
38+
src: `${window.debugKitBaseUrl}debug-kit/toolbar/${window.debugKitId}`,
39+
height: 40,
40+
width: 40
41+
});
5542

56-
body.appendChild(iframe);
57-
bodyOverflow = body.style.overflow;
43+
// Set styles efficiently
44+
Object.assign(iframe.style, {
45+
position: 'fixed',
46+
bottom: 0,
47+
right: 0,
48+
border: 0,
49+
outline: 0,
50+
overflow: 'hidden',
51+
zIndex: 99999
52+
});
5853

54+
doc.documentElement.appendChild(iframe);
5955
window.addEventListener('message', onMessage, false);
6056
};
6157

@@ -69,7 +65,11 @@ if (elem) {
6965
url: this._arguments && this._arguments[1],
7066
type: this.getResponseHeader('Content-Type'),
7167
};
72-
iframe.contentWindow.postMessage(`ajax-completed$$${JSON.stringify(params)}`, window.location.origin);
68+
if (iframe && iframe.contentWindow) {
69+
iframe.contentWindow.postMessage(`ajax-completed$$${JSON.stringify(params)}`, window.location.origin);
70+
} else {
71+
console.error('DebugKit: iframe not available for XHR logging');
72+
}
7373
}
7474
if (original) {
7575
return original.apply(this, [].slice.call(arguments));

0 commit comments

Comments
 (0)