Skip to content

Commit

Permalink
Comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
aghorler committed Jul 5, 2017
1 parent 2327427 commit e05659f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
12 changes: 12 additions & 0 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Function to verify that a user has created a keypair, and act accordingly. */
function userVerify(){
chrome.storage.local.get('publicKey', function(verify){
if(verify.publicKey !== undefined){
Expand All @@ -9,7 +10,9 @@ function userVerify(){
});
}

/* Function to encrypt and add a bookmark. */
function addBookmark(){
/* Query active tab. */
chrome.tabs.query({
'active': true,
'lastFocusedWindow': true
Expand All @@ -18,22 +21,28 @@ function addBookmark(){
bookmarks: [],
publicKey: ""
}, function(bookmarkItems){
/* Generate bookmark from tab. */
var tempTitle = tabItems[0].title;
var title = prompt("Bookmark title", tempTitle);

if(title !== null){
var url = tabItems[0].url;

/* Prepare tab title for encryption. */
var optionsTitle = {
data: title,
publicKeys: openpgp.key.readArmored(bookmarkItems.publicKey).keys
};

/* Encrypt tab title, and then proceed further. */
openpgp.encrypt(optionsTitle).then(function(ciphertextTitle){
/* Prepare tab url for encryption. */
var optionsUrl = {
data: url,
publicKeys: openpgp.key.readArmored(bookmarkItems.publicKey).keys
};

/* Encrypt tab title, and then store encrypted bookmark. */
openpgp.encrypt(optionsUrl).then(function(ciphertextUrl){
bookmarkItems.bookmarks['bookmarks'].push({"title": ciphertextTitle.data, "url": ciphertextUrl.data});

Expand All @@ -48,6 +57,7 @@ function addBookmark(){
});
}

/* Preform various tasks on first installation. */
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "install"){
chrome.storage.local.set({bookmarks: {"bookmarks": []}});
Expand All @@ -59,8 +69,10 @@ chrome.runtime.onInstalled.addListener(function(details){
}
});

/* Call userVerify() on context meny click. */
chrome.contextMenus.onClicked.addListener(userVerify);

/* Open bookmarks page on extension toolbar icon click. */
chrome.browserAction.onClicked.addListener(function(){
chrome.tabs.create({url: "/html/bookmarks.html"});
});
31 changes: 29 additions & 2 deletions js/process.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Function to verify that a user has created a keypair, and alter the page accordingly. */
function checkForNewUser(){
chrome.storage.local.get('publicKey', function(passwordCheck){
if(passwordCheck.publicKey !== undefined){
Expand All @@ -6,26 +7,30 @@ function checkForNewUser(){
}
else{
document.getElementById("submit").style.display = 'none';
document.getElementById("setup-account").addEventListener("click", createPassword);
document.getElementById("setup-account").addEventListener("click", generateKeyPair);
}
});
}

function createPassword(){
/* Function to generate a keypair. */
function generateKeyPair(){
var newPassword = document.getElementById("new-password").value;
var newPasswordRepeat = document.getElementById("new-password-repeat").value;

/* Verify private key encryption password. */
if(newPassword !== ""){
if(newPassword === newPasswordRepeat){
document.getElementById("setup-account").innerHTML = "Generating keys";
document.getElementById("setup-account").disabled = true;

/* Prepare to generate keypair. */
var options = {
userIds: [{ name:'Encrypted Bookmarks User', email:'example@example.com' }],
numBits: 2048,
passphrase: newPassword
};

/* Generate keypair, store, and reload the page. */
openpgp.generateKey(options).then(function(key){
var privkey = key.privateKeyArmored;
var pubkey = key.publicKeyArmored;
Expand All @@ -45,6 +50,7 @@ function createPassword(){
}
}

/* Function to retrieve and decrypt bookmarks. */
function loadBookmarks(){
chrome.storage.local.get({
privateKey: "",
Expand All @@ -53,16 +59,23 @@ function loadBookmarks(){
var password = document.getElementById("password").value;

if(password !== ""){
/* Decrypt private key with entered password. */
var privKeyObj = openpgp.key.readArmored(items.privateKey).keys[0];
privKeyObj.decrypt(password);

/* Hide submit button. */
document.getElementById("submit").style.display = 'none';

/* Manual loop to resolve asynchronous issues. */
var store = items.bookmarks;
if(store['bookmarks'].length > 0){

/* Function to decrypt the title and url of a bookmark, and write it to the page. */
var i = 0;
function decryptBookmark(){
/* Only proceed if i does not exceed the total number of bookmarks. */
if(i < store['bookmarks'].length){
/* Create various HTML elements. */
var p1 = document.createElement("p");
var span1 = document.createElement("span");
var span2 = document.createElement("span");
Expand All @@ -71,34 +84,43 @@ function loadBookmarks(){
var a2 = document.createElement("a");
var display = document.getElementById("content");

/* Add various HTML elements to page. */
p1.appendChild(span1);
p1.appendChild(span2);
p1.appendChild(span3);
span1.appendChild(a1);
span3.appendChild(a2);
display.appendChild(p1);

/* Assign each bookmark link a chronological id. */
a1.id = "a" + i;

var urlEnc = store['bookmarks'][i].url;
var titleEnc = store['bookmarks'][i].title;

/* Prepare url for decryption. */
optionsUrl = {
message: openpgp.message.readArmored(urlEnc),
privateKey: privKeyObj
};

/* Decrypt url, and then proceed. */
openpgp.decrypt(optionsUrl).then(function(plaintextUrl){
/* Write url to page. */
document.getElementById("a" + i).href = plaintextUrl.data;

/* Prepare title for decryption. */
optionsTitle = {
message: openpgp.message.readArmored(titleEnc),
privateKey: privKeyObj
};

/* Decrypt title, and then proceed. */
openpgp.decrypt(optionsTitle).then(function(plaintextTitle){
/* Write title to page. */
document.getElementById("a" + i).textContent = plaintextTitle.data;

/* Write various other things to page. */
a1.target = "_blank";
a2.href = "#";
a2.textContent = "Delete";
Expand All @@ -107,14 +129,17 @@ function loadBookmarks(){
p1.id = "b" + i;
span2.textContent = " | ";

/* Add EventListener to each delete link. */
document.getElementById(i).addEventListener("click", deleteBookmark);

/* Increment i, and call function again. */
i++;
decryptBookmark();
});
});
}
}
/* Call function first time. */
decryptBookmark();
}
else{
Expand All @@ -127,6 +152,7 @@ function loadBookmarks(){
});
}

/* Function to delete a bookmark. */
function deleteBookmark(){
var index = this.id;
chrome.storage.local.get('bookmarks', function(items){
Expand All @@ -142,4 +168,5 @@ function deleteBookmark(){
});
}

/* Call checkForNewUser on page load. */
document.addEventListener('DOMContentLoaded', checkForNewUser);

0 comments on commit e05659f

Please sign in to comment.