Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

javascript - Delete specific cookie in chrome extension when the icon clicked

this is my first program and i'm getting lost.. I try to do an extension that delete one cookie from specific web when I click on the icon of this extension. What's wrong with my code?

Thank you all!

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"})  
}  

EDIT

I tried this:

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});
}

});

and it worked,but this way it delete the cookie every time it shwoes up.

So I try:

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) ;

and this way it doesn't work again

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have done this thing in my extension

delete_cookie('cookies_name');

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

that work fine for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...