与浏览器交互-Cookies操作与管理

683 阅读7分钟

内容

  1. 清单

    1. h3Name
  2. 范例

    1. h3Name
  3. API 参考: chrome.cookies

    1. 属性

      1. propertyName
    2. 方法

      1. get
      2. getAll
      3. getAllCookieStores
      4. remove
      5. set
    3. 事件

      1. onChanged
    4. 类型

      1. Cookie
      2. CookieStore

For information on how to use experimental APIs, see the chrome.experimental.* APIs page.

Cookies

清单

要使用cookies API, 你必须在你的清单中声明"cookies"权限,以及任何你希望cookie可以访问的主机权限。例如:

{
  "name": "My extension",
  ...
  **"permissions": [
    "cookies",
    "*://*.google.com"
  ]**,
  ...
}

范例

你可以在examples/api/cookies目录下找到一个使用cookies API的简单范例。对于其他的例子和查看源代码的帮助,参见 示例

API 参考: chrome.cookies

属性

getLastError

chrome.extensionlastError

方法

get

void chrome.cookies.get(, object details, function callback)

Undocumented.

获取一个cookie的信息。如果对于给定的URL有多个cookie存在,将返回对应于最长路径的cookie。对于路径长度相同的cookies,将返回最早创建的cookie。

参数

details ( optional enumerated Type array of object对象 )

Undocumented.

用于识别所收到的cookie的详细信息。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

url ( optional enumerated Type array of string字符串 )

Undocumented.

与所收到的cookie关联的URL。这个参数可以是一个完整的URL,这时候所有跟随在URL上的数据(比如查询字符串)将被忽略。如果清单文件中没有设置这个URL对应的主机权限,那么这个API调用会失败。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

name ( optional enumerated Type array of string字符串 )

Undocumented.

收到的cookie名字。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

storeId ( optional enumerated Type array of string可选,字符串 )

Undocumented.

cookie的存储id,用于从中检索cookie。默认情况下,当前执行上下文的cookie存储将被使用。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

callback ( optional enumerated Type array of function 函数 )

Undocumented.

Description of this parameter from the json schema.

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

Returns

回调函数

如果指定了回调参数,它应该指定一个如下所示函数:

If you specify the callback parameter, it should specify a function that looks like this:

function(Cookie cookie) {...};

cookie ( optional enumerated Cookie array of paramType ) (可选,Cookie)

Undocumented.

包含cookie的详细信息。如果没找到cookie,该参数为null。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

This function was added in version . If you require this function, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

getAll

void chrome.cookies.getAll(, object details, function callback)

Undocumented.

从一个cookie存储获取与给定信息匹配的所有cookies。所获取cookies将按照最长路径优先排序。当多个cookies有相同长度路径,最早创建的cookie在前面。

参数

details ( optional enumerated Type array of object )

Undocumented.

对cookies进行筛选的信息。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

url ( optional enumerated Type array of string可选,字符串 )

Undocumented.

限定只查找与给定URL匹配的cookies。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

name ( optional enumerated Type array of string可选,字符串 )

Undocumented.

根据名称过滤cookies。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

domain ( optional enumerated Type array of string可选,字符串 )

Undocumented.

限定只查找与给定域名或者子域名匹配的cookies。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

path ( optional enumerated Type array of string可选,字符串 )

Undocumented.

限定只查找与给定路径完全一致的cookies。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

secure ( optional enumerated Type array of boolean可选,Boolean类型 )

Undocumented.

根据cookie的Secure属性进行筛选。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

session ( optional enumerated Type array of boolean可选,Boolean类型 )

Undocumented.

根据cookie的生命周期是会话的还是持久的进行过滤。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

storeId ( optional enumerated Type array of string可选,字符串 )

Undocumented.

cookie的存储id,用于从中检索cookie。默认情况下,当前执行上下文的cookie存储将被使用。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

callback ( optional enumerated Type array of function )

Undocumented.

Description of this parameter from the json schema.

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

Returns

回调函数

如果指定了回调参数,它应该指定一个如下所示函数:

If you specify the callback parameter, it should specify a function that looks like this:

function(array of Cookie cookies) {...};

cookies ( optional enumerated Type array of Cookie array of paramType paramType 可选,cookie列表 )

Undocumented.

所有与给定cookie信息匹配、存在并未过期的cookie列表。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

This function was added in version . If you require this function, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

getAllCookieStores

void chrome.cookies.getAllCookieStores(, function callback)

Undocumented.

列举所有存在的cookie存储。

参数

callback ( optional enumerated Type array of function )

Undocumented.

Description of this parameter from the json schema.

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

Returns

回调函数

如果指定了回调参数,它应该指定一个如下所示函数:

If you specify the callback parameter, it should specify a function that looks like this:

function(array of CookieStore cookieStores) {...};

cookieStores ( optional enumerated Type array of CookieStore 可选,cookie存储列表 array of paramType paramType )

Undocumented.

所有存在的cookie存储。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

This function was added in version . If you require this function, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

remove

void chrome.cookies.remove(, object details)

Undocumented.

根据名称删除cookie。

参数

details ( optional enumerated Type array of object )

Undocumented.

用于鉴定待删除cookie的信息。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

url ( optional enumerated Type array of string )

Undocumented.

与所收到的cookie关联的URL。如果清单文件中没有设置这个URL对应的主机权限,那么这个API调用会失败。

This parameter was added in version . You must omit this parameter in earlier versions, and you may omit it in any version. If you require this parameter, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

name ( optional enumerated Type array of string )

Undocumented.