0

the chrome.cookies API is not clear to me. I want to getAll cookies for 3 different Domains, then delete those and wait for this process to finish, afterwards I want to set some cookies.

If I understand cookies chrome.cookies.getAll does not return a promise, only I can define a callback. Now its clear to me that I can write callbacks for all three getAll cookies commands but in there I am removing several cookies again this process goes asynchronously. So I am totally lost on how I can identify when all cookies of 3 domains have been completely removed.

One option that I could think of is that I run the 3 cookie.getAlls one time ahead and count the number of cookies, then with every remove I increase a counter and in a callback of remove I check whether I have reached the total number. This seems very strange so I can not believe that this is the correct way to do it.

Thanks

1 Answers1

0

i don't think it's optimize but this is a quick answer.

function RemoveCookies(cookies, domain){
   for(var i=0; i<cookies.length;i++) {
        chrome.cookies.remove({url: 'https://'domain + cookies[i].path, name:cookies[i].name});
      }
}

function RemoveDomain1(callback, ...params){
  chrome.cookies.getAll({domain: domain1}, function(cookies) {
     RemoveCookies(cookies, domain1);
     callback(params)
  
 });
}
function RemoveDomain2(callback, ...params){
  chrome.cookies.getAll({domain: domain2}, function(cookies) {
     RemoveCookies(cookies, domain2);
     callback(params)
  
 });
}
function RemoveDomain3(callback, ...params){
  chrome.cookies.getAll({domain: domain3}, function(cookies) {
     RemoveCookies(cookies, domain3);
     callback()
  
 });
}

RemoveDomain1(RemoveDomain2, RemoveDomain3, DoSomthingAfterAll)

check this link too maybe it help

chif.j
  • 180
  • 1
  • 4