Skip to content

Commit

Permalink
Add cookies API sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
hokein committed Jun 26, 2015
1 parent 26d0633 commit 76a7ebe
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cookies/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var app = require('app');
var BrowserWindow = require('browser-window');

var mainWindow = null;
app.on('ready', function() {
mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.loadUrl('file://' + __dirname + '/manager.html');
});

43 changes: 43 additions & 0 deletions cookies/manager.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<html>
<head>
<style>
table {
border-collapse:collapse;
}

td {
border: 1px solid black;
padding-left: 5px;
}

td.button {
border: none;
}

td.cookie_count {
text-align: right;
}

</style>
<script src="manager.js"></script>
</head>
<body>
<h2>Cookies! ... Nom Nom Nom...</h2>
<button id="remove_button">DELETE ALL!</button>
<div id="filter_div">
Filter: <input id="filter" type="text">
<button>x</button>
</div>
<br />
<div id="summary_div">
Showing <span id="filter_count"></span> of <span id="total_count"></span> cookie domains.
<span id="delete_all_button"></span>
</div>
<br />
<table id="cookies">
<tr class="header">
<th>Name</th>
<th>#Cookies</th>
</tr>
</body>
</html>
248 changes: 248 additions & 0 deletions cookies/manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
var win = require('remote').getCurrentWindow();

// A simple Timer class.
function Timer() {
this.start_ = new Date();

this.elapsed = function() {
return (new Date()) - this.start_;
}

this.reset = function() {
this.start_ = new Date();
}
}

// Compares cookies for "key" (name, domain, etc.) equality, but not "value"
// equality.
function cookieMatch(c1, c2) {
return (c1.name == c2.name) && (c1.domain == c2.domain) &&
(c1.hostOnly == c2.hostOnly) && (c1.path == c2.path) &&
(c1.secure == c2.secure) && (c1.httpOnly == c2.httpOnly) &&
(c1.session == c2.session) && (c1.storeId == c2.storeId);
}

// Returns an array of sorted keys from an associative array.
function sortedKeys(array) {
var keys = [];
for (var i in array) {
keys.push(i);
}
keys.sort();
return keys;
}

// Shorthand for document.querySelector.
function select(selector) {
return document.querySelector(selector);
}

// An object used for caching data about the browser's cookies, which we update
// as notifications come in.
function CookieCache() {
this.cookies_ = {};

this.reset = function() {
this.cookies_ = {};
}

this.add = function(cookie) {
var domain = cookie.domain;
if (!this.cookies_[domain]) {
this.cookies_[domain] = [];
}
this.cookies_[domain].push(cookie);
};

this.remove = function(cookie) {
var domain = cookie.domain;
if (this.cookies_[domain]) {
var i = 0;
while (i < this.cookies_[domain].length) {
if (cookieMatch(this.cookies_[domain][i], cookie)) {
this.cookies_[domain].splice(i, 1);
} else {
i++;
}
}
if (this.cookies_[domain].length == 0) {
delete this.cookies_[domain];
}
}
};

// Returns a sorted list of cookie domains that match |filter|. If |filter| is
// null, returns all domains.
this.getDomains = function(filter) {
var result = [];
sortedKeys(this.cookies_).forEach(function(domain) {
if (!filter || domain.indexOf(filter) != -1) {
result.push(domain);
}
});
return result;
}

this.getCookies = function(domain) {
return this.cookies_[domain];
};
}


var cache = new CookieCache();


function removeAllForFilter() {
var filter = select("#filter").value;
var timer = new Timer();
cache.getDomains(filter).forEach(function(domain) {
removeCookiesForDomain(domain);
});
}

function removeAll() {
var all_cookies = [];
cache.getDomains().forEach(function(domain) {
cache.getCookies(domain).forEach(function(cookie) {
all_cookies.push(cookie);
});
});
cache.reset();
var count = all_cookies.length;
var timer = new Timer();
for (var i = 0; i < count; i++) {
removeCookie(all_cookies[i]);
}
timer.reset();
win.webContents.session.cookies.get({}, function(cookies) {
for (var i in cookies) {
cache.add(cookies[i]);
removeCookie(cookies[i]);
}
});
}

function removeCookie(cookie) {
var url = "http" + (cookie.secure ? "s" : "") + "://" + cookie.domain +
cookie.path;
win.webContents.session.cookies.remove({"url": url, "name": cookie.name},
function(error) {
if (error) throw error;
update(cookie);
});
}

function removeCookiesForDomain(domain) {
var timer = new Timer();
cache.getCookies(domain).forEach(function(cookie) {
removeCookie(cookie);
});
}

function resetTable() {
var table = select("#cookies");
while (table.rows.length > 1) {
table.deleteRow(table.rows.length - 1);
}
}

var reload_scheduled = false;

function scheduleReloadCookieTable() {
if (!reload_scheduled) {
reload_scheduled = true;
setTimeout(reloadCookieTable, 250);
}
}

function reloadCookieTable() {
reload_scheduled = false;

var filter = select("#filter").value;

var domains = cache.getDomains(filter);

select("#filter_count").innerText = domains.length;
select("#total_count").innerText = cache.getDomains().length;

select("#delete_all_button").innerHTML = "";
if (domains.length) {
var button = document.createElement("button");
button.onclick = removeAllForFilter;
button.innerText = "delete all " + domains.length;
select("#delete_all_button").appendChild(button);
}

resetTable();
var table = select("#cookies");

domains.forEach(function(domain) {
var cookies = cache.getCookies(domain);
var row = table.insertRow(-1);
row.insertCell(-1).innerText = domain;
var cell = row.insertCell(-1);
cell.innerText = cookies.length;
cell.setAttribute("class", "cookie_count");

var button = document.createElement("button");
button.innerText = "delete";
button.onclick = (function(dom){
return function() {
removeCookiesForDomain(dom);
};
}(domain));
var cell = row.insertCell(-1);
cell.appendChild(button);
cell.setAttribute("class", "button");
});
}

function focusFilter() {
select("#filter").focus();
}

function resetFilter() {
var filter = select("#filter");
filter.focus();
if (filter.value.length > 0) {
filter.value = "";
reloadCookieTable();
}
}

var ESCAPE_KEY = 27;
window.onkeydown = function(event) {
if (event.keyCode == ESCAPE_KEY) {
resetFilter();
}
}

function update(cookie) {
cache.remove(cookie);
scheduleReloadCookieTable();
}

function onload() {
focusFilter();
var timer = new Timer();
win.webContents.session.cookies.get({}, function(error, cookies) {
if (error) throw error;
console.log(cookies);
start = new Date();
for (var i in cookies) {
cache.add(cookies[i]);
}
timer.reset();
reloadCookieTable();
});
}

document.addEventListener('DOMContentLoaded', function() {
onload();
document.body.addEventListener('click', focusFilter);
document.querySelector('#remove_button').addEventListener('click', removeAll);
document.querySelector('#filter_div input').addEventListener(
'input', reloadCookieTable);
document.querySelector('#filter_div button').addEventListener(
'click', resetFilter);
});
5 changes: 5 additions & 0 deletions cookies/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name" : "cookies-demo",
"version" : "0.1.0",
"main" : "main.js"
}

0 comments on commit 76a7ebe

Please sign in to comment.