protocol
进程:
实现与 file://
协议具有相同效果的协议的示例:
注意: 除了指定的方法, 其他方法只能在 app
模块的 ready
事件被触发后使用。
A 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 { session, app, protocol } = require('electron')
const path = require('path')
app.whenReady().then(() => {
const partition = 'persist:example'
const ses = session.fromPartition(partition)
ses.protocol.registerFileProtocol('atom', (request, callback) => {
const url = request.url.substr(7)
callback({ path: path.normalize(`${__dirname}/${url}`) })
})
mainWindow = new BrowserWindow({ webPreferences: { partition } })
})
方法
protocol
模块具有以下方法:
customSchemes
注意. 此方法只能在 app
的 ready
事件触发前调用,且只能调用一次
Registers the scheme
as standard, secure, bypasses content security policy for resources, allows registering ServiceWorker, supports fetch API, and streaming video/audio. Specify a privilege with the value of true
to enable the capability.
An example of registering a privileged scheme, that bypasses Content Security Policy:
标准scheme遵循 RFC 3986 所设定的 URI泛型语法 。 例如, http
和 https
是标准协议, 而 file
不是。
Registering a scheme as standard allows relative and absolute resources to be resolved correctly when served. 否则, 该协议将表现为 file
协议, 而且,这种文件协议将不能解析相对路径。
例如, 当您使用自定义协议加载以下内容时,如果你不将其注册为标准scheme, 图片将不会被加载, 因为非标准scheme无法识别相对 路径:
<body>
<img src='test.png'>
</body>
注册一个scheme作为标准scheme将允许其通过访问文件。 否则, 渲染器将会因为该scheme,而抛出一个安全性错误。
By default web storage apis (localStorage, sessionStorage, webSQL, indexedDB, cookies) are disabled for non standard schemes. So in general if you want to register a custom protocol to replace the http
protocol, you have to register it as a standard scheme.
Protocols that use streams (http and stream protocols) should set stream: true
. The <video>
and <audio>
HTML elements expect protocols to buffer their responses by default. The stream
flag configures those elements to correctly expect streaming responses.
handler
Functionrequest
ProtocolRequestcallback
Functionresponse
(String | )
Returns Boolean
- Whether the protocol was successfully registered
要处理 request
, 应当使用文件的路径或具有 path
属性的对象来调用 callback
。例如:callback(filePath)
或 callback({ path: filePath })
. The filePath
must be an absolute path.
By default the scheme
is treated like http:
, which is parsed differently from protocols that follow the “generic URI syntax” like file:
.
scheme
Stringhandler
Functionrequest
ProtocolRequestcallback
Functionresponse
(Buffer | )
Returns Boolean
- Whether the protocol was successfully registered
注册一个 scheme
协议, 将 Buffer
作为响应发送
The usage is the same with registerFileProtocol
, except that the callback
should be called with either a Buffer
object or an object that has the data
property.
示例:
scheme
Stringhandler
Functionrequest
ProtocolRequestcallback
Functionresponse
(String | )
Returns Boolean
- Whether the protocol was successfully registered
注册一个 scheme
协议, 将 String
作为响应发送
The usage is the same with registerFileProtocol
, except that the callback
should be called with either a String
or an object that has the data
property.
scheme
Stringhandler
Functionrequest
ProtocolRequestcallback
Functionresponse
ProtocolResponse
Returns Boolean
- Whether the protocol was successfully registered
注册一个 scheme
协议, 将 HTTP 请求作为响应发送
The usage is the same with registerFileProtocol
, except that the callback
should be called with an object that has the url
property.
scheme
Stringhandler
Functionrequest
callback
Functionresponse
(ReadableStream | ProtocolResponse)
Returns Boolean
- Whether the protocol was successfully registered
Registers a protocol of scheme
that will send a stream as a response.
The usage is the same with registerFileProtocol
, except that the callback
should be called with either a object or an object that has the data
property.
示例:
const { protocol } = require('electron')
const { PassThrough } = require('stream')
function createStream (text) {
const rv = new PassThrough() // PassThrough is also a Readable stream
rv.push(text)
rv.push(null)
return rv
}
protocol.registerStreamProtocol('atom', (request, callback) => {
statusCode: 200,
headers: {
'content-type': 'text/html'
},
data: createStream('<h5>Response</h5>')
})
})
scheme
String
Returns Boolean
- Whether the protocol was successfully unregistered
取消对自定义scheme
的注册
scheme
String
Returns Boolean
- Whether scheme
is already registered.
scheme
Stringhandler
Functionrequest
ProtocolRequestcallback
Functionresponse
(String | )
Returns Boolean
- Whether the protocol was successfully intercepted
终止 scheme
协议, 并将 handler
作为该protocol新的处理方式,即返回一个file。
scheme
Stringhandler
Functionrequest
ProtocolRequestcallback
Functionresponse
(String | )
Returns Boolean
- Whether the protocol was successfully intercepted
终止 scheme
协议, 并将 handler
作为该protocol新的处理方式,即返回一个String
。
scheme
Stringhandler
Functionrequest
ProtocolRequestcallback
Functionresponse
(Buffer | )
Returns Boolean
- Whether the protocol was successfully intercepted
终止 scheme
协议, 并将 handler
作为该protocol新的处理方式,即返回一个Buffer
。
scheme
Stringhandler
Functionrequest
ProtocolRequestcallback
Functionresponse
Returns Boolean
- Whether the protocol was successfully intercepted
终止 scheme
协议, 并将 handler
作为该protocol新的处理方式,即返回一个新 HTTP 请求。
scheme
Stringhandler
Functionrequest
ProtocolRequestcallback
Functionresponse
(ReadableStream | )
Returns Boolean
- Whether the protocol was successfully intercepted
它与 registerStreamProtocol
方法相同, 不过它是用来替换现有的protocol处理方式。
scheme
String
Returns Boolean
- Whether the protocol was successfully unintercepted
移除为 scheme
安装的拦截器,并还原其原始处理方式。
Returns Boolean
- Whether scheme
is already intercepted.