0

I am developing a crossrider (crossbrowser) extension right now, based on an existing chrome plugin.

Part of the privacy concept and the login procedure i need to manually set and remove some cookies. In Chrome, i do the following procedures:

chrome.cookies.set({"url":"https://protonmail.ch/","domain":".protonmail.ch","path":"/","expirationDate": expire,"secure":true,"name":"protonmail_pw","value":"true"}, function (cookie){
    //continue
});

chrome.cookies.getAll({"domain":".protonmail.ch"}, function(cookies) {
    for(var i=0; i<cookies.length;i++) {
        chrome.cookies.remove({'url': "http" + (cookies[i].secure ? "s" : "") + "://" + cookies[i].domain + cookies[i].path, name: cookies[i].name});
    }
});

How can i reconstruct this behaviour using crossrider? I wasn't able to find any documentation regarding cookie manipulation!

Best Regards & Thanks in Advance

James Cameron
  • 1,681
  • 25
  • 40

1 Answers1

0

Crossrider supports using its local database appAPI.db.async for storing data; hence, your code could be ported as follows:

appAPI.db.async.set(
  "protonmail_pw",
  {
    "url": "https://protonmail.ch/",
    "domain": "protonmail.ch",
    "path": "/",
    "secure": true,
    "value": "true"
  },
  appAPI.time.hoursFromNow(12), // Set the expiration as required
  function() {
    //continue
  }
);

appAPI.db.async.getList(function(cookies) {
  for (var i = 0; i < cookies.length; i++) {
    if (cookies[i].value.domain==="protonmail.ch" &&
        cookies[i].value.url==="http" + (cookies[i].value.secure ? "s" : "") + "://" + cookies[i].value.domain + cookies[i].value.path)
      appAPI.db.async.remove(cookies[i].key);
  }
});

// Example request
appAPI.db.async.get(
  "protonmail_pw",
  function(value) {
    appAPI.request.post({
      url: 'http://example.com',
      postData: {
        data: {},
        cookie: value
      },
      onSuccess: function(response) {
        console.log('Response: ' + response);
      },
      contentType: 'application/json'
    });
  }
);

UPDATE: Added sample request to example

[Disclosure: I am a Crossrider employee]

Shlomo
  • 3,763
  • 11
  • 16
  • And this emulates a cookie for the webserver? My goal is not to save data but to send a cookie to the server as this triggers a certain behaviour there. – James Cameron Jul 15 '15 at 07:24
  • No, this is purely local. For a web server I suggest sending the cookie data as part of the request and processing at the server end it as part of the data. – Shlomo Jul 15 '15 at 11:35
  • That's what i expected. Can you probably make another example on how to append cookies as part of the request? I've seen how to add a header, but i think it overrides. I need existing cookies and an additional one. Do i understand you right i can't remove cookies using crossrider? – James Cameron Jul 15 '15 at 12:12
  • This still isn't what i want to do, this is just about sending post data. Look, this is not my server. I don't want to send data, i want to set a [http-cookie](https://en.wikipedia.org/wiki/HTTP_cookie). Before i can log in, i need to manually set a cookie as a workaround for the login procedure. Neither a local storage is in any way useful, nor sending post-data. For the post-request, there is an option called `additionalRequestHeaders`. Is that probably a good approach on adding a cookie? – James Cameron Jul 15 '15 at 13:05
  • Thanks for clarifying your requirements. I don't think the _additionalRequestHeaders_ property will help, but you're welcome to try. If using cookies this way is critical to your extension's operation, then perhaps another solution/framework may meet your requirements. – Shlomo Jul 16 '15 at 17:46