docs: improve protocol.handle file examples (#42141)

* docs: improve protocol.handle file examples

Co-authored-by: David Sanders <dsanders11@ucsbalum.com>

* chore: fix lint

Co-authored-by: David Sanders <dsanders11@ucsbalum.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
This commit is contained in:
trop[bot] 2024-05-13 16:59:39 +02:00 committed by GitHub
parent fb54883e26
commit ea064a015a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,10 +9,14 @@ An example of implementing a protocol that has the same effect as the
```js ```js
const { app, protocol, net } = require('electron') const { app, protocol, net } = require('electron')
const path = require('node:path')
const url = require('node:url')
app.whenReady().then(() => { app.whenReady().then(() => {
protocol.handle('atom', (request) => protocol.handle('atom', (request) => {
net.fetch('file://' + request.url.slice('atom://'.length))) const filePath = request.url.slice('atom://'.length)
return net.fetch(url.pathToFileURL(path.join(__dirname, filePath)).toString())
})
}) })
``` ```
@ -42,7 +46,7 @@ app.whenReady().then(() => {
ses.protocol.handle('atom', (request) => { ses.protocol.handle('atom', (request) => {
const filePath = request.url.slice('atom://'.length) const filePath = request.url.slice('atom://'.length)
return net.fetch(url.pathToFileURL(path.join(__dirname, filePath)).toString()) return net.fetch(url.pathToFileURL(path.resolve(__dirname, filePath)).toString())
}) })
const mainWindow = new BrowserWindow({ webPreferences: { partition } }) const mainWindow = new BrowserWindow({ webPreferences: { partition } })