May 25, 2021 Electron
protocol
module can register a custom protocol or use an existing protocol.
For example, use
a protocol
file://
to :
const electron = require('electron');
const app = electron.app;
const path = require('path');
app.on('ready', function() {
var protocol = electron.protocol;
protocol.registerFileProtocol('atom', function(request, callback) {
var url = request.url.substr(7);
callback({path: path.normalize(__dirname + '/' + url)});
}, function (error) {
if (error)
console.error('Failed to register protocol')
});
});
Note: This
module is only
app
after the
ready event of the
ready
module is triggered.
protocol
module has the following methods:
protocol.registerStandardSchemes(schemes)
schemes
Array - Register a custom scheme as a standard scheme.
A standard
scheme
the generic
URI syntax
standard of RFC 3986.
This
file:
filesystem:
.
protocol.registerServiceWorkerSchemes(schemes)
schemes
Array - Register a custom scheme to handle service workers.
protocol.registerFileProtocol(scheme, handler[, completion])
scheme
String
handler
Function
completion
Function (optional)
Register a protocol to send a response file.
When a request is initiated through this
completion(null)
handler(request, callback)
is used to
scheme
call
handler
completion
.
request
Object
url
String
referrer
String
method
String
uploadData
Array (optional)
callback
Function
uploadData
is
an array
data
objects:
data
Object
bytes
Buffer - What is being sent.
file
String - Uploaded file path.
In order to
process
callback
to be called using
a file path or an object with a
path
parameter,
such as a
callback(filePath)
or
callback({path: filePath})
.
When callback is
called
callback
you
can specify a number or an
error
parameter to identify
request
failure.
You can use
the error number to refer
to the net error list
.
The
scheme
is
registered as
http:
which is different from
protocol resolution that follows the "generic URI syntax" rule, such as
file:
protocol.registerStandardSchemes
create a standard scheme.
protocol.registerBufferProtocol(scheme, handler[, completion])
scheme
String
handler
Function
completion
Function (optional)
Register a
scheme
to
send a response
Buffer
.
This method is
similar to
registerFileProtocol
in that unless you
call callback with
a
Buffer
object, or an object with
data
mimeType
and
charset
callback
.
Example:
protocol.registerBufferProtocol('atom', function(request, callback) {
callback({mimeType: 'text/html', data: new Buffer('<h5>Response</h5>')});
}, function (error) {
if (error)
console.error('Failed to register protocol')
});
protocol.registerStringProtocol(scheme, handler[, completion])
scheme
String
handler
Function
completion
Function (optional)
Register a
scheme
to
send a response
String
.
This method is
similar to
registerFileProtocol
in that unless you call
String
with a String
object,
or an object with
data
mimeType
and
charset
callback
.
protocol.registerHttpProtocol(scheme, handler[, completion])
scheme
String
handler
Function
completion
Function (optional)
Register a
scheme
to send HTTP requests in response.
This method is
similar to
registerFileProtocol
in that it is called
callback using a
redirectRequest
object,
or an object with the
url
method
referrer
uploadData
session
callback
.
redirectRequest
Object
url
String
method
String
session
Object (optional)
uploadData
Object (optional)
The default for this HTTP request uses the current session
.
If you want to use a different session value, you should set the
session
to
null
.
Post requests
should contain
uploadData
uploadData
object
contentType
String - Content MIME type.
data
String - What is being sent.
protocol.unregisterProtocol(scheme[, completion])
scheme
String
completion
Function (optional)
Sign out
of the custom
scheme
.
protocol.isProtocolHandled(scheme, callback)
scheme
String
callback
Function
Callback will be called with
callback
that identifies whether
scheme
already exists.
protocol.interceptFileProtocol(scheme, handler[, completion])
scheme
String
handler
Function
completion
Function (optional)
Block
scheme
protocol and
handler
as the new handle to the protocol to send the response file.
protocol.interceptStringProtocol(scheme, handler[, completion])
scheme
String
handler
Function
completion
Function (optional)
Block
scheme
protocol and use
handler
as
the new handle to the protocol to send a response
String
.
protocol.interceptBufferProtocol(scheme, handler[, completion])
scheme
String
handler
Function
completion
Function (optional)
Block
scheme
protocol and use
handler
as
the new handle to the protocol to send a
Buffer
.
protocol.interceptHttpProtocol(scheme, handler[, completion])
scheme
String
handler
Function
completion
Function (optional)
Block
scheme
protocol and
handler
as the new handle to the protocol to send a new response to http requests.
Intercepts
scheme
protocol and uses
handler
as the protocol's new handler which sends a new HTTP request as a response.
protocol.uninterceptProtocol(scheme[, completion])
scheme
String
completion
Function
scheme
blocking of the scheme and processes it with its original handle.