Merge pull request #1981 from atom/cookies-api

Implement Cookies api
This commit is contained in:
Cheng Zhao 2015-06-23 10:04:42 +08:00
commit 19963bfcd1
9 changed files with 702 additions and 0 deletions

View file

@ -1024,3 +1024,90 @@ app.on('ready', function() {
is different from the handlers on the main process.
2. There is no way to send synchronous messages from the main process to a
renderer process, because it would be very easy to cause dead locks.
## Class: WebContents.session.cookies
The `cookies` gives you ability to query and modify cookies, an example is:
```javascipt
var BrowserWindow = require('browser-window');
var win = new BrowserWindow({ width: 800, height: 600 });
win.loadUrl('https://github.com');
win.webContents.on('did-finish-load', function() {
// Query all cookies.
win.webContents.session.cookies.get({}, function(error, cookies) {
if (error) throw error;
console.log(cookies);
});
// Query all cookies that are associated with a specific url.
win.webContents.session.cookies.get({ url : "http://www.github.com" },
function(error, cookies) {
if (error) throw error;
console.log(cookies);
});
// Set a cookie with the given cookie data;
// may overwrite equivalent cookies if they exist.
win.webContents.session.cookies.set(
{ url : "http://www.github.com", name : "dummy_name", value : "dummy"},
function(error, cookies) {
if (error) throw error;
console.log(cookies);
});
});
```
### WebContents.session.cookies.get(details, callback)
* `details` Object
* `url` String - Retrieves cookies which are associated with `url`.
Empty imples retrieving cookies of all urls.
* `name` String - Filters cookies by name
* `domain` String - Retrieves cookies whose domains match or are subdomains of `domains`
* `path` String - Retrieves cookies whose path matches `path`
* `secure` Boolean - Filters cookies by their Secure property
* `session` Boolean - Filters out session or persistent cookies.
* `callback` Function - function(error, cookies)
* `error` Error
* `cookies` Array - 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
* `host_only` 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 (typically HTTPS)
* `http_only` Boolean - Whether the cookie is marked as HttpOnly
* `session` Boolean - Whether the cookie is a session cookie or a persistent
* cookie with an expiration date.
* `expirationDate` Double - (Option) The expiration date of the cookie as
the number of seconds since the UNIX epoch. Not provided for session cookies.
### WebContents.session.cookies.set(details, callback)
* `details` Object
* `url` String - Retrieves cookies which are associated with `url`
* `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.
* `session` Boolean - Whether the cookie should be marked as HttpOnly. Defaults to false.
* `expirationDate` Double - The expiration date of the cookie as the number of
seconds since the UNIX epoch. If omitted, the cookie becomes a session cookie.
* `callback` Function - function(error)
* `error` Error
### WebContents.session.cookies.remove(details, callback)
* `details` Object
* `url` String - The URL associated with the cookie
* `name` String - The name of cookie to remove
* `callback` Function - function(error)
* `error` Error