2016-08-12 22:01:30 +02:00
|
|
|
|
# session
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
2016-04-22 10:30:49 -07:00
|
|
|
|
> Manage browser sessions, cookies, cache, proxy settings, etc.
|
2016-04-21 15:35:29 -07:00
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
The `session` module can be used to create new `Session` objects.
|
|
|
|
|
|
|
|
|
|
You can also access the `session` of existing pages by using the `session`
|
2016-06-22 16:34:25 +09:00
|
|
|
|
property of [`WebContents`](web-contents.md), or from the `session` module.
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
|
|
|
|
```javascript
|
2016-07-25 18:39:25 -07:00
|
|
|
|
const {BrowserWindow} = require('electron')
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
let win = new BrowserWindow({width: 800, height: 600})
|
|
|
|
|
win.loadURL('http://github.com')
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
const ses = win.webContents.session
|
2016-07-25 18:39:25 -07:00
|
|
|
|
console.log(ses.getUserAgent())
|
2015-11-19 21:31:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Methods
|
|
|
|
|
|
|
|
|
|
The `session` module has the following methods:
|
|
|
|
|
|
2016-08-22 11:47:55 -07:00
|
|
|
|
### `session.fromPartition(partition[, options])`
|
2015-11-19 21:31:39 +08:00
|
|
|
|
|
|
|
|
|
* `partition` String
|
2016-07-12 22:21:49 +09:00
|
|
|
|
* `options` Object
|
|
|
|
|
* `cache` Boolean - Whether to enable cache.
|
2015-11-19 21:31:39 +08:00
|
|
|
|
|
2016-09-25 12:59:30 +13:00
|
|
|
|
Returns `Session` - A session instance from `partition` string. When there is an existing
|
2016-07-12 22:21:49 +09:00
|
|
|
|
`Session` with the same `partition`, it will be returned; othewise a new
|
|
|
|
|
`Session` instance will be created with `options`.
|
2015-11-19 21:31:39 +08:00
|
|
|
|
|
|
|
|
|
If `partition` starts with `persist:`, the page will use a persistent session
|
|
|
|
|
available to all pages in the app with the same `partition`. if there is no
|
|
|
|
|
`persist:` prefix, the page will use an in-memory session. If the `partition` is
|
|
|
|
|
empty then default session of the app will be returned.
|
|
|
|
|
|
2016-07-12 22:21:49 +09:00
|
|
|
|
To create a `Session` with `options`, you have to ensure the `Session` with the
|
|
|
|
|
`partition` has never been used before. There is no way to change the `options`
|
|
|
|
|
of an existing `Session` object.
|
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
## Properties
|
|
|
|
|
|
|
|
|
|
The `session` module has the following properties:
|
|
|
|
|
|
2016-08-22 11:47:55 -07:00
|
|
|
|
### `session.defaultSession`
|
2015-11-19 21:31:39 +08:00
|
|
|
|
|
2016-09-25 12:59:30 +13:00
|
|
|
|
A `Session` object, the default session object of the app.
|
2015-11-19 21:31:39 +08:00
|
|
|
|
|
|
|
|
|
## Class: Session
|
|
|
|
|
|
2016-08-22 14:11:03 -07:00
|
|
|
|
> Get and set properties of a session.
|
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
You can create a `Session` object in the `session` module:
|
|
|
|
|
|
|
|
|
|
```javascript
|
2016-07-25 18:39:25 -07:00
|
|
|
|
const {session} = require('electron')
|
|
|
|
|
const ses = session.fromPartition('persist:name')
|
|
|
|
|
console.log(ses.getUserAgent())
|
2015-08-20 15:17:53 +02:00
|
|
|
|
```
|
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
### Instance Events
|
2015-09-01 15:32:14 +05:30
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
The following events are available on instances of `Session`:
|
|
|
|
|
|
|
|
|
|
#### Event: 'will-download'
|
2015-09-01 15:32:14 +05:30
|
|
|
|
|
|
|
|
|
* `event` Event
|
2015-09-20 19:08:31 +08:00
|
|
|
|
* `item` [DownloadItem](download-item.md)
|
2015-09-01 20:16:28 +08:00
|
|
|
|
* `webContents` [WebContents](web-contents.md)
|
2015-09-01 15:32:14 +05:30
|
|
|
|
|
2015-11-17 21:36:36 +08:00
|
|
|
|
Emitted when Electron is about to download `item` in `webContents`.
|
2015-09-01 20:16:28 +08:00
|
|
|
|
|
2016-02-02 15:54:51 +05:30
|
|
|
|
Calling `event.preventDefault()` will cancel the download and `item` will not be
|
|
|
|
|
available from next tick of the process.
|
2015-09-01 15:32:14 +05:30
|
|
|
|
|
|
|
|
|
```javascript
|
2016-07-25 18:39:25 -07:00
|
|
|
|
const {session} = require('electron')
|
2016-05-04 11:59:02 -06:00
|
|
|
|
session.defaultSession.on('will-download', (event, item, webContents) => {
|
2016-07-25 18:39:25 -07:00
|
|
|
|
event.preventDefault()
|
2016-05-04 11:59:02 -06:00
|
|
|
|
require('request')(item.getURL(), (data) => {
|
2016-07-25 18:39:25 -07:00
|
|
|
|
require('fs').writeFileSync('/somewhere', data)
|
|
|
|
|
})
|
|
|
|
|
})
|
2015-09-01 15:32:14 +05:30
|
|
|
|
```
|
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
### Instance Methods
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
The following methods are available on instances of `Session`:
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
2016-01-14 15:01:54 +05:30
|
|
|
|
#### `ses.getCacheSize(callback)`
|
|
|
|
|
|
|
|
|
|
* `callback` Function
|
|
|
|
|
* `size` Integer - Cache size used in bytes.
|
|
|
|
|
|
|
|
|
|
Returns the session's current cache size.
|
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
#### `ses.clearCache(callback)`
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
|
|
|
|
* `callback` Function - Called when operation is done
|
|
|
|
|
|
|
|
|
|
Clears the session’s HTTP cache.
|
|
|
|
|
|
2016-07-15 14:13:56 +08:00
|
|
|
|
#### `ses.clearStorageData([options, callback])`
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
* `options` Object (optional)
|
2015-08-20 15:17:53 +02:00
|
|
|
|
* `origin` String - Should follow `window.location.origin`’s representation
|
2015-08-26 13:57:42 -07:00
|
|
|
|
`scheme://host:port`.
|
2016-09-28 18:28:44 +13:00
|
|
|
|
* `storages` String[] - The types of storages to clear, can contain:
|
2015-08-20 15:17:53 +02:00
|
|
|
|
`appcache`, `cookies`, `filesystem`, `indexdb`, `local storage`,
|
|
|
|
|
`shadercache`, `websql`, `serviceworkers`
|
2016-09-28 18:28:44 +13:00
|
|
|
|
* `quotas` String[] - The types of quotas to clear, can contain:
|
2015-08-26 13:57:42 -07:00
|
|
|
|
`temporary`, `persistent`, `syncable`.
|
2016-07-15 14:13:56 +08:00
|
|
|
|
* `callback` Function (optional) - Called when operation is done.
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
|
|
|
|
Clears the data of web storages.
|
|
|
|
|
|
2016-01-12 20:58:12 +05:30
|
|
|
|
#### `ses.flushStorageData()`
|
|
|
|
|
|
|
|
|
|
Writes any unwritten DOMStorage data to disk.
|
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
#### `ses.setProxy(config, callback)`
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
2016-01-11 03:55:56 +05:30
|
|
|
|
* `config` Object
|
|
|
|
|
* `pacScript` String - The URL associated with the PAC file.
|
|
|
|
|
* `proxyRules` String - Rules indicating which proxies to use.
|
2016-07-21 10:01:08 +05:30
|
|
|
|
* `proxyBypassRules` String - Rules indicating which URLs should
|
|
|
|
|
bypass the proxy settings.
|
2015-08-26 13:57:42 -07:00
|
|
|
|
* `callback` Function - Called when operation is done.
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
2016-01-11 22:45:34 +08:00
|
|
|
|
Sets the proxy settings.
|
|
|
|
|
|
2016-01-11 12:24:01 +05:30
|
|
|
|
When `pacScript` and `proxyRules` are provided together, the `proxyRules`
|
|
|
|
|
option is ignored and `pacScript` configuration is applied.
|
|
|
|
|
|
2016-06-08 11:03:01 +09:00
|
|
|
|
The `proxyRules` has to follow the rules below:
|
2016-01-11 22:45:34 +08:00
|
|
|
|
|
2015-08-20 15:17:53 +02:00
|
|
|
|
```
|
2016-01-11 22:45:34 +08:00
|
|
|
|
proxyRules = schemeProxies[";"<schemeProxies>]
|
|
|
|
|
schemeProxies = [<urlScheme>"="]<proxyURIList>
|
|
|
|
|
urlScheme = "http" | "https" | "ftp" | "socks"
|
|
|
|
|
proxyURIList = <proxyURL>[","<proxyURIList>]
|
|
|
|
|
proxyURL = [<proxyScheme>"://"]<proxyHost>[":"<proxyPort>]
|
2015-08-20 15:17:53 +02:00
|
|
|
|
```
|
|
|
|
|
|
2016-01-11 22:45:34 +08:00
|
|
|
|
For example:
|
2016-01-29 20:10:18 +01:00
|
|
|
|
|
2016-01-11 22:45:34 +08:00
|
|
|
|
* `http=foopy:80;ftp=foopy2` - Use HTTP proxy `foopy:80` for `http://` URLs, and
|
|
|
|
|
HTTP proxy `foopy2:80` for `ftp://` URLs.
|
|
|
|
|
* `foopy:80` - Use HTTP proxy `foopy:80` for all URLs.
|
|
|
|
|
* `foopy:80,bar,direct://` - Use HTTP proxy `foopy:80` for all URLs, failing
|
|
|
|
|
over to `bar` if `foopy:80` is unavailable, and after that using no proxy.
|
|
|
|
|
* `socks4://foopy` - Use SOCKS v4 proxy `foopy:1080` for all URLs.
|
|
|
|
|
* `http=foopy,socks5://bar.com` - Use HTTP proxy `foopy` for http URLs, and fail
|
|
|
|
|
over to the SOCKS5 proxy `bar.com` if `foopy` is unavailable.
|
|
|
|
|
* `http=foopy,direct://` - Use HTTP proxy `foopy` for http URLs, and use no
|
|
|
|
|
proxy if `foopy` is unavailable.
|
|
|
|
|
* `http=foopy;socks=foopy2` - Use HTTP proxy `foopy` for http URLs, and use
|
|
|
|
|
`socks4://foopy2` for all other URLs.
|
|
|
|
|
|
2016-07-21 10:01:08 +05:30
|
|
|
|
The `proxyBypassRules` is a comma separated list of rules described below:
|
|
|
|
|
|
|
|
|
|
* `[ URL_SCHEME "://" ] HOSTNAME_PATTERN [ ":" <port> ]`
|
|
|
|
|
|
|
|
|
|
Match all hostnames that match the pattern HOSTNAME_PATTERN.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
"foobar.com", "*foobar.com", "*.foobar.com", "*foobar.com:99",
|
|
|
|
|
"https://x.*.y.com:99"
|
|
|
|
|
|
|
|
|
|
* `"." HOSTNAME_SUFFIX_PATTERN [ ":" PORT ]`
|
|
|
|
|
|
|
|
|
|
Match a particular domain suffix.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
".google.com", ".com", "http://.google.com"
|
|
|
|
|
|
|
|
|
|
* `[ SCHEME "://" ] IP_LITERAL [ ":" PORT ]`
|
|
|
|
|
|
|
|
|
|
Match URLs which are IP address literals.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
"127.0.1", "[0:0::1]", "[::1]", "http://[::1]:99"
|
|
|
|
|
|
|
|
|
|
* `IP_LITERAL "/" PREFIX_LENGHT_IN_BITS`
|
|
|
|
|
|
|
|
|
|
Match any URL that is to an IP literal that falls between the
|
|
|
|
|
given range. IP range is specified using CIDR notation.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
"192.168.1.1/16", "fefe:13::abc/33".
|
|
|
|
|
|
|
|
|
|
* `<local>`
|
|
|
|
|
|
|
|
|
|
Match local addresses. The meaning of `<local>` is whether the
|
|
|
|
|
host matches one of: "127.0.0.1", "::1", "localhost".
|
|
|
|
|
|
2015-11-19 21:32:46 +08:00
|
|
|
|
### `ses.resolveProxy(url, callback)`
|
|
|
|
|
|
|
|
|
|
* `url` URL
|
|
|
|
|
* `callback` Function
|
|
|
|
|
|
|
|
|
|
Resolves the proxy information for `url`. The `callback` will be called with
|
|
|
|
|
`callback(proxy)` when the request is performed.
|
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
#### `ses.setDownloadPath(path)`
|
2015-08-20 15:17:53 +02:00
|
|
|
|
|
|
|
|
|
* `path` String - The download location
|
|
|
|
|
|
|
|
|
|
Sets download saving directory. By default, the download directory will be the
|
|
|
|
|
`Downloads` under the respective app folder.
|
2015-09-27 18:49:52 +05:30
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
#### `ses.enableNetworkEmulation(options)`
|
2015-09-27 18:49:52 +05:30
|
|
|
|
|
|
|
|
|
* `options` Object
|
2016-08-22 14:25:42 -07:00
|
|
|
|
* `offline` Boolean (optional) - Whether to emulate network outage. Defaults
|
|
|
|
|
to false.
|
|
|
|
|
* `latency` Double (optional) - RTT in ms. Defaults to 0 which will disable
|
|
|
|
|
latency throttling.
|
|
|
|
|
* `downloadThroughput` Double (optional) - Download rate in Bps. Defaults to 0
|
|
|
|
|
which will disable download throttling.
|
|
|
|
|
* `uploadThroughput` Double (optional) - Upload rate in Bps. Defaults to 0
|
|
|
|
|
which will disable upload throttling.
|
2015-09-27 18:49:52 +05:30
|
|
|
|
|
|
|
|
|
Emulates network with the given configuration for the `session`.
|
|
|
|
|
|
2015-09-28 12:52:50 +05:30
|
|
|
|
```javascript
|
|
|
|
|
// To emulate a GPRS connection with 50kbps throughput and 500 ms latency.
|
|
|
|
|
window.webContents.session.enableNetworkEmulation({
|
2016-07-25 18:39:25 -07:00
|
|
|
|
latency: 500,
|
|
|
|
|
downloadThroughput: 6400,
|
|
|
|
|
uploadThroughput: 6400
|
|
|
|
|
})
|
2015-09-28 12:52:50 +05:30
|
|
|
|
|
|
|
|
|
// To emulate a network outage.
|
2016-07-25 18:39:25 -07:00
|
|
|
|
window.webContents.session.enableNetworkEmulation({offline: true})
|
2015-09-28 12:52:50 +05:30
|
|
|
|
```
|
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
#### `ses.disableNetworkEmulation()`
|
2015-09-27 18:49:52 +05:30
|
|
|
|
|
|
|
|
|
Disables any network emulation already active for the `session`. Resets to
|
|
|
|
|
the original network configuration.
|
2015-11-18 11:35:26 +08:00
|
|
|
|
|
2015-11-19 21:31:39 +08:00
|
|
|
|
#### `ses.setCertificateVerifyProc(proc)`
|
2015-11-18 11:35:26 +08:00
|
|
|
|
|
|
|
|
|
* `proc` Function
|
|
|
|
|
|
|
|
|
|
Sets the certificate verify proc for `session`, the `proc` will be called with
|
|
|
|
|
`proc(hostname, certificate, callback)` whenever a server certificate
|
|
|
|
|
verification is requested. Calling `callback(true)` accepts the certificate,
|
|
|
|
|
calling `callback(false)` rejects it.
|
|
|
|
|
|
|
|
|
|
Calling `setCertificateVerifyProc(null)` will revert back to default certificate
|
|
|
|
|
verify proc.
|
|
|
|
|
|
|
|
|
|
```javascript
|
2016-07-25 18:39:25 -07:00
|
|
|
|
const {BrowserWindow} = require('electron')
|
|
|
|
|
let win = new BrowserWindow()
|
|
|
|
|
|
|
|
|
|
win.webContents.session.setCertificateVerifyProc((hostname, cert, callback) => {
|
|
|
|
|
callback(hostname === 'github.com')
|
|
|
|
|
})
|
2015-11-18 11:35:26 +08:00
|
|
|
|
```
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
2016-02-01 03:05:34 +05:30
|
|
|
|
#### `ses.setPermissionRequestHandler(handler)`
|
|
|
|
|
|
|
|
|
|
* `handler` Function
|
|
|
|
|
* `webContents` Object - [WebContents](web-contents.md) requesting the permission.
|
2016-04-22 23:23:15 +09:00
|
|
|
|
* `permission` String - Enum of 'media', 'geolocation', 'notifications', 'midiSysex',
|
2016-04-20 22:25:15 +05:30
|
|
|
|
'pointerLock', 'fullscreen', 'openExternal'.
|
2016-04-22 23:23:15 +09:00
|
|
|
|
* `callback` Function - Allow or deny the permission.
|
2016-02-01 03:05:34 +05:30
|
|
|
|
|
|
|
|
|
Sets the handler which can be used to respond to permission requests for the `session`.
|
2016-02-01 15:33:38 +05:30
|
|
|
|
Calling `callback(true)` will allow the permission and `callback(false)` will reject it.
|
2016-02-01 03:05:34 +05:30
|
|
|
|
|
|
|
|
|
```javascript
|
2016-07-25 18:39:25 -07:00
|
|
|
|
const {session} = require('electron')
|
|
|
|
|
session.fromPartition('some-partition').setPermissionRequestHandler((webContents, permission, callback) => {
|
|
|
|
|
if (webContents.getURL() === 'some-host' && permission === 'notifications') {
|
|
|
|
|
return callback(false) // denied.
|
2016-02-01 03:05:34 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-07-25 18:39:25 -07:00
|
|
|
|
callback(true)
|
|
|
|
|
})
|
2016-02-01 03:05:34 +05:30
|
|
|
|
```
|
|
|
|
|
|
2016-02-01 19:03:23 +05:30
|
|
|
|
#### `ses.clearHostResolverCache([callback])`
|
|
|
|
|
|
|
|
|
|
* `callback` Function (optional) - Called when operation is done.
|
|
|
|
|
|
|
|
|
|
Clears the host resolver cache.
|
|
|
|
|
|
2016-05-23 10:59:55 +05:30
|
|
|
|
#### `ses.allowNTLMCredentialsForDomains(domains)`
|
|
|
|
|
|
|
|
|
|
* `domains` String - A comma-seperated list of servers for which
|
|
|
|
|
integrated authentication is enabled.
|
|
|
|
|
|
|
|
|
|
Dynamically sets whether to always send credentials for HTTP NTLM or Negotiate
|
|
|
|
|
authentication.
|
|
|
|
|
|
|
|
|
|
```javascript
|
2016-07-25 18:39:25 -07:00
|
|
|
|
const {session} = require('electron')
|
2016-05-23 10:59:55 +05:30
|
|
|
|
// consider any url ending with `example.com`, `foobar.com`, `baz`
|
|
|
|
|
// for integrated authentication.
|
|
|
|
|
session.defaultSession.allowNTLMCredentialsForDomains('*example.com, *foobar.com, *baz')
|
|
|
|
|
|
|
|
|
|
// consider all urls for integrated authentication.
|
|
|
|
|
session.defaultSession.allowNTLMCredentialsForDomains('*')
|
|
|
|
|
```
|
|
|
|
|
|
2016-06-22 16:05:38 +09:00
|
|
|
|
#### `ses.setUserAgent(userAgent[, acceptLanguages])`
|
|
|
|
|
|
|
|
|
|
* `userAgent` String
|
|
|
|
|
* `acceptLanguages` String (optional)
|
|
|
|
|
|
|
|
|
|
Overrides the `userAgent` and `acceptLanguages` for this session.
|
|
|
|
|
|
|
|
|
|
The `acceptLanguages` must a comma separated ordered list of language codes, for
|
|
|
|
|
example `"en-US,fr,de,ko,zh-CN,ja"`.
|
|
|
|
|
|
|
|
|
|
This doesn't affect existing `WebContents`, and each `WebContents` can use
|
|
|
|
|
`webContents.setUserAgent` to override the session-wide user agent.
|
|
|
|
|
|
|
|
|
|
#### `ses.getUserAgent()`
|
|
|
|
|
|
2016-09-25 12:59:30 +13:00
|
|
|
|
Returns `String` - The user agent for this session.
|
2016-06-22 16:05:38 +09:00
|
|
|
|
|
2016-08-23 11:29:21 +05:30
|
|
|
|
#### `ses.getBlobData(identifier, callback)`
|
2016-08-23 06:29:44 +05:30
|
|
|
|
|
2016-08-31 05:38:32 +05:30
|
|
|
|
* `identifier` String - Valid UUID.
|
2016-08-23 06:29:44 +05:30
|
|
|
|
* `callback` Function
|
|
|
|
|
* `result` Buffer - Blob data.
|
|
|
|
|
|
2016-09-25 12:59:30 +13:00
|
|
|
|
Returns `Blob` - The blob data associated with the `identifier`.
|
2016-08-23 06:29:44 +05:30
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
### Instance Properties
|
|
|
|
|
|
|
|
|
|
The following properties are available on instances of `Session`:
|
|
|
|
|
|
|
|
|
|
#### `ses.cookies`
|
|
|
|
|
|
2016-09-25 12:59:30 +13:00
|
|
|
|
A Cookies object for this session.
|
2016-06-22 16:34:25 +09:00
|
|
|
|
|
2015-12-04 05:33:56 +05:30
|
|
|
|
#### `ses.webRequest`
|
|
|
|
|
|
2016-09-25 12:59:30 +13:00
|
|
|
|
A WebRequest object for this session.
|
2016-06-22 16:34:25 +09:00
|
|
|
|
|
|
|
|
|
#### `ses.protocol`
|
|
|
|
|
|
2016-09-25 12:59:30 +13:00
|
|
|
|
A Protocol object (an instance of [protocol](protocol.md) module) for this session.
|
2016-06-22 16:34:25 +09:00
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
const {app, session} = require('electron')
|
|
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
|
|
app.on('ready', function () {
|
2016-07-25 18:39:25 -07:00
|
|
|
|
const protocol = session.fromPartition('some-partition').protocol
|
2016-06-22 16:34:25 +09:00
|
|
|
|
protocol.registerFileProtocol('atom', function (request, callback) {
|
|
|
|
|
var url = request.url.substr(7)
|
2016-07-25 18:39:25 -07:00
|
|
|
|
callback({path: path.normalize(`${__dirname}/${url}`)})
|
2016-06-22 16:34:25 +09:00
|
|
|
|
}, function (error) {
|
2016-07-25 18:39:25 -07:00
|
|
|
|
if (error) console.error('Failed to register protocol')
|
2016-06-22 16:34:25 +09:00
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Class: Cookies
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
2016-08-22 14:11:03 -07:00
|
|
|
|
> Query and modify a session's cookies.
|
|
|
|
|
|
2016-08-22 21:45:25 -07:00
|
|
|
|
Instances of the `Cookies` class are accessed by using `cookies` property of
|
|
|
|
|
a `Session`.
|
2016-06-22 16:34:25 +09:00
|
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
|
|
```javascript
|
2016-07-25 18:39:25 -07:00
|
|
|
|
const {session} = require('electron')
|
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
// Query all cookies.
|
|
|
|
|
session.defaultSession.cookies.get({}, (error, cookies) => {
|
2016-07-25 18:39:25 -07:00
|
|
|
|
console.log(error, cookies)
|
2016-06-22 16:34:25 +09:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Query all cookies associated with a specific url.
|
|
|
|
|
session.defaultSession.cookies.get({url: 'http://www.github.com'}, (error, cookies) => {
|
2016-07-25 18:39:25 -07:00
|
|
|
|
console.log(error, cookies)
|
2016-06-22 16:34:25 +09:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Set a cookie with the given cookie data;
|
|
|
|
|
// may overwrite equivalent cookies if they exist.
|
|
|
|
|
const cookie = {url: 'http://www.github.com', name: 'dummy_name', value: 'dummy'}
|
|
|
|
|
session.defaultSession.cookies.set(cookie, (error) => {
|
2016-07-25 18:39:25 -07:00
|
|
|
|
if (error) console.error(error)
|
2016-06-22 16:34:25 +09:00
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Instance Methods
|
|
|
|
|
|
|
|
|
|
The following methods are available on instances of `Cookies`:
|
|
|
|
|
|
|
|
|
|
#### `cookies.get(filter, callback)`
|
|
|
|
|
|
|
|
|
|
* `filter` Object
|
|
|
|
|
* `url` String (optional) - Retrieves cookies which are associated with
|
|
|
|
|
`url`. Empty implies retrieving cookies of all urls.
|
|
|
|
|
* `name` String (optional) - Filters cookies by name.
|
|
|
|
|
* `domain` String (optional) - Retrieves cookies whose domains match or are
|
|
|
|
|
subdomains of `domains`
|
|
|
|
|
* `path` String (optional) - Retrieves cookies whose path matches `path`.
|
|
|
|
|
* `secure` Boolean (optional) - Filters cookies by their Secure property.
|
|
|
|
|
* `session` Boolean (optional) - Filters out session or persistent cookies.
|
|
|
|
|
* `callback` Function
|
|
|
|
|
|
|
|
|
|
Sends a request to get all cookies matching `details`, `callback` will be called
|
|
|
|
|
with `callback(error, cookies)` on complete.
|
|
|
|
|
|
|
|
|
|
`cookies` is an Array of `cookie` objects.
|
|
|
|
|
|
|
|
|
|
* `cookie` Object
|
|
|
|
|
* `name` String - The name of the cookie.
|
|
|
|
|
* `value` String - The value of the cookie.
|
|
|
|
|
* `domain` String - The domain of the cookie.
|
|
|
|
|
* `hostOnly` String - Whether the cookie is a host-only cookie.
|
|
|
|
|
* `path` String - The path of the cookie.
|
|
|
|
|
* `secure` Boolean - Whether the cookie is marked as secure.
|
|
|
|
|
* `httpOnly` Boolean - Whether the cookie is marked as HTTP only.
|
|
|
|
|
* `session` Boolean - Whether the cookie is a session cookie or a persistent
|
|
|
|
|
cookie with an expiration date.
|
|
|
|
|
* `expirationDate` Double (optional) - The expiration date of the cookie as
|
|
|
|
|
the number of seconds since the UNIX epoch. Not provided for session
|
|
|
|
|
cookies.
|
|
|
|
|
|
|
|
|
|
#### `cookies.set(details, callback)`
|
|
|
|
|
|
|
|
|
|
* `details` Object
|
|
|
|
|
* `url` String - The url to associate the cookie with.
|
|
|
|
|
* `name` String - The name of the cookie. Empty by default if omitted.
|
|
|
|
|
* `value` String - The value of the cookie. Empty by default if omitted.
|
|
|
|
|
* `domain` String - The domain of the cookie. Empty by default if omitted.
|
|
|
|
|
* `path` String - The path of the cookie. Empty by default if omitted.
|
|
|
|
|
* `secure` Boolean - Whether the cookie should be marked as Secure. Defaults to
|
|
|
|
|
false.
|
2016-06-28 18:16:34 -07:00
|
|
|
|
* `httpOnly` Boolean - Whether the cookie should be marked as HTTP only.
|
|
|
|
|
Defaults to false.
|
2016-06-22 16:34:25 +09:00
|
|
|
|
* `expirationDate` Double - The expiration date of the cookie as the number of
|
|
|
|
|
seconds since the UNIX epoch. If omitted then the cookie becomes a session
|
|
|
|
|
cookie and will not be retained between sessions.
|
|
|
|
|
* `callback` Function
|
|
|
|
|
|
|
|
|
|
Sets a cookie with `details`, `callback` will be called with `callback(error)`
|
|
|
|
|
on complete.
|
|
|
|
|
|
|
|
|
|
#### `cookies.remove(url, name, callback)`
|
|
|
|
|
|
|
|
|
|
* `url` String - The URL associated with the cookie.
|
|
|
|
|
* `name` String - The name of cookie to remove.
|
|
|
|
|
* `callback` Function
|
|
|
|
|
|
|
|
|
|
Removes the cookies matching `url` and `name`, `callback` will called with
|
|
|
|
|
`callback()` on complete.
|
|
|
|
|
|
|
|
|
|
## Class: WebRequest
|
|
|
|
|
|
2016-08-22 14:11:03 -07:00
|
|
|
|
> Intercept and modify the contents of a request at various stages of its lifetime.
|
|
|
|
|
|
2016-08-22 21:45:25 -07:00
|
|
|
|
Instances of the `WebRequest` class are accessed by using the `webRequest`
|
|
|
|
|
property of a `Session`.
|
2016-06-22 16:34:25 +09:00
|
|
|
|
|
2016-08-22 14:11:03 -07:00
|
|
|
|
The methods of `WebRequest` accept an optional `filter` and a `listener`. The
|
2016-06-22 16:34:25 +09:00
|
|
|
|
`listener` will be called with `listener(details)` when the API's event has
|
2016-08-22 14:11:03 -07:00
|
|
|
|
happened. The `details` object describes the request. Passing `null`
|
2016-06-22 16:34:25 +09:00
|
|
|
|
as `listener` will unsubscribe from the event.
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
2016-08-22 14:11:03 -07:00
|
|
|
|
The `filter` object has a `urls` property which is an Array of URL
|
2015-12-11 19:02:56 +08:00
|
|
|
|
patterns that will be used to filter out the requests that do not match the URL
|
|
|
|
|
patterns. If the `filter` is omitted then all requests will be matched.
|
|
|
|
|
|
2015-12-12 15:45:02 +08:00
|
|
|
|
For certain events the `listener` is passed with a `callback`, which should be
|
2016-08-22 14:11:03 -07:00
|
|
|
|
called with a `response` object when `listener` has done its work.
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
An example of adding `User-Agent` header for requests:
|
|
|
|
|
|
2015-12-04 05:33:56 +05:30
|
|
|
|
```javascript
|
2016-07-25 18:39:25 -07:00
|
|
|
|
const {session} = require('electron')
|
|
|
|
|
|
2015-12-04 05:33:56 +05:30
|
|
|
|
// Modify the user agent for all requests to the following urls.
|
2016-05-04 11:59:02 -06:00
|
|
|
|
const filter = {
|
2016-05-10 18:42:11 +09:00
|
|
|
|
urls: ['https://*.github.com/*', '*://electron.github.io']
|
2016-06-22 16:34:25 +09:00
|
|
|
|
}
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
2016-05-04 11:59:02 -06:00
|
|
|
|
session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
|
2016-07-25 18:39:25 -07:00
|
|
|
|
details.requestHeaders['User-Agent'] = 'MyAgent'
|
2016-06-22 16:34:25 +09:00
|
|
|
|
callback({cancel: false, requestHeaders: details.requestHeaders})
|
|
|
|
|
})
|
2015-12-04 05:33:56 +05:30
|
|
|
|
```
|
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
### Instance Methods
|
|
|
|
|
|
|
|
|
|
The following methods are available on instances of `WebRequest`:
|
|
|
|
|
|
|
|
|
|
#### `webRequest.onBeforeRequest([filter, ]listener)`
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
|
|
|
|
* `filter` Object
|
|
|
|
|
* `listener` Function
|
|
|
|
|
|
2015-12-12 15:45:02 +08:00
|
|
|
|
The `listener` will be called with `listener(details, callback)` when a request
|
|
|
|
|
is about to occur.
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
|
|
|
|
* `details` Object
|
2015-12-12 11:31:19 +08:00
|
|
|
|
* `id` Integer
|
2015-12-11 19:02:56 +08:00
|
|
|
|
* `url` String
|
|
|
|
|
* `method` String
|
|
|
|
|
* `resourceType` String
|
|
|
|
|
* `timestamp` Double
|
2016-01-26 13:58:21 +05:30
|
|
|
|
* `uploadData` Array (optional)
|
2016-01-28 18:59:07 +08:00
|
|
|
|
* `callback` Function
|
|
|
|
|
|
|
|
|
|
The `uploadData` is an array of `data` objects:
|
|
|
|
|
|
|
|
|
|
* `data` Object
|
|
|
|
|
* `bytes` Buffer - Content being sent.
|
|
|
|
|
* `file` String - Path of file being uploaded.
|
2016-08-31 05:38:32 +05:30
|
|
|
|
* `blobUUID` String - UUID of blob data. Use [ses.getBlobData](session.md#sesgetblobdataidentifier-callback) method
|
|
|
|
|
to retrieve the data.
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
2015-12-12 15:45:02 +08:00
|
|
|
|
The `callback` has to be called with an `response` object:
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
|
|
|
|
* `response` Object
|
2015-12-18 11:07:32 +08:00
|
|
|
|
* `cancel` Boolean (optional)
|
|
|
|
|
* `redirectURL` String (optional) - The original request is prevented from
|
2015-12-11 19:02:56 +08:00
|
|
|
|
being sent or completed, and is instead redirected to the given URL.
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
#### `webRequest.onBeforeSendHeaders([filter, ]listener)`
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
|
|
|
|
* `filter` Object
|
|
|
|
|
* `listener` Function
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
2015-12-12 15:45:02 +08:00
|
|
|
|
The `listener` will be called with `listener(details, callback)` before sending
|
|
|
|
|
an HTTP request, once the request headers are available. This may occur after a
|
|
|
|
|
TCP connection is made to the server, but before any http data is sent.
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
|
|
|
|
* `details` Object
|
2015-12-12 11:31:19 +08:00
|
|
|
|
* `id` Integer
|
2015-12-11 19:02:56 +08:00
|
|
|
|
* `url` String
|
|
|
|
|
* `method` String
|
|
|
|
|
* `resourceType` String
|
|
|
|
|
* `timestamp` Double
|
|
|
|
|
* `requestHeaders` Object
|
2016-01-28 18:59:07 +08:00
|
|
|
|
* `callback` Function
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
2015-12-12 15:45:02 +08:00
|
|
|
|
The `callback` has to be called with an `response` object:
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
|
|
|
|
* `response` Object
|
2015-12-18 11:07:32 +08:00
|
|
|
|
* `cancel` Boolean (optional)
|
|
|
|
|
* `requestHeaders` Object (optional) - When provided, request will be made
|
2015-12-11 19:02:56 +08:00
|
|
|
|
with these headers.
|
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
#### `webRequest.onSendHeaders([filter, ]listener)`
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
|
|
|
|
* `filter` Object
|
|
|
|
|
* `listener` Function
|
|
|
|
|
|
2015-12-11 19:02:56 +08:00
|
|
|
|
The `listener` will be called with `listener(details)` just before a request is
|
|
|
|
|
going to be sent to the server, modifications of previous `onBeforeSendHeaders`
|
|
|
|
|
response are visible by the time this listener is fired.
|
|
|
|
|
|
|
|
|
|
* `details` Object
|
2015-12-12 11:31:19 +08:00
|
|
|
|
* `id` Integer
|
2015-12-11 19:02:56 +08:00
|
|
|
|
* `url` String
|
|
|
|
|
* `method` String
|
|
|
|
|
* `resourceType` String
|
|
|
|
|
* `timestamp` Double
|
|
|
|
|
* `requestHeaders` Object
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
2016-08-26 23:47:04 -07:00
|
|
|
|
#### `webRequest.onHeadersReceived([filter, ]listener)`
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
|
|
|
|
* `filter` Object
|
|
|
|
|
* `listener` Function
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
2015-12-12 15:45:02 +08:00
|
|
|
|
The `listener` will be called with `listener(details, callback)` when HTTP
|
|
|
|
|
response headers of a request have been received.
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
|
|
|
|
* `details` Object
|
2015-12-12 11:31:19 +08:00
|
|
|
|
* `id` String
|
2015-12-11 19:02:56 +08:00
|
|
|
|
* `url` String
|
|
|
|
|
* `method` String
|
|
|
|
|
* `resourceType` String
|
|
|
|
|
* `timestamp` Double
|
|
|
|
|
* `statusLine` String
|
|
|
|
|
* `statusCode` Integer
|
|
|
|
|
* `responseHeaders` Object
|
2016-01-28 18:59:07 +08:00
|
|
|
|
* `callback` Function
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
2015-12-12 15:45:02 +08:00
|
|
|
|
The `callback` has to be called with an `response` object:
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
|
|
|
|
* `response` Object
|
|
|
|
|
* `cancel` Boolean
|
2015-12-18 11:07:32 +08:00
|
|
|
|
* `responseHeaders` Object (optional) - When provided, the server is assumed
|
2015-12-11 19:02:56 +08:00
|
|
|
|
to have responded with these headers.
|
2016-04-22 22:53:26 +09:00
|
|
|
|
* `statusLine` String (optional) - Should be provided when overriding
|
|
|
|
|
`responseHeaders` to change header status otherwise original response
|
|
|
|
|
header's status will be used.
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
#### `webRequest.onResponseStarted([filter, ]listener)`
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
|
|
|
|
* `filter` Object
|
|
|
|
|
* `listener` Function
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
|
|
|
|
The `listener` will be called with `listener(details)` when first byte of the
|
|
|
|
|
response body is received. For HTTP requests, this means that the status line
|
|
|
|
|
and response headers are available.
|
|
|
|
|
|
|
|
|
|
* `details` Object
|
2015-12-12 11:31:19 +08:00
|
|
|
|
* `id` Integer
|
2015-12-11 19:02:56 +08:00
|
|
|
|
* `url` String
|
|
|
|
|
* `method` String
|
|
|
|
|
* `resourceType` String
|
|
|
|
|
* `timestamp` Double
|
|
|
|
|
* `responseHeaders` Object
|
2016-04-22 23:30:16 +09:00
|
|
|
|
* `fromCache` Boolean - Indicates whether the response was fetched from disk
|
2015-12-11 19:02:56 +08:00
|
|
|
|
cache.
|
|
|
|
|
* `statusCode` Integer
|
|
|
|
|
* `statusLine` String
|
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
#### `webRequest.onBeforeRedirect([filter, ]listener)`
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
|
|
|
|
* `filter` Object
|
|
|
|
|
* `listener` Function
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
|
|
|
|
The `listener` will be called with `listener(details)` when a server initiated
|
|
|
|
|
redirect is about to occur.
|
|
|
|
|
|
|
|
|
|
* `details` Object
|
2015-12-12 11:31:19 +08:00
|
|
|
|
* `id` String
|
2015-12-11 19:02:56 +08:00
|
|
|
|
* `url` String
|
|
|
|
|
* `method` String
|
|
|
|
|
* `resourceType` String
|
|
|
|
|
* `timestamp` Double
|
|
|
|
|
* `redirectURL` String
|
|
|
|
|
* `statusCode` Integer
|
2015-12-18 11:07:32 +08:00
|
|
|
|
* `ip` String (optional) - The server IP address that the request was
|
2015-12-11 19:02:56 +08:00
|
|
|
|
actually sent to.
|
|
|
|
|
* `fromCache` Boolean
|
|
|
|
|
* `responseHeaders` Object
|
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
#### `webRequest.onCompleted([filter, ]listener)`
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
|
|
|
|
* `filter` Object
|
|
|
|
|
* `listener` Function
|
|
|
|
|
|
2015-12-11 19:02:56 +08:00
|
|
|
|
The `listener` will be called with `listener(details)` when a request is
|
|
|
|
|
completed.
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
2015-12-11 19:02:56 +08:00
|
|
|
|
* `details` Object
|
2015-12-12 11:31:19 +08:00
|
|
|
|
* `id` Integer
|
2015-12-11 19:02:56 +08:00
|
|
|
|
* `url` String
|
|
|
|
|
* `method` String
|
|
|
|
|
* `resourceType` String
|
|
|
|
|
* `timestamp` Double
|
|
|
|
|
* `responseHeaders` Object
|
|
|
|
|
* `fromCache` Boolean
|
|
|
|
|
* `statusCode` Integer
|
|
|
|
|
* `statusLine` String
|
|
|
|
|
|
2016-06-22 16:34:25 +09:00
|
|
|
|
#### `webRequest.onErrorOccurred([filter, ]listener)`
|
2015-12-04 05:33:56 +05:30
|
|
|
|
|
|
|
|
|
* `filter` Object
|
|
|
|
|
* `listener` Function
|
2015-12-11 19:02:56 +08:00
|
|
|
|
|
|
|
|
|
The `listener` will be called with `listener(details)` when an error occurs.
|
|
|
|
|
|
|
|
|
|
* `details` Object
|
2015-12-12 11:31:19 +08:00
|
|
|
|
* `id` Integer
|
2015-12-11 19:02:56 +08:00
|
|
|
|
* `url` String
|
|
|
|
|
* `method` String
|
|
|
|
|
* `resourceType` String
|
|
|
|
|
* `timestamp` Double
|
|
|
|
|
* `fromCache` Boolean
|
|
|
|
|
* `error` String - The error description.
|