From 7ae7d43ddadab0f69acd2cfa9729208130fe8b29 Mon Sep 17 00:00:00 2001 From: Michael Keller Date: Mon, 22 Jan 2024 12:29:29 -0500 Subject: [PATCH] Support search parameteres in the `loadUrl` method (#37) Co-authored-by: Sindre Sorhus --- index.d.ts | 5 ++++- index.js | 5 +++-- readme.md | 22 +++++++++++++++++++++- test/fixture-search-params.js | 15 +++++++++++++++ test/index 2.html | 9 +++++++++ test/sub/index.html | 15 +++++++++++++++ test/test.js | 12 ++++++++++++ 7 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 test/fixture-search-params.js create mode 100644 test/index 2.html create mode 100644 test/sub/index.html diff --git a/index.d.ts b/index.d.ts index f4723ba..058aeee 100644 --- a/index.d.ts +++ b/index.d.ts @@ -48,7 +48,7 @@ declare namespace electronServe { /** Load the index file in the window. */ - type loadURL = (window: BrowserWindow) => Promise; + type loadURL = (window: BrowserWindow, searchParameters?: Record | URLSearchParams) => Promise; } /** @@ -70,6 +70,9 @@ let mainWindow; await loadURL(mainWindow); + // Or optionally with search parameters. + await loadURL(mainWindow, {id: 4, foo: 'bar'}); + // The above is equivalent to this: await mainWindow.loadURL('app://-'); // The `-` is just the required hostname. diff --git a/index.js b/index.js index bdf5ca5..6c387be 100644 --- a/index.js +++ b/index.js @@ -73,7 +73,8 @@ module.exports = options => { session.protocol.registerFileProtocol(options.scheme, handler); }); - return async window_ => { - await window_.loadURL(`${options.scheme}://${options.hostname}`); + return async (window_, searchParameters) => { + const queryString = searchParameters ? '?' + new URLSearchParams(searchParameters).toString() : ''; + await window_.loadURL(`${options.scheme}://${options.hostname}${queryString}`); }; }; diff --git a/readme.md b/readme.md index fc0eafe..f89cb4c 100644 --- a/readme.md +++ b/readme.md @@ -29,6 +29,9 @@ let mainWindow; await loadURL(mainWindow); + // Or optionally with search parameters. + await loadURL(mainWindow, {id: 4, foo: 'bar'}); + // The above is equivalent to this: await mainWindow.loadURL('app://-'); // The `-` is just the required hostname @@ -37,7 +40,7 @@ let mainWindow; ## API -### serve(options) +### loadUrl = serve(options) #### options @@ -86,6 +89,23 @@ Default: [`electron.session.defaultSession`](https://electronjs.org/docs/api/ses The [partition](https://electronjs.org/docs/api/session#sessionfrompartitionpartition-options) the protocol should be installed to, if you're not using Electron's default partition. +### loadUrl(window, searchParameters?) + +The `serve` function returns a `loadUrl` function, which you use to serve your HTML file in that window. + +##### window + +*Required*\ +Type: `BrowserWindow` + +The window to load the file in. + +##### searchParameters + +Type: `object | URLSearchParams` + +Key value pairs or an [`URLSearchParams` instance](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) to set as the search parameters. + ## Related - [electron-util](https://github.com/sindresorhus/electron-util) - Useful utilities for developing Electron apps and modules diff --git a/test/fixture-search-params.js b/test/fixture-search-params.js new file mode 100644 index 0000000..a76464a --- /dev/null +++ b/test/fixture-search-params.js @@ -0,0 +1,15 @@ +'use strict'; +const {app, BrowserWindow} = require('electron'); +const {join} = require('path'); +const serve = require('..'); + +const loadUrl = serve({directory: join(__dirname, 'sub')}); + +let mainWindow; + +(async () => { + await app.whenReady(); + + mainWindow = new BrowserWindow(); + loadUrl(mainWindow, {id: 4, foo: 'bar'}); +})(); diff --git a/test/index 2.html b/test/index 2.html new file mode 100644 index 0000000..fc42923 --- /dev/null +++ b/test/index 2.html @@ -0,0 +1,9 @@ + + + + + + +

🚀

+ + diff --git a/test/sub/index.html b/test/sub/index.html new file mode 100644 index 0000000..46a4731 --- /dev/null +++ b/test/sub/index.html @@ -0,0 +1,15 @@ + + + + + + + +

+ + diff --git a/test/test.js b/test/test.js index 9d73e4e..d4a318b 100644 --- a/test/test.js +++ b/test/test.js @@ -77,3 +77,15 @@ test('serves directory custom file', async t => { await client.waitUntilTextExists('h1', '🦉', 5000); t.pass(); }); + +test('serves directory index with search params', async t => { + t.context.spectron = new Application({ + path: electron, + args: ['fixture-search-params.js'] + }); + await t.context.spectron.start(); + const {client} = t.context.spectron; + await client.waitUntilWindowLoaded(); + await client.waitUntilTextExists('h1', '4bar', 5000); + t.pass(); +});