docs: When to use protocol.registerStandardSchemes

This commit is contained in:
Cheng Zhao 2016-05-09 10:20:08 +09:00
parent 566bb1f708
commit 4dc431043f

View file

@ -6,22 +6,21 @@ An example of implementing a protocol that has the same effect as the
`file://` protocol: `file://` protocol:
```javascript ```javascript
const electron = require('electron'); const {app, protocol} = require('electron')
const { app, protocol } = electron; const path = require('path')
const path = require('path');
app.on('ready', function() { app.on('ready', function () {
protocol.registerFileProtocol('atom', function(request, callback) { protocol.registerFileProtocol('atom', function (request, callback) {
const url = request.url.substr(7); const url = request.url.substr(7)
callback({path: path.normalize(__dirname + '/' + url)}); callback({path: path.normalize(__dirname + '/' + url)})
}, function (error) { }, function (error) {
if (error) if (error)
console.error('Failed to register protocol') console.error('Failed to register protocol')
}); })
}); })
``` ```
**Note:** All methods unless specified can only be used after the `ready` **Note:** All methods unless specified can only be used after the `ready` event
event in the `app` module is emitted. of the `app` module gets emitted.
## Methods ## Methods
@ -31,13 +30,36 @@ The `protocol` module has the following methods:
* `schemes` Array - Custom schemes to be registered as standard schemes. * `schemes` Array - Custom schemes to be registered as standard schemes.
A standard `scheme` adheres to what RFC 3986 calls A standard scheme adheres to what RFC 3986 calls [generic URI
[generic URI syntax](https://tools.ietf.org/html/rfc3986#section-3). This syntax](https://tools.ietf.org/html/rfc3986#section-3). For example `http` and
includes `file:`, `filesystem:`, `http` etc. Registering a scheme as standard, will `https` are standard schemes, while `file` is not.
allow relative and absolute resources to be resolved correctly when served.
**Note:** This method can only be used before the `ready` event in the Registering a scheme as standard, will allow relative and absolute resources to
`app` module is emitted. be resolved correctly when served. Otherwise the scheme will behave like the
`file` protocol, but without the ability to resolve relative URLs.
For example when you load following page with custom protocol without
registering it as standard scheme, the image will not be loaded because
non-standard schemes can not recognize relative URLs:
```html
<body>
<img src='test.png'>
</body>
```
So if you want to register a custom protocol to replace the `http` protocol, you
have to register it as standard scheme:
```javascript
protocol.registerStandardSchemes(['atom'])
app.on('ready', function () {
protocol.registerHttpProtocol('atom', ...)
})
```
**Note:** This method can only be used before the `ready` event of the `app`
module gets emitted.
### `protocol.registerServiceWorkerSchemes(schemes)` ### `protocol.registerServiceWorkerSchemes(schemes)`