Updates
This commit is contained in:
parent
7e811c2855
commit
f4755cb4b1
6 changed files with 90 additions and 17 deletions
|
@ -1,8 +1,11 @@
|
|||
function check_first_install() {
|
||||
if (localStorage.getItem('first_install_ran'))
|
||||
return;
|
||||
|
||||
if (localStorage.getItem('first_install_ran')) {
|
||||
localStorage.setItem('first_install_ran', 1);
|
||||
chrome.tabs.create({url: "options.html"});
|
||||
} else {
|
||||
if (isRegistrationDone()) {
|
||||
subscribeToPush(function(message) {
|
||||
console.log("Got message from " + message.source + ": \"" + getString(message.message));
|
||||
storeMessage(message);
|
||||
});
|
||||
}
|
||||
}
|
||||
check_first_install();
|
||||
|
|
38
helpers.js
38
helpers.js
|
@ -37,9 +37,13 @@ function base64DecToArr (sBase64, nBlocksSize) {
|
|||
/*********************************
|
||||
*** Type conversion utilities ***
|
||||
*********************************/
|
||||
var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
|
||||
var StaticUint8ArrayProto = new Uint8Array().__proto__;
|
||||
function getString(thing) {
|
||||
if (thing == "[object Uint8Array]")
|
||||
if (thing.__proto__ == StaticUint8ArrayProto)
|
||||
return String.fromCharCode.apply(null, thing);
|
||||
if (thing != undefined && thing.__proto__ == StaticByteBufferProto)
|
||||
return thing.toString("utf8");
|
||||
return thing;
|
||||
}
|
||||
|
||||
|
@ -85,6 +89,8 @@ var storage = {};
|
|||
|
||||
storage.putEncrypted = function(key, value) {
|
||||
//TODO
|
||||
if (value === undefined)
|
||||
throw "Tried to store undefined";
|
||||
localStorage.setItem("e" + key, JSON.stringify(getString(value)));
|
||||
}
|
||||
|
||||
|
@ -97,6 +103,8 @@ storage.getEncrypted = function(key, defaultValue) {
|
|||
}
|
||||
|
||||
storage.putUnencrypted = function(key, value) {
|
||||
if (value === undefined)
|
||||
throw "Tried to store undefined";
|
||||
localStorage.setItem("u" + key, JSON.stringify(getString(value)));
|
||||
}
|
||||
|
||||
|
@ -107,6 +115,34 @@ storage.getUnencrypted = function(key, defaultValue) {
|
|||
return JSON.parse(value);
|
||||
}
|
||||
|
||||
function registrationDone() {
|
||||
storage.putUnencrypted("registration_done", "");
|
||||
}
|
||||
|
||||
function isRegistrationDone() {
|
||||
return storage.getUnencrypted("registration_done") !== undefined;
|
||||
}
|
||||
|
||||
function getMessageMap() {
|
||||
return storage.getEncrypted("messageMap", {});
|
||||
}
|
||||
|
||||
function storeMessage(outgoingMessageSignal) {
|
||||
var messageMap = getMessageMap();
|
||||
var conversation = messageMap[outgoingMessageSignal.source]; //TODO: Also support Group message IDs here
|
||||
if (conversation === undefined) {
|
||||
conversation = []
|
||||
messageMap[outgoingMessageSignal.source] = conversation;
|
||||
}
|
||||
|
||||
conversation[conversation.length] = { message: getString(outgoingMessageSignal.message),
|
||||
destinations: outgoingMessageSignal.destinations,
|
||||
sender: outgoingMessageSignal.source,
|
||||
timestamp: outgoingMessageSignal.timestamp.div(dcodeIO.Long.fromNumber(1000)).toNumber() };
|
||||
storage.putEncrypted("messageMap", messageMap);
|
||||
chrome.runtime.sendMessage(conversation[conversation.length - 1]);
|
||||
}
|
||||
|
||||
/*******************************************
|
||||
*** Utilities to manage keys/randomness ***
|
||||
*******************************************/
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
},
|
||||
|
||||
"background": {
|
||||
"scripts": ["background.js"]
|
||||
"pages": ["background.html"]
|
||||
},
|
||||
|
||||
"options_page": "options.html"
|
||||
|
|
|
@ -52,7 +52,10 @@ $('#init-go').click(function() {
|
|||
$('#verify3done').html('done');
|
||||
doAjax({call: 'keys', httpType: 'PUT', do_auth: true, jsonData: keys,
|
||||
success_callback: function(response) {
|
||||
$('#verify4done').html('done');
|
||||
$('#complete-number').html(number);
|
||||
$('#verify').hide();
|
||||
$('#setup-complete').show();
|
||||
registrationDone();
|
||||
}, error_callback: function(code) {
|
||||
alert(code); //TODO
|
||||
}
|
||||
|
@ -81,7 +84,7 @@ $('#init-go').click(function() {
|
|||
}
|
||||
});
|
||||
|
||||
if (storage.getUnencrypted("number_id") === undefined) {
|
||||
if (!isRegistrationDone()) {
|
||||
$('#init-setup').show();
|
||||
} else {
|
||||
$('#complete-number').html(storage.getUnencrypted("number_id").split(".")[0]);
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
<html>
|
||||
<head>
|
||||
<script type="text/javascript" src="jquery-2.0.3.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<a id="inbox_link">Inbox</a>
|
||||
<a id="send_link">Send</a>
|
||||
<div id="inbox">
|
||||
<!-- stuff -->
|
||||
<ul id="messages">
|
||||
</ul>
|
||||
</div>
|
||||
<div id="send" style="display:none;"></div>
|
||||
|
||||
|
|
43
popup.js
43
popup.js
|
@ -1,14 +1,45 @@
|
|||
$('#inbox_link').onclick = function() {
|
||||
$('#inbox').style('display: none;'); $('#send').style('display: block;');
|
||||
$('#inbox').show();
|
||||
$('#send').hide();
|
||||
}
|
||||
$('#send_link').onclick = function() {
|
||||
$('#send').style('display: block;'); $('#send').style('display: none;');
|
||||
$('#inbox').hide();
|
||||
$('#send').show();
|
||||
}
|
||||
|
||||
if (storage.getUnencrypted("number_id") === undefined)
|
||||
if (storage.getUnencrypted("number_id") === undefined) {
|
||||
chrome.tabs.create({url: "options.html"});
|
||||
else {
|
||||
subscribeToPush(function(message) {
|
||||
console.log("GOT MESSAGE IN POPUP! " + message);
|
||||
} else {
|
||||
function fillMessages() {
|
||||
var MAX_MESSAGES_PER_CONVERSATION = 4;
|
||||
var MAX_CONVERSATIONS = 5;
|
||||
|
||||
var conversations = [];
|
||||
|
||||
var messageMap = getMessageMap();
|
||||
for (conversation in messageMap) {
|
||||
var messages = messageMap[conversation];
|
||||
messages.sort(function(a, b) { return b.timestamp - a.timestamp; });
|
||||
conversations[conversations.length] = messages;
|
||||
}
|
||||
|
||||
conversations.sort(function(a, b) { return b[0].timestamp - a[0].timestamp });
|
||||
|
||||
var ul = $('#messages');
|
||||
ul.html('');
|
||||
for (var i = 0; i < MAX_CONVERSATIONS && i < conversations.length; i++) {
|
||||
var conversation = conversations[i];
|
||||
ul.append('<li>');
|
||||
for (var j = 0; j < MAX_MESSAGES_PER_CONVERSATION && conversation.length; j++) {
|
||||
ul.append(JSON.stringify(conversation[j]));
|
||||
}
|
||||
ul.append('</li>');
|
||||
}
|
||||
}
|
||||
|
||||
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
||||
console.log("Got push message from background.js " + JSON.stringify(request));
|
||||
fillMessages();
|
||||
});
|
||||
fillMessages();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue