Skip to content

Commit

Permalink
Added support for saving API Key in local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Yang authored and Vincent Yang committed Dec 31, 2016
1 parent 8e19b18 commit 7f2938a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
40 changes: 40 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ var queries = {
}
};

//check for local storage
var ls = {
get: function () {
var test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch(e) {
return false;
}
}
};


$("document").ready(function() {
//handling upload event
Expand All @@ -33,11 +47,37 @@ $("document").ready(function() {
activityChart(rawActivityData);
});

//if a key is saved, load it in the 'Enter API key' field
if (ls) {
// We can use localStorage
if(!localStorage.getItem('rescuetimeApiKey')) {
//no stored API Key
console.log("No stored API Key");
} else {
key = localStorage.getItem('rescuetimeApiKey');
document.getElementById('api_key').value = key;
document.getElementById('api_key').placeholder = '';
console.log("retrieving key from storage");
}
}
});

function storeKey(key) {
if (ls) {
localStorage.setItem('rescuetimeApiKey', key);
console.log("Saving key");
}
}


function init() {
var key = document.getElementById('api_key').value.trim();

// Only store the key if the save box is checked
if (document.getElementById('save').checked) {
storeKey(key);
}

if (key.length > 5) {
usingFiles = false;

Expand Down
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ <h2>Get the data from RescueTime</h2>
<button type="button" class="btn btn-primary" onClick="init()">Start analysis</button>
<div class="checkbox">
<label>
<input type="checkbox" id="download"> Download JSON file for the selected range. <a href="https://github.com/ilbonte/rescuetime-again#notes" target="_blank">Why?</a>
<input type="checkbox" id="download"> Download JSON file for the selected range. <a href="https://github.com/ilbonte/rescuetime-again#notes" target="_blank">Why?</a><br>
<input type="checkbox" id="save" checked> Save API Key</a>
</label>
</div>
</div>
Expand Down

0 comments on commit 7f2938a

Please sign in to comment.