Skip to content

Commit

Permalink
Support search parameteres in the loadUrl method (#37)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
mhkeller and sindresorhus authored Jan 22, 2024
1 parent c307a57 commit 7ae7d43
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 4 deletions.
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ declare namespace electronServe {
/**
Load the index file in the window.
*/
type loadURL = (window: BrowserWindow) => Promise<void>;
type loadURL = (window: BrowserWindow, searchParameters?: Record<string, string> | URLSearchParams) => Promise<void>;
}

/**
Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
};
};
22 changes: 21 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,7 +40,7 @@ let mainWindow;

## API

### serve(options)
### loadUrl = serve(options)

#### options

Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/fixture-search-params.js
Original file line number Diff line number Diff line change
@@ -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'});
})();
9 changes: 9 additions & 0 deletions test/index 2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>🚀</h1>
</body>
</html>
15 changes: 15 additions & 0 deletions test/sub/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script>
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');
const foo = urlParams.get('foo');
document.getElementById('params').innerHTML = `${id}${foo}`;
</script>
</head>
<body>
<h1 id="params"></h1>
</body>
</html>
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 7ae7d43

Please sign in to comment.