chrome.cookies.getAll返回一个空数组

时间:2018-06-09 07:24:07

标签: javascript google-chrome cookies google-chrome-extension

我正在开发一个简单的Chrome扩展程序,只需点击一下即可删除域中的所有Cookie,但出于某种原因,它无效。当我尝试从域中获取所有cookie时,它返回一个空数组。我究竟做错了什么? 这是js脚本:



$("#fixTheCookiesButton").click(() => {
  // delete the cookies
  chrome.cookies.getAll({domain: "https://www.youtube.com"}, (cookies) => {
    console.log("deleting " + cookies.length + " cookies")
    for(var i = 0; i < cookies.length; i++){
      console.log(i + " deleted")
      chrome.cookies.remove({
        url: "https://www.youtube.com" + cookies[i].path,
        name: cookies[i].name
      })
    }
    
    // some other stuff that isn't relevant here
}
&#13;
&#13;
&#13;

以及我的清单:

{
  "manifest_version": 2,
  "name": "FixYT",
  "version": "1.0",
  "description": "Fixes that YT cookie bug with one click",
  "browser_action": {
          "default_title": "FixYT",
          "default_popup": "popup.html"
  },
  "permissions": [
    "cookies",
    "https://www.youtube.com/",
    "*://www.youtube.com/",
    "tabs",
    "*://*/"
  ]
}

我试过环顾互联网,但我无法找到解决方法。

1 个答案:

答案 0 :(得分:1)

你应该在background.js中调用这个代码块

    chrome.cookies.getAll({
  domain: ".youtube.com"
}, function (cookies) {
  for (var i = 0; i < cookies.length; i++) {
    console.log(cookies[i] + "deleted");
    chrome.cookies.remove({
      url: "https://" + cookies[i].domain + cookies[i].path,
      name: cookies[i].name
    });
  }
});