Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store ThinkUp settings in sync storage #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
function checkInsights(timecheck) {
var api_url = localStorage["install_url"] + "api/v1/insight.php" + "?since=" + timecheck
+ "&as=" + localStorage["install_api_key"] + '&un=' + encodeURI(localStorage["email_address"]);
//var api_url = localStorage["install_url"] + "test.json";
console.log(Date() + " checking for new insights " + api_url);
chrome.storage.sync.get(
null,
function(ThinkUpSettings) {
var api_url = ThinkUpSettings.install_url + "api/v1/insight.php" + "?since=" + timecheck
+ "&as=" + ThinkUpSettings.install_api_key + '&un=' + encodeURI(ThinkUpSettings.email_address);
//var api_url = localStorage["install_url"] + "test.json";
console.log(Date() + " checking for new insights " + api_url);

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var data = JSON.parse(xhr.responseText);
if (typeof data.error === 'undefined') {
for (var i = 0; i < data.length; i++) {
var insight = data[i];
var htmlstripper = document.createElement("div");
htmlstripper.innerHTML = insight.text;
var title = insight.prefix.replace(":","");
var notification = window.webkitNotifications.createNotification("icon.png", title, htmlstripper.innerText);
notification.show();
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var data = JSON.parse(xhr.responseText);
if (typeof data.error === 'undefined') {
for (var i = 0; i < data.length; i++) {
var insight = data[i];
var htmlstripper = document.createElement("div");
htmlstripper.innerHTML = insight.text;
var title = insight.prefix.replace(":","");
var notification = window.webkitNotifications.createNotification("icon.png", title, htmlstripper.innerText);
notification.show();
}
} else {
console.log('Error: ' + data.error.message);
}
}
} else {
console.log('Error: ' + data.error.message);
}
xhr.open("GET", api_url, true);
xhr.send();
}
}
xhr.open("GET", api_url, true);
xhr.send();
);
return Math.round(+new Date() / 1000);
}

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"default_title": "ThinkUp Notifier",
"default_popup": "popup.html"
},
"permissions": ["http://*/", "tabs", "notifications"]
"permissions": ["http://*/", "tabs", "notifications", "storage"]
}
7 changes: 6 additions & 1 deletion options.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen" type="text/css">
<style type="text/css">
body {
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
Expand Down Expand Up @@ -37,6 +37,10 @@
padding: 7px 9px;
}

#status {
margin-top: 20px;
}

</style>
</head>
<body>
Expand All @@ -54,6 +58,7 @@ <h2 class="form-options-heading">
<input type="text" class="input-block-level" name="install_api_key" id="install_api_key">

<button class="btn btn-large" type="submit" id="save">Save Settings</button>
<br/>
<div id="status"></div>

</form>
Expand Down
69 changes: 37 additions & 32 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
// Saves options to localStorage.
function save_options() {
var install_url = document.getElementById("install_url").value;
localStorage["install_url"] = install_url;
// Saves options to sync storage.
function save_options(e) {
e.preventDefault();

var install_api_key = document.getElementById("install_api_key").value;
localStorage["install_api_key"] = install_api_key;
var ThinkUpSettings = {}

var email_address = document.getElementById("email_address").value;
localStorage["email_address"] = email_address;
ThinkUpSettings.install_url = document.getElementById("install_url").value.slice(-1) == '/' ?
document.getElementById("install_url").value : document.getElementById("install_url").value+'/';
ThinkUpSettings.install_api_key = document.getElementById("install_api_key").value;
ThinkUpSettings.email_address = document.getElementById("email_address").value;

// Update status to let user know options were saved.
var status = document.getElementById("status");
status.innerHTML = "Options saved.";
setTimeout(function() {
status.innerHTML = "";
}, 5000);
if (!ThinkUpSettings.install_url || !ThinkUpSettings.install_api_key || !ThinkUpSettings.email_address) {
var status = document.getElementById("status");
status.innerHTML = '<div class="alert alert-error">All the fields are required!<br/>Options not saved.</div>';
return;
}

chrome.storage.sync.set(
ThinkUpSettings,
function() {
// Update status to let user know options were saved.
var status = document.getElementById("status");
status.innerHTML = '<div class="alert alert-success">Options saved.</div>';
setTimeout(function() {
status.innerHTML = "";
}, 5000);
}
);
}

// Restores option fields to saved values from localStorage.
function restore_options() {
// Initialize the option controls.
var install_url = localStorage["install_url"];
if (!install_url) {
return;
}
document.getElementById("install_url").value = install_url;
chrome.storage.sync.get(
null,
function(ThinkUpSettings) {
if (!ThinkUpSettings.install_url || !ThinkUpSettings.install_api_key || !ThinkUpSettings.email_address) {
return;
}

var install_api_key = localStorage["install_api_key"];
if (!install_api_key) {
return;
}
document.getElementById("install_api_key").value = install_api_key;

var email_address = localStorage["email_address"];
if (!email_address) {
return;
}
document.getElementById("email_address").value = email_address;
document.getElementById("install_url").value = ThinkUpSettings.install_url;
document.getElementById("install_api_key").value = ThinkUpSettings.install_api_key;
document.getElementById("email_address").value = ThinkUpSettings.email_address;
}
);
}

document.addEventListener('DOMContentLoaded', restore_options);
document.querySelector('#save').addEventListener('click', save_options);
document.querySelector('#save').addEventListener('click', save_options);
15 changes: 10 additions & 5 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
if (localStorage["install_url"]) {
document.getElementById("thinkup-if").src = localStorage["install_url"];
} else {
chrome.tabs.create({url: chrome.extension.getURL("options.html"), selected: true});
}
chrome.storage.sync.get(
null,
function(ThinkUpSettings) {
if (ThinkUpSettings.install_url) {
document.getElementById("thinkup-if").src = ThinkUpSettings.install_url;
} else {
chrome.tabs.create({url: chrome.extension.getURL("options.html"), selected: true});
}
}
);