この記事はまだボランティアによって 日本語 に翻訳されていません。ぜひ MDN に参加して翻訳を手伝ってください!
The get()
method of the cookies
API retrieves information about a single cookie, given its name and URL.
If more than one cookie with the same name exists for a given URL, the one with the longest path will be returned. For cookies with the same path length, the cookie with the earliest creation time will be returned. If no matching cookie could be found, null
is returned.
Syntax
// callback version chrome.cookies.get( details, // object function(cookie) {...} // function ); // promise version var getCookie = browser.cookies.get( details // object ) getCookie.then( function(cookie) {}, // function function(error) {} // function );
Parameters
details
- An
object
containing details that can be used to match a cookie to be retrieved. It can include the following properties: -
url
- A
string
representing the URL with which the cookie to retrieve is associated. This argument may be a full URL, in which case any data following the URL path (e.g. the query string) is simply ignored. If host permissions for this URL are not specified in the WebExtension's manifest file, the API call will fail. name
- A
string
representing the name of the cookie to retrieve. storeId
Optional- A
string
representing the ID of thecookie store
in which to look for the cookie (as returned bycookies.getAllCookieStores()
). By default, the current execution context's cookie store will be used.
callback (callback version only)
- A callback function, which is passed a
Cookie
object containing details about the cookie, ornull
if the cookie was not found.
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Chrome | Edge | Firefox | Firefox for Android | Opera | |
---|---|---|---|---|---|
Basic support | Yes | Yes | 45.0 | 48.0 | 33 |
Examples
In the following snippet (taken from our cookie-bg-picker example), we are using an event listener to check for the tabs.onUpdated
event occurring (i.e. a change has occurred, like a new URL being loaded into the tab).
When this happens, we run the cookieUpdate()
function. Inside here we get the current tab using tabs.query()
, and inject our content script into the current tab using tabs.executeScript()
.
We then try to get the "bgpicker" cookie for the URL for the current tab using get()
:
- If it exists already in the browser, background customization will have previously been set on the current page using the extension, so we get the customization preferences out of the cookie value and send them to the content script using
tabs.sendMessage()
. - If the cookie does not already exist, no preferences have been set, so no action is needed.
chrome.tabs.onUpdated.addListener(cookieUpdate); function cookieUpdate(tabId, changeInfo, tab) { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { /* inject content script into current tab */ chrome.tabs.executeScript(null, { file: "/content_scripts/updatebg.js" }); // get any previously set cookie for the current tab chrome.cookies.get({ url: tabs[0].url, name: "bgpicker" }, function(cookie) { if(cookie) { var cookieVal = JSON.parse(cookie.value); chrome.tabs.sendMessage(tabs[0].id, {image: cookieVal.image}); chrome.tabs.sendMessage(tabs[0].id, {color: cookieVal.color}); } }); }); }
Example add-ons
This API is based on Chromium's chrome.cookies
API. This documentation is derived from cookies.json
in the Chromium code.
Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the 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.