单击图标时,删除chrome扩展中的特定cookie

时间:2016-06-12 16:48:56

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

这是我的第一个节目,我迷路了...... 当我点击此扩展程序的图标时,我尝试执行从特定网站删除一个cookie的扩展程序。 我的代码出了什么问题?

谢谢大家!

的manifest.json:

    {    
  "name" : "Delete Cookie",
  "version" : "0.8",
  "manifest_version": 2
  "description" : "Delete Cookie by icon clicked",
  "permissions": [ "cookies", "http://www.example.com" ],
  "icons": { "16": "cookie.png", "48": "cookie.png", "128": "cookie.png" },
  "browser_action": {
  "default_icon": "cookie.png"
  },
  "background": {
  "scripts": ["background.js"]
  },

}

backgroung.js:

chrome.browserAction.onClicked.addListener(DeletCookie) 
function DeletCookie()  
{  
      chrome.cookies.remove({url:"http://www.example.com", name: "CookieName"})  
}  

修改

我试过了:

chrome.cookies.getAll({domain: "www.example.com"}, function(cookies) {
for(var i=0; i<cookies.length;i++) {
  console.log(cookies[i]);

  chrome.cookies.remove({url: "https://" + cookies[i].domain  + cookies[i].path, name: cookies[i].name});
}

});

并且它有效,但这样它每次都会删除cookie。

所以我试试:

function DeleteCookie (){
   chrome.cookies.getAll({domain: "www.example.com"}, function(cookies) {
       for(var i=0; i<cookies.length;i++) {
        console.log(cookies[i]);

        chrome.cookies.remove({url: "https://" + cookies[i].domain  +           cookies[i].path, name: cookies[i].name});
       }
    });   
}; 
chrome.browserAction.onClicked.addListener(DeletCookie) ;

这样它再也不起作用了

1 个答案:

答案 0 :(得分:0)

我在扩展程序中做了这件事

delete_cookie('cookies_name');

var delete_cookie = function(name) {
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};

对我来说很好。