使用 WebExtensions 获取或设置 cookies, 并且在修改时能够获得通知。
你需要在 manifest.json 文件中开启“cookies”API 权限,并且需要对应站点的 主机权限 才能设置指定站点的cookie。详细信息查看 cookie 权限.
类型
cookies.Cookie
- 代表一个HTTP cookie的信息。
cookies.CookieStore
- 代表一个保存在浏览器中的 cookie。
cookies.OnChangedCause
- 代表 cookie 改变的原因。
方法
cookies.get()
- 返回一个单独的 cookie 的信息。
cookies.getAll()
- 返回所有符合筛选条件的 cookies。
cookies.set()
- 根据给定cookie数据设置一个cookie;如果同样的cookie存在讲会覆盖。
cookies.remove()
- 根据名字删除cookie。
cookies.getAllCookieStores()
- 列出所有保存的cookie。
事件句柄
cookies.onChanged
- 当设置或删除cookie时触发。
权限
为了使用这个API,插件必须在它的manifest中指定"cookies" API 权限,和它想要使用cookie的任何网站的 host 权限 。插件将能读取或写入host权限中所匹配的URL可以读取或写入的任何cookie。例如:
http://*.example.com/
-
拥有这个host权限的插件将可以:
- 读取一个
www.example.com
任意路径下的不安全cookie。 - 写入一个
www.example.com
任意路径下的不安全cookie。
它不能:
- 读取
www.example.com
的安全cookie。
- 读取一个
http://www.example.com/
-
拥有这个host权限的插件将可以:
- 读取
www.example.com
任意路径下的不安全cookie。 - 读取
.example.com
任意路径下的不安全cookie。 - 写入
www.example.com
任意路径下的安全和不安全cookie。 - 写入
.example.com
任意路径下的安全和不安全cookie。
它不能:
- 读取或写入
foo.example.com
的cookie。 - 读取或写入
foo.www.example.com
的cookie。
- 读取
*://*.example.com/
-
拥有这个host权限的插件将可以:
- 读取或写入
www.example.com
任意路径下安全的和不安全的cookie。
- 读取或写入
浏览器兼容性
BCD tables only load in the browser
The "Chrome incompatibilities" section is included from https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Chrome_incompatibilities using the WebExtChromeCompat macro.
If you need to update this content, edit https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Chrome_incompatibilities, then shift-refresh this page to see your changes.
Edge 不兼容
在 Edge 中不支持 Promises,使用 callbacks 代替。
Example extensions
这个API 基于 Chromium 的 chrome.cookies
API. 这篇文档来源于Chromium 代码的 cookies.json
。
Microsoft Edge 兼容性数据由 Microsoft Corporation 提供,并包含在 Creative Commons Attribution 3.0 United States License.
// Copyright 2015 The Chromium Authors. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.