开发者

Google chrome extension cookie help!

开发者 https://www.devze.com 2023-02-13 18:45 出处:网络
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.

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 c开发者_JS百科ookie 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 i.e. 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?


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.


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/*"
]
0

精彩评论

暂无评论...
验证码 换一张
取 消