From f78504515bc908bc7d0bee817b54f715b994cc01 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 27 May 2020 13:50:54 -0700 Subject: [PATCH] fix: handle asynchronous URL loading in bw proxy (#23776) --- docs/api/web-contents.md | 8 ++++---- lib/renderer/window-setup.ts | 6 +++++- spec-main/api-browser-window-spec.ts | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 50bcad75eea1..acd2ad448176 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -907,10 +907,10 @@ Returns `String` - The URL of the current web page. ```javascript const { BrowserWindow } = require('electron') let win = new BrowserWindow({ width: 800, height: 600 }) -win.loadURL('http://github.com') - -let currentURL = win.webContents.getURL() -console.log(currentURL) +win.loadURL('http://github.com').then(() => { + const currentURL = win.webContents.getURL() + console.log(currentURL) +}) ``` #### `contents.getTitle()` diff --git a/lib/renderer/window-setup.ts b/lib/renderer/window-setup.ts index 1029a7827187..87b58409a2e3 100644 --- a/lib/renderer/window-setup.ts +++ b/lib/renderer/window-setup.ts @@ -125,7 +125,11 @@ class LocationProxy { } private getGuestURL (): URL | null { - const urlString = this._invokeWebContentsMethodSync('getURL') as string; + const maybeURL = this._invokeWebContentsMethodSync('getURL') as string; + + // When there's no previous frame the url will be blank, so accountfor that here + // to prevent url parsing errors on an empty string. + const urlString = maybeURL !== '' ? maybeURL : 'about:blank'; try { return new URL(urlString); } catch (e) { diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 6aaa059ece53..c4c8b9bd3ce4 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -4128,7 +4128,7 @@ describe('BrowserWindow module', () => { window.postMessage({openedLocation}, '*') `); const [, data] = await p; - expect(data.pageContext.openedLocation).to.equal(''); + expect(data.pageContext.openedLocation).to.equal('about:blank'); }); });