browser.cookies.getAll() always returns nothing - Firefox Extension
Asked Answered
G

3

2

I have been trying to list all browser cookies in my extension with browser.cookies.getAll(). At first, I thought it was a permissions issue, but my permissions appear to be set correctly. Here is my code:

manifest.json

{
    "manifest_version": 2,
    "name": "CookieExample",
    "version": "0.1",
    
    "applications": {
        "gecko": {
            "id": "[email protected]"
        }
    },
    
    "permissions": [
        "cookies",
        "webNavigation",
        "webRequest",
        "webRequestBlocking",
        "<all_urls>"
    ],
    
    "background": {
        "scripts": ["cookies.js"]
    }        
}

cookies.js

browser.cookies.getAll({}, function(cookies) {
    console.log(cookies);
});

The console simply logs [] as the return value. I don't know where I've gone wrong or if it's a problem with the browser. Cookie Manager works just fine and lists all browser cookies without a hitch. Looking into its source code, the only difference is that it uses a cookie store ID to query for cookies, whereas I used nothing.

Gar answered 18/8, 2018 at 18:20 Comment(0)
S
0

you can use browser.cookies.getAllCookieStores to get all stores, match them with the id of the tab you want the cookies from (a store knows all it's tabs in the tabIds property), then pass the storeId to browser.cookies.getAll.

Stratification answered 29/8, 2018 at 14:54 Comment(4)
Is it only possible to get cookies from open tabs? Not just all cookies in the browser?Gar
cookies are stored per domain, not per tab. you can ask each tab for it's URL and query all cookies for that specific URL.Stratification
match them with the id of the tab you want the cookies from: how to get all cookies regardless if any tab is open? It would be interesting to find a solution without requiring anything about tabs.Landwaiter
Cookies aren't tab related at all. In order to retrieve cookies for a specific request you need to specify a target URL and from wich cookie store they should be taken (There are two: The common one and the store for private browsing). If you want to retrieve all cookies in any cookie store you need to use getAllCookieStores to enumerate the stores and the use getAll to retrieve all cookies in that store. If you don't specify the cookie store in getAll the browser will automatically select one, but I don't know how the browser decides whether to use the common or private one.Stratification
R
0

from mozilla docs

function logCookies(cookies) {
  for (let cookie of cookies) {
    console.log(cookie.value);
  }
}

var gettingAll = browser.cookies.getAll();
gettingAll.then(logCookies);
Rodrickrodrigez answered 2/3, 2022 at 4:43 Comment(0)
C
0

browser.cookies.getAll() always returns nothing

there are few javascripts functions(methods) which are not compatible with safari and firefox. one of the them is cookieStore.getAll() in order to use them we can use
const allCookies = document.cookie.split(';');

Castoff answered 8/3 at 4:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.