简体   繁体   中英

Google chrome extension cookie help!

I need my chrome extension to read a value of a cookie set by a rails app, I did just try and use plain javascript to achieve this but kept getting the result of the cookie as null.

As I understand it you have to use the Chrome cookie API to read them? I'm totally clueless when it comes to chrome extensions, I'm struggling with what the structure should be for the extensions ie A Background page, popup or a content script??

Ideally I want the popup.html to iterate through the values in the cookie that is sent, so I would strip the cookie for the values which would then be written to the popup.html file.

Any ideas or suggestions where to start?

Currently the best (the simplest) way to get site cookies in extension is like this:

chrome.cookies.get({ url: 'http://example.com', name: 'somename' },
  function (cookie) {
    if (cookie) {
      console.log(cookie.value);
    }
    else {
      console.log('Can\'t get cookie! Check the name!');
    }
});

So now you don't need content script for this but don't forget to include permissions into manifest:

"permissions": [
  "cookies",
  "*://*.example.com/*"
]

The only way you can read your sites cookies is by Content Scripts .

Since you wanted to do that in a popup.html page, you have two choices:

  1. Tabs API - chrome.tabs.executeScript
  2. Messaging API - chrome.tabs.sendRequest

Once you are in the content script, you can communicate to that pages Cookies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM