0

My plan was to make an ajax without sending the cookies there. I tried to do some magic via jquery, but unfortunately I found I have no power over what cookies are sent. So I needed to delete them, do the ajax and then put them back. Here's the code.

chrome.cookies.getAll({domain: domain}, function(cookies) {
  var domain = 'facebook.com';
  // 1. delete cookies
  var myCookies = _.cloneDeep(cookies);
  for(var i=0; i<cookies.length;i++) {
    chrome.cookies.remove({url: "https://"+domain + cookies[i].path, name: cookies[i].name});
  }

  // 2. now do some ajax

  // 3. now get them back
  for (var i = 0; i < myCookies.length; ++i) {
    var cookie = myCookies[i];
    var obj = {
      name: cookie.name,
      value: cookie.value,
      url: "https://"+domain + cookie.path
    };
    chrome.cookies.set(obj);
  }
});

unfortunately, it logs me out, even when the cookies are added back, like they were supposed to. where is the catch? Is the "url" and "domain" argument used well?

Xan
  • 74,770
  • 16
  • 179
  • 206
Novellizator
  • 13,633
  • 9
  • 43
  • 65
  • 1
    For an alternative approach, see this: http://stackoverflow.com/a/29965173/934239 The question is valid on its own though. – Xan May 13 '15 at 14:17
  • Though the answer may be very specific to Facebook. – Xan May 13 '15 at 14:23
  • @Xan btw why specific? ignoring all the cookies shoudln't be platform specific... – Novellizator May 13 '15 at 14:24
  • It's possible that you did not clean all relevant cookies, and Facebook invalidated the session. Also, there are [more properties](https://developer.chrome.com/extensions/cookies#type-Cookie) that you might want to restore with `set` – Xan May 13 '15 at 14:25
  • well, the problem is more, that I cleaned and didn't put them back. Because it logged me out, so there was more tha enough cleaning. The request was also successful. The only problem is with restoring them back. As for the properties, thanks, good point. But still, I thought whan it has the right path, name, value and the other properties are default, it should bother. Unfortunatley it does for some reason – Novellizator May 13 '15 at 14:36
  • @Xan the client in the end doesn't want the solution with cookie deleting. I checked out your suggestion and it looks quite well. I'd like to ask you one thing though: can so something like: add onsendsendheaders listener, then perform the request and then remove this listener? i don't want this listener to mainly 1. affect page requests... but also 2. further extension requests – Novellizator May 14 '15 at 09:11
  • Probably adding/removing a listener is more expensive than setting a variable and checking it in the listener. – Xan May 14 '15 at 09:18

0 Answers0