4

Trying to get all cookies from the page, using chrome extension.

For example the page os https://ya.ru.

Here is my code:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    var domain = getDomain(tabs[0].url);

    chrome.cookies.getAll({
        domain: domain
    }, function(cookies) {
        console.log(cookies);
    });

});

This code will return all cookies (8) for domain ya.ru. But the problem is when I'm opening the browser console I see cookies not only for ya.ru domain:

enter image description here

Same situation is on google and other sites. So I'm having multiple domain cookies on one page. How to get ALL cookies on the page?

Thank for your time.

KaronatoR
  • 2,579
  • 4
  • 21
  • 31
  • To use the cookies API, you must declare the "cookies" permission in your manifest, along with host permissions for any hosts whose cookies you want to access. – Aefits Apr 30 '18 at 11:13
  • So you have to declare all domains in your manifest file that you want to read cookies of. – Aefits Apr 30 '18 at 11:14

1 Answers1

8

Devtools shows cookies for all resource URLs requested by the page (source code) so we can do the same by accessing Performance API in the content script code that we'll execute in the page:

chrome.tabs.executeScript({
  code: 'performance.getEntriesByType("resource").map(e => e.name)',
}, data => {
  if (chrome.runtime.lastError || !data || !data[0]) return;
  const urls = data[0].map(url => url.split(/[#?]/)[0]);
  const uniqueUrls = [...new Set(urls).values()].filter(Boolean);
  Promise.all(
    uniqueUrls.map(url =>
      new Promise(resolve => {
        chrome.cookies.getAll({url}, resolve);
      })
    )
  ).then(results => {
    // convert the array of arrays into a deduplicated flat array of cookies
    const cookies = [
      ...new Map(
        [].concat(...results)
          .map(c => [JSON.stringify(c), c])
      ).values()
    ];

    // do something with the cookies here
    console.log(uniqueUrls, cookies);
  });
});

Important notes:

  • Your manifest.json should have "<all_urls>" in "permissions" or an explicit list of URL match patterns that will allow the corresponding results in chrome.cookies.getAll.

  • Cookie deduplication code above may be slow when there are thousands of cookies.

  • We're reading cookies for an URL, not domain, because it's the only reliable way to get an encompassing cookie (imgur.com) for something like i.stack.imgur.com

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Sorry, there's strange error sometimes, maybe you can answer why? https://ibb.co/gtQq9S https://ibb.co/juaU3n – KaronatoR May 01 '18 at 13:44
  • 1
    I've modified the code so it does nothing when the error occurs. – wOxxOm May 01 '18 at 13:51
  • Just a note that this answer loads cookies only from resources. If all your resources are off main domain (i.e. CDN) then cookies for your main URL won't show in results. Either merge performance.getEntriesByType("resource") with performance.getEntriesByType("navigation"), or with getDomain() – J. Wrong Oct 29 '20 at 15:29