Skip to content

Commit 51170b5

Browse files
committed
fix(popup): reserve the pop-out window during the click
When the panel has not loaded yet, the toolbar opens a blank window inside the click and points it at the panel once the address is known, so a pop-up blocker allows it. The reserved window closes when no devtools server is found. Refs #342
1 parent f5dfc62 commit 51170b5

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

‎packages/devtools/src/__tests__/popup.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,32 @@ describe.sequential('devtools popup', () => {
804804
expect(fab.getAttribute('aria-label')).toBe('Toggle Pangular Inspector');
805805
});
806806

807+
it('reserves the window during the click when the panel has not loaded yet', async () => {
808+
sessionStorage.setItem('pangular-page-id', 'k3x9');
809+
const win = { ...fakeWindow(), location: { href: '' } };
810+
const open = vi.spyOn(window, 'open').mockReturnValue(win as unknown as Window);
811+
await loadPopup(true);
812+
popoutButton().click();
813+
// The blank window opens inside the click, before the address lookup.
814+
expect(open).toHaveBeenCalledTimes(1);
815+
expect(open.mock.calls[0][0]).toBe('about:blank');
816+
expect(open.mock.calls[0][1]).toBe('pangular-inspector-k3x9');
817+
const base = `${location.origin}/__devframes/pangular/`;
818+
await vi.waitFor(() =>
819+
expect(win.location.href).toBe(`${base}?baseURL=${encodeURIComponent(base)}&pageId=k3x9`),
820+
);
821+
expect(open).toHaveBeenCalledTimes(1);
822+
});
823+
824+
it('closes the reserved window when there is no devtools server', async () => {
825+
const win = { ...fakeWindow(), location: { href: '' } };
826+
vi.spyOn(window, 'open').mockReturnValue(win as unknown as Window);
827+
await loadPopup(false);
828+
popoutButton().click();
829+
await vi.waitFor(() => expect(notice().textContent).toContain('No devtools server found'));
830+
expect(win.close).toHaveBeenCalled();
831+
});
832+
807833
it('says so and stays in the page when the browser blocks the window', async () => {
808834
vi.spyOn(window, 'open').mockReturnValue(null);
809835
await loadPopup(true);

‎packages/devtools/src/popup.ts‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export function createDevtoolsPopup(options: { src?: string; hotkey?: string | f
334334
popoutBtn.textContent = '⧉';
335335
popoutBtn.title = 'Open in its own window';
336336
popoutBtn.setAttribute('aria-label', 'Open Pangular Inspector in its own window');
337-
popoutBtn.addEventListener('click', () => void popOut());
337+
popoutBtn.addEventListener('click', () => void popOut(true));
338338

339339
const closeBtn = document.createElement('button');
340340
closeBtn.type = 'button';
@@ -994,22 +994,42 @@ export function createDevtoolsPopup(options: { src?: string; hotkey?: string | f
994994
* closes it here. The launcher, the shortcut or the panel's **Back into the
995995
* page** command bring it back.
996996
*/
997-
async function popOut() {
997+
async function popOut(fromToolbar = false) {
998998
notice.textContent = '';
999-
// The frame's address is known once the panel loaded, which keeps the
1000-
// call inside the click, as a pop-up blocker wants.
1001-
const src = iframe.src && !iframe.hidden ? iframe.src : await resolveTarget();
999+
const known = iframe.src && !iframe.hidden ? iframe.src : undefined;
1000+
const width = Math.min(1200, Math.max(480, Math.round(window.outerWidth * 0.6) || 960));
1001+
const height = Math.min(900, Math.max(360, Math.round(window.outerHeight * 0.8) || 720));
1002+
const features = `popup,width=${width},height=${height}`;
1003+
const closeQuietly = (w: Window | null) => {
1004+
try {
1005+
if (w && !w.closed) w.close();
1006+
} catch {
1007+
// the window is already gone
1008+
}
1009+
};
1010+
let win: Window | null = null;
1011+
// A pop-up blocker only allows window.open inside the click, so a toolbar
1012+
// click that still has to look up the panel's address opens a blank window
1013+
// first and points it at the panel once the address is known.
1014+
if (fromToolbar && !known) {
1015+
try {
1016+
win = window.open('about:blank', popoutName(), features);
1017+
} catch {
1018+
win = null;
1019+
}
1020+
}
1021+
const src = known ?? (await resolveTarget());
10021022
if (src === null) {
1023+
closeQuietly(win);
10031024
notice.textContent = 'No devtools server found, so there is nothing to open.';
10041025
return;
10051026
}
1006-
const width = Math.min(1200, Math.max(480, Math.round(window.outerWidth * 0.6) || 960));
1007-
const height = Math.min(900, Math.max(360, Math.round(window.outerHeight * 0.8) || 720));
10081027
const url = popoutUrl(src);
1009-
let win: Window | null = null;
10101028
try {
1011-
win = window.open(url, popoutName(), `popup,width=${width},height=${height}`);
1029+
if (win) win.location.href = url;
1030+
else win = window.open(url, popoutName(), features);
10121031
} catch {
1032+
closeQuietly(win);
10131033
win = null;
10141034
}
10151035
if (!win) {

0 commit comments

Comments
 (0)