protocol Type
⚠ Process Availability: Main ✔ | Renderer ❌ | Utility ❌ | Exported ✔
Register a custom protocol and intercept existing protocol requests.Process: MainAn example of implementing a protocol that has the same effect as the file:// protocol:const { app, protocol, net } = require('electron')const path = require('node:path')const url = require('node:url')app.whenReady().then(() => { protocol.handle('atom', (request) => { const filePath = request.url.slice('atom://'.length) return net.fetch(url.pathToFileURL(path.join(__dirname, filePath)).toString()) })})> [!NOTE] All methods unless specified can only be used after the ready event of the app module gets emitted.### Using protocol with a custom partition or sessionA protocol is registered to a specific Electron session object. If you don't specify a session, then your protocol will be applied to the default session that Electron uses. However, if you define a partition or session on your browserWindow's webPreferences, then that window will use a different session and your custom protocol will not work if you just use electron.protocol.XXX.To have your custom protocol work in combination with a custom session, you need to register it to that session explicitly.const { app, BrowserWindow, net, protocol, session } = require('electron')const path = require('node:path')const url = require('node:url')app.whenReady().then(() => { const partition = 'persist:example' const ses = session.fromPartition(partition) ses.protocol.handle('atom', (request) => { const filePath = request.url.slice('atom://'.length) return net.fetch(url.pathToFileURL(path.resolve(__dirname, filePath)).toString()) }) const mainWindow = new BrowserWindow({ webPreferences: { partition } })})
Static members
| Static member |
Description
|
Register a protocol handler for scheme. Requests made to URLs with this scheme will delegate to this handler to determine
what response should be sent.Either a Response or a Promise |
|
Full Usage:
protocol.isProtocolHandled scheme
Parameters:
string
Returns: bool
Modifiers: inline |
Whether scheme is already handled.
|
Full Usage:
protocol.registerSchemesAsPrivileged customSchemes
Parameters:
CustomScheme[]
Modifiers: inline |
|
Full Usage:
protocol.unhandle scheme
Parameters:
string
Modifiers: inline |
Removes a protocol handler registered with protocol.handle.
|
fable-electron-docs-api