简体   繁体   中英

Error Trying to Get Cookies From Chrome Extension

I have a very basic Chrome extension. I can execute JS just fine but accessing anything in the Chrome API seems to be an issue. I am trying remove cookies for a particular site. However, I get the following error when my code executes.

The error:

content.js:6 Uncaught TypeError: Cannot read property 'getAll' of undefined

My Code:

(function(){
   chrome.cookies.getAll({}, cookies=>{
      _.forEach(cookies, cookie=>{
          chrome.cookies.remove({name: cookie.name, url: "www.mydomain.com"});
      });
    });
)();

I figured it may have something to do with my permissions but I am not sure. Here is my manifest.json file.

{
  "manifest_version": 2,
  "name": "Hello World Extension",
  "version": "0.1",
  "content_scripts": [
      {
        "matches": [
          "<all_urls>"
        ],
        "js": ["jquery.js","content.js"]
      }
  ],
  "permissions": [
      "cookies"
  ]
}

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.

{
  "manifest_version": 2,
  "name": "Hello World Extension",
  "version": "0.1",
  "content_scripts": [
    {
      "matches": [
         "<all_urls>"
      ],
      "js": ["jquery.js","content.js"]
    }
  ],
  "permissions": [
     "cookies",
     "http://*/*",
     "https://*/*"
  ]
}

To remove the cookies, try this:

function removeAll(url){
   chrome.cookies.getAll({}, function(cookies) {
     for (var i in cookies) {
        chrome.cookies.remove({"url": url, "name": cookie.name});
     }
  });
}

I hope this works

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