在CrossRider中操纵cookie

时间:2015-07-14 14:01:42

标签: javascript cookies crossrider

我正在开发一个基于existing chrome plugin的crossrider(crossbrowser)扩展程序。

隐私概念和登录程序的一部分我需要手动设置和删除一些cookie。在Chrome中,我执行以下过程:

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

如何使用crossrider重建此行为?我无法找到任何关于cookie操作的文档!

最好的问候&amp;在此先感谢

1 个答案:

答案 0 :(得分:0)

Crossrider支持使用其本地数据库appAPI.db.async来存储数据;因此,您的代码可以移植如下:

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

更新:向示例

添加了示例请求

[披露:我是Crossrider员工]