--- title: cookies slug: Mozilla/Add-ons/WebExtensions/API/cookies tags: - API - Add-ons - Cookies - Extensions - WebExtensions translation_of: Mozilla/Add-ons/WebExtensions/API/cookies ---
使用 WebExtensions 获取或设置 cookies, 并且在修改时能够获得通知。
你需要在 manifest.json 文件中开启“cookies”API 权限,并且需要对应站点的 主机权限 才能设置指定站点的cookie。详细信息查看 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。{{Compat("webextensions.api.cookies")}}
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 中不支持 Promises,使用 callbacks 代替。
{{WebExtExamples("h2")}}
这个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.