- protocol
- 方法
- protocol.registerStandardSchemes(schemes[, options])
- protocol.registerServiceWorkerSchemes(schemes)
- protocol.registerFileProtocol(scheme, handler[, completion])
- protocol.registerBufferProtocol(scheme, handler[, completion])
- protocol.registerStringProtocol(scheme, handler[, completion])
- protocol.registerHttpProtocol(scheme, handler[, completion])
- protocol.registerStreamProtocol(scheme, handler[, completion])
- protocol.unregisterProtocol(scheme[, completion])
- protocol.isProtocolHandled(scheme, callback)
- protocol.interceptFileProtocol(scheme, handler[, completion])
- protocol.interceptStringProtocol(scheme, handler[, completion])
- protocol.interceptBufferProtocol(scheme, handler[, completion])
- protocol.interceptHttpProtocol(scheme, handler[, completion])
- protocol.interceptStreamProtocol(scheme, handler[, completion])
- protocol.uninterceptProtocol(scheme[, completion])
- 方法
protocol
注册自定义协议并拦截基于现有协议的请求。
线程:主线程
实现与 file:// 协议具有相同效果的协议的示例:
const { app, protocol } = require('electron')const path = require('path')app.on('ready', () => {protocol.registerFileProtocol('atom', (request, callback) => {const url = request.url.substr(7)callback({ path: path.normalize(`${__dirname}/${url}`) })}, (error) => {if (error) console.error('Failed to register protocol')})})
注意: 除了指定的方法, 其他方法只能在 app 模块的 ready 事件被触发后使用。
方法
protocol 模块具有以下方法:
protocol.registerStandardSchemes(schemes[, options])
schemesString[] - 注册 schemes 为标准schemes。选项Object (可选)secureBoolean (可选) -true注册scheme为安全scheme。 默认false.
标准scheme遵循 RFC 3986 所设定的 URI泛型语法 。 例如,http和https是标准协议, 而file不是。
将一个scheme注册为标准scheme, 将保证相对和绝对资源在使用时能够得到正确的解析。 否则, 该协议将表现为 file 协议, 而且,这种文件协议将不能解析相对路径。
例如, 当您使用自定义协议加载以下内容时,如果你不将其注册为标准scheme, 图片将不会被加载, 因为非标准scheme无法识别相对 路径:
<body><img src='test.png'></body>
注册一个scheme作为标准scheme将允许其通过FileSystem 接口访问文件。 否则, 渲染器将会因为该scheme,而抛出一个安全性错误。
默认情况下web storage apis (localStorage, sessionStorage, webSQL, indexedDB, cookies) 被禁止访问非标准schemes。 所以一般来说如果你想注册一个 自定义协议来替换http协议,您必须将其注册为标准scheme:
const { app, protocol } = require('electron')protocol.registerStandardSchemes(['atom'])app.on('ready', () => {protocol.registerHttpProtocol('atom', '...')})
注意: 此方法只能在 app 模块的 ready 事件被发出之前使用。
protocol.registerServiceWorkerSchemes(schemes)
schemesString[] - 将自定义 schemes注册为处理线程服务的标准schemes。
protocol.registerFileProtocol(scheme, handler[, completion])
schemeStringhandlerFunction - 回调函数requestObjecturlStringreferrerStringmethodStringuploadDataUploadData[]
callbackFunction - 回调函数filePathString (可选)
completionFunction (可选)errorError
注册一个scheme协议, 将该文件作为响应发送 当要使用scheme创建request时, 将使用handler(request, callback)来调用handler。completion将在scheme注册成功时通过completion(null)调用,失败时通过completion(error)调用。
要处理 request, 应当使用文件的路径或具有 path 属性的对象来调用 callback。例如:callback(filePath)或 callback({ path: filePath }).
当 callback 被调用后,并且没有带着数字或 error 属性的对象时, request将会失败, 并且带有你指定的 error错误号。 更多的错误号信息,您可以查阅网络错误列表.
默认情况下, scheme 与 http:,类似, 它的分析方式不同于遵循 "generic URI syntax" 的协议(例如 file:), 所以您可能需要调用protocol.registerStandardSchemes 以使您的方案作为标准方案处理。
protocol.registerBufferProtocol(scheme, handler[, completion])
schemeStringhandlerFunction - 回调函数requestObjecturlStringreferrerStringmethodStringuploadDataUploadData[]
callbackFunction - 回调函数buffer(Buffer | MimeTypedBuffer) (可选)
completionFunction (可选)errorError
注册一个scheme协议, 将Buffer作为响应发送
该用法与 registerFileProtocol 相同, 只是callback 会被Buffer对象或者带有data,mimeType和 charset属性的对象调用。
示例:
const { protocol } = require('electron')protocol.registerBufferProtocol('atom', (request, callback) => {callback({ mimeType: 'text/html', data: Buffer.from('<h5>Response</h5>') })}, (error) => {if (error) console.error('Failed to register protocol')})
protocol.registerStringProtocol(scheme, handler[, completion])
schemeStringhandlerFunction - 回调函数requestObjecturlStringreferrerStringmethodStringuploadDataUploadData[]
callbackFunction - 回调函数dataString (可选)
completionFunction (可选)errorError
注册一个scheme协议, 将String作为响应发送
该用法与 registerFileProtocol 相同, 只是callback 会被String对象或者带有data,mimeType和 charset属性的对象调用。
protocol.registerHttpProtocol(scheme, handler[, completion])
schemeStringhandlerFunction - 回调函数requestObjecturlStringheadersObjectreferrerStringmethodStringuploadDataUploadData[]
callbackFunction - 回调函数redirectRequestObjecturlStringmethodStringsessionObject (可选)uploadDataObject (可选)contentTypeString - 内容的MIME类型。dataString - 发送内容。
completionFunction (可选)errorError
注册一个scheme协议, 将 HTTP 请求作为响应发送
该用法与 registerFileProtocol 相同, 只是callback 会被redirectRequest对象或者带有url, method, referrer, uploadData 和 session 属性的对象调用。
默认情况下, HTTP 请求会重复使用当前的 session。如果希望请求具有不同的session, 则应将 session设置为 null.
对于 POST 请求, 必须提供 uploadData 对象。
protocol.registerStreamProtocol(scheme, handler[, completion])
schemeStringhandlerFunctionrequestObjecturlStringheadersObjectreferrerStringmethodStringuploadDataUploadData[]
callbackFunctionstream(ReadableStream | StreamProtocolResponse) (可选)
completionFunction (可选)errorError
注册一个scheme协议, 将Readable作为响应发送
该用法类似于 register{Any}Protocol ,只是callback 会被Readable对象或者带有data, statusCode 和 headers 属性的对象调用。
示例:
const { protocol } = require('electron')const { PassThrough } = require('stream')function createStream (text) {const rv = new PassThrough() // PassThrough is also a Readable streamrv.push(text)rv.push(null)return rv}protocol.registerStreamProtocol('atom', (request, callback) => {callback({statusCode: 200,headers: {'content-type': 'text/html'},data: createStream('<h5>Response</h5>')})}, (error) => {if (error) console.error('Failed to register protocol')})
可以传递任何可读取流 API 的对象(data/end/error 事件)。以下是如何返回文件的方法示例:
const { protocol } = require('electron')const fs = require('fs')protocol.registerStreamProtocol('atom', (request, callback) => {callback(fs.createReadStream('index.html'))}, (error) => {if (error) console.error('Failed to register protocol')})
protocol.unregisterProtocol(scheme[, completion])
schemeStringcompletionFunction (可选)errorError
取消对自定义scheme的注册
protocol.isProtocolHandled(scheme, callback)
schemeStringcallbackFunction - 回调函数errorErrorcallback会被调用,带有布尔值,表示是否已经有scheme的处理程序。
protocol.interceptFileProtocol(scheme, handler[, completion])
schemeStringhandlerFunction - 回调函数requestObjecturlStringreferrerStringmethodStringuploadDataUploadData[]
callbackFunction - 回调函数filePathString
completionFunction (可选)errorError
终止scheme协议, 并将handler作为该protocol新的处理方式,即返回一个file。
protocol.interceptStringProtocol(scheme, handler[, completion])
schemeStringhandlerFunction - 回调函数requestObjecturlStringreferrerStringmethodStringuploadDataUploadData[]
callbackFunction - 回调函数dataString (可选)
completionFunction (可选)errorError
终止scheme协议, 并将handler作为该protocol新的处理方式,即返回一个String。
protocol.interceptBufferProtocol(scheme, handler[, completion])
schemeStringhandlerFunction - 回调函数requestObjecturlStringreferrerStringmethodStringuploadDataUploadData[]
callbackFunction - 回调函数bufferBuffer (可选)
completionFunction (可选)errorError
终止scheme协议, 并将handler作为该protocol新的处理方式,即返回一个Buffer。
protocol.interceptHttpProtocol(scheme, handler[, completion])
schemeStringhandlerFunctionrequestObjecturlStringheadersObjectreferrerStringmethodStringuploadDataUploadData[]
callbackFunctionredirectRequestObjecturlStringmethodStringsessionObject (可选)uploadDataObject (可选)contentTypeString - 内容的MIME类型。dataString - 发送内容。
completionFunction (可选)errorError
终止scheme协议, 并将handler作为该protocol新的处理方式,即返回一个新 HTTP 请求。
protocol.interceptStreamProtocol(scheme, handler[, completion])
schemeStringhandlerFunctionrequestObjecturlStringheadersObjectreferrerStringmethodStringuploadDataUploadData[]
callbackFunction - 回调函数stream(ReadableStream | StreamProtocolResponse ) (可选)
completionFunction (可选)errorError
它与registerStreamProtocol方法相同, 不过它是用来替换现有的protocol处理方式。
protocol.uninterceptProtocol(scheme[, completion])
schemeStringcompletionFunction (可选)errorError
移除为scheme安装的拦截器,并还原其原始处理方式。
