讓擴充套件可以取得、設定 cookies 資訊,並監控其變動。
 

使用此 API 前,必須先在 manifest.json 檔案中加入「cookies」這項 API 權限宣告,也必須以 host 權限宣告將要存取 Cookies 的網站列入。參見 Cookie 權限一節。

型別

cookies.Cookie
代表一個 HTTP cookie 的相關資訊。
cookies.CookieStore
代表瀏覽器中的 cookie 存放空間。
cookies.OnChangedCause
代表觸發 cookie 資料變動的原因。

方法

cookies.get()
取回單一 cookie 的相關資訊。
cookies.getAll()
取回符合設定條件的所有 cookies 資訊。
cookies.set()
為 cookie 設定資料。如果目前已有相同的 cookies,則會覆寫原本的 cookie 資料。
cookies.remove()
刪除某特定名稱的 cookie。
cookies.getAllCookieStores()
列出目前所有的 cookie 存放空間。

事件處理程序

cookies.onChanged
當 cookie 設定或刪除時觸發。

權限

使用此 API 前,擴充套件應於 manifest.json 設定檔中指明需要「cookies」API 權限,亦須以 host 權限宣告指明需要存取 cookies 的網站清單。此後,符合 host 權限宣告的 URL 所能讀寫的任何 cookies,該擴充套件即可讀取。比方說:

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。

瀏覽器相容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxOperaFirefox for Android
CookieChrome Full support YesEdge Full support 14Firefox Full support 45Opera Full support YesFirefox Android Full support 48
CookieStoreChrome Full support YesEdge Full support 14Firefox Full support 45Opera Full support YesFirefox Android Full support 48
OnChangedCauseChrome Full support YesEdge No support NoFirefox Full support 45Opera Full support YesFirefox Android Full support 48
getChrome Full support YesEdge Full support 14Firefox Full support 45
Notes
Full support 45
Notes
Notes Provides access to cookies from private browsing mode and container tabs since version 52.
Opera Full support YesFirefox Android Full support 48
getAllChrome Full support YesEdge Full support 14
Notes
Full support 14
Notes
Notes If no URL is provided, cookies are retrieved only for URLs in currently opened tabs. In Chrome, this gets all cookies on a user's machine.
Firefox Full support 45
Notes
Full support 45
Notes
Notes Before version 52, the 'tabIds' list was empty and only cookies from the default cookie store were returned. From version 52 onwards, this has been fixed and the result includes cookies from private browsing mode and container tabs.
Opera Full support YesFirefox Android Full support 48
getAllCookieStoresChrome Full support YesEdge Full support 14
Notes
Full support 14
Notes
Notes Always returns the same default cookie store with ID 0. All cookies belong to this store.
Firefox Full support 45
Notes
Full support 45
Notes
Notes Before version 52, only the default cookie store was visible. From version 52 onwards, the cookie stores for private browsing mode and container tabs are also readable.
Opera Full support YesFirefox Android Full support 48
onChangedChrome Full support YesEdge No support NoFirefox Full support 45Opera Full support YesFirefox Android Full support 48
removeChrome Full support YesEdge Full support 14Firefox Full support 45
Notes
Full support 45
Notes
Notes Before version 56, this function did not remove cookies from private browsing mode. From version 56 onwards this is fixed.
Opera Full support YesFirefox Android Full support 48
Notes
Full support 48
Notes
Notes Before version 56, this function did not remove cookies from private browsing mode. From version 56 onwards this is fixed.
setChrome Full support YesEdge Full support 14Firefox Full support 45
Notes
Full support 45
Notes
Notes Before version 56, this function did not modify cookies in private browsing mode. From version 56 onwards this is fixed.
Opera Full support YesFirefox Android Full support 48
Notes
Full support 48
Notes
Notes Before version 56, this function did not modify cookies in private browsing mode. From version 56 onwards this is fixed.

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.

Edge 不相容資訊

Edge 不支援 promises,請使用回呼(callback)函式處理。

Chrome incompatibilities

None.

Example extensions

致謝

此 API 基於 Chromium 的 chrome.cookies API 而來,文件改作自 Chromium 程式碼裡的 cookies.json

Microsoft Edge 的相容資訊來自微軟公司,原文以創用 CC 姓名標示 3.0 美國版條款授權大眾使用。

文件標籤與貢獻者

此頁面的貢獻者: BobChao
最近更新: BobChao,