Skip to content

Commit

Permalink
Save Auth0ID in local storage and add notifications for extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Josephine-Chen committed Mar 4, 2017
1 parent d091845 commit f675dbe
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 49 deletions.
40 changes: 9 additions & 31 deletions extension/background.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
// (function() {
// var httpRequest;
// document.getElementById("ajaxButton").onclick = function() { makeRequest('localhost:7777/api/users/'); };

// function makeRequest(url) {
// console.log('making request');
// httpRequest = new XMLHttpRequest();

// if (!httpRequest) {
// console.log('Giving up :( Cannot create an XMLHTTP instance');
// //alert('Giving up :( Cannot create an XMLHTTP instance');
// return false;
// }
// httpRequest.onreadystatechange = showContents;
// httpRequest.open('GET', url);
// httpRequest.send();
// }

// function showContents() {
// if (httpRequest.readyState === XMLHttpRequest.DONE) {
// if (httpRequest.status === 200) {
// console.log(httpRequest.responseText);
// //alert(httpRequest.responseText);
// } else {
// console.log('There was a problem with the request.');
// //alert('There was a problem with the request.');
// }
// }
// }
// })();
// Notification
chrome.alarms.create("notificationAlarm", {periodInMinutes: 2});
chrome.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name === "notificationAlarm") {
console.log('alarm');
chrome.notifications.create('notificationAlarm', {type: 'basic', iconUrl: 'icon128.png', title: 'Notification', message: 'This is a notification!'});
}
});

//Clear stats
// Clear stats
function clearStats() {
if (config.clearStatsInterval < 3600) {
config.nextTimeToClear = 0;
Expand Down
4 changes: 2 additions & 2 deletions extension/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function Config() {
}
//Handle time display format
Config.timeDisplayFormatEnum = {
PRETTY: 0,
//PRETTY: 0,
MINUTES: 1
};

Expand Down Expand Up @@ -43,7 +43,7 @@ Config.prototype.isIgnoredSite = function(site) {
Object.defineProperty(Config.prototype, "timeDisplayFormat", {
get: function() {
if (!localStorage.timeDisplayFormat) {
localStorage.timeDisplayFormat = Config.timeDisplayFormatEnum.PRETTY;
localStorage.timeDisplayFormat = Config.timeDisplayFormatEnum.MINUTES;
}
return localStorage.timeDisplayFormat;
},
Expand Down
4 changes: 4 additions & 0 deletions extension/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<script src="options.js"></script>

<body>
<b>Auth0 ID</b>:
<input type="text" id="auth0_id"></input>
<button id="add_auth0_id">Store ID</button>
<br />
<b>Time Display</b>:
<select id="time_display">
<option value="0">Pretty</option>
Expand Down
10 changes: 10 additions & 0 deletions extension/options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
var config = new Config();
var sites = new Sites(config);

//Add Auth0id
function addAuth0Id(){
console.log('clickity click');
var auth0_input = document.getElementById("auth0_id");
localStorage.auth0_id = auth0_input.value;
console.log(localStorage.auth0_id);
}

//Update the interval to clear stats
function updateClearStatsInterval() {
var select = document.getElementById("clear_stats_interval");
Expand Down Expand Up @@ -109,5 +117,7 @@ document.addEventListener("DOMContentLoaded", function () {
"change", updateTimeDisplay);
document.getElementById("download").addEventListener(
"click", download);
document.getElementById("add_auth0_id").addEventListener(
"click", addAuth0Id);
restoreOptions();
});
1 change: 0 additions & 1 deletion extension/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@
<tbody id="stats_tbody"></tbody>
</table>
</div>

</body>
</html>
30 changes: 17 additions & 13 deletions extension/popup.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
var config = new Config();
var gsites = new Sites(config);

function test() {
var newInput = document.createElement("input");
newInput.setAttribute("id", "authId");
document.body.appendChild(newInput);
var testBtn = document.createElement("button");
var testBtnTxt = document.createTextNode("Send Data");
testBtn.appendChild(testBtnTxt);
document.body.appendChild(testBtn);
testBtn.setAttribute("id", "testButton");
testBtn.onclick = function() {
function sendToApp() {
// var newInput = document.createElement("input");
// newInput.setAttribute("id", "authId");
// document.body.appendChild(newInput);
var sendDataBtn = document.createElement("button");
var sendDataBtnTxt = document.createTextNode("Send Data");
sendDataBtn.appendChild(sendDataBtnTxt);
document.body.appendChild(sendDataBtn);
sendDataBtn.setAttribute("id", "testButton");
sendDataBtn.onclick = function() {
// Save auth0id
//localStorage.auth0_id = newInput.value;
// Get all sites and store in array to be parsed and stored in db
//url: `http://localhost:7777/api/users/${newInput.value}/extension_data`,
console.log(JSON.stringify(localStorage.auth0_id));
var allSites = [];
for(var prop in gsites.sites) {
allSites.push({url: prop, time: gsites.sites[prop], freq: 0});
}
$.ajax({
type: 'POST',
url: `http://localhost:7777/api/users/${newInput.value}/extension_data`,
url: `http://localhost:7777/api/users/${localStorage.auth0_id}/extension_data`,
contentType: 'application/json',
data: JSON.stringify({urls:allSites}),
data: JSON.stringify({ urls:allSites }),
success: function(data) {
console.log('success!', data);
},
Expand Down Expand Up @@ -191,7 +195,7 @@ function clearStats() {

function initialize() {
addLocalDisplay();
test();
sendToApp();

if (config.lastClearTime) {
var div = document.getElementById("lastClear");
Expand Down
3 changes: 1 addition & 2 deletions server/controllers/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,7 @@ function upsertExtension(req, res) {
urls.forEach((url) => {
db.Extension.upsert({url: url.url, time_spent: url.time, freq: url.freq, UserId: user.id}).then((data) => {
res.status(200).json({
data: data,
success: 'Successfully upserted', userId
data: data
});
}).catch((err) => { res.send({'ERROR: UPSERT EXTENSION': err}); });
});
Expand Down

0 comments on commit f675dbe

Please sign in to comment.