Skip to content

Commit 35c64c3

Browse files
committed
groundwork for timers
1 parent c7d387b commit 35c64c3

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

scripts/tab.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var Tab = function (domain, favicon) {
22
this.url = domain;
3-
this.startTime = Date.now;
3+
this.startTime = new Date();
44
this.favicon = favicon;
55
};
66

@@ -9,5 +9,14 @@ Tab.prototype = {
99
startTime: {},
1010
summaryTime: {},
1111
favicon: {},
12-
percentage: {}
12+
percentage: {},
13+
14+
start: function(){
15+
this.startTime = new Date();
16+
},
17+
18+
stop: function(){
19+
this.summaryTime = new Date() - this.startTime;
20+
this.startTime = null;
21+
}
1322
};

scripts/webTimer.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ WebTimer.prototype = {
1313
var newTab = new Tab(domain, tab.favIconUrl);
1414
this.tabs.push(newTab);
1515
}
16-
this.startTimeTracker(tab);
16+
this.startTimeTracker(this.getTab(domain));
1717
this.currentTab = domain;
18+
this.stopTimeTracker(this.getTab(this.currentTab));
1819
}
1920
}
2021
},
@@ -25,6 +26,10 @@ WebTimer.prototype = {
2526
else return true;
2627
},
2728

29+
getTab: function (domain) {
30+
return this.tabs.find(o => o.url === domain);
31+
},
32+
2833
extractHostname: function (url) {
2934
var hostname;
3035

@@ -50,6 +55,12 @@ WebTimer.prototype = {
5055
},
5156

5257
startTimeTracker: function (tab) {
53-
var tab = new Tab(tab.url, tab.favIconUrl);
58+
tab.start();
59+
chrome.storage.sync.set({'tabs': JSON.stringify(this.tabs)});
60+
},
61+
62+
stopTimeTracker: function (tab) {
63+
tab.stop();
64+
chrome.storage.sync.set({'tabs': JSON.stringify(this.tabs)});
5465
}
5566
};

scripts/webact.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
window.onload = function () {
2-
var table = document.getElementById('resultTable');
3-
var tabs = chrome.extension.getBackgroundPage().timer.tabs;
2+
var resultTabs = {};
3+
//var tabs = chrome.extension.getBackgroundPage().timer.tabs;
4+
chrome.storage.sync.get('tabs', function(val){
5+
resultTabs = JSON.parse(val.tabs);
6+
getTabsFromStorage(resultTabs);
7+
});
8+
};
9+
10+
function getTabsFromStorage(tabs){
411
for (var i = 0; i < tabs.length; i++) {
12+
var table = document.getElementById('resultTable');
513
var div = document.createElement('div');
614
div.classList.add('inline-flex');
715

@@ -10,11 +18,11 @@ window.onload = function () {
1018
img.setAttribute('src', tabs[i].favicon);
1119

1220
var span = document.createElement('span');
13-
span.innerText = tabs[i].url;
21+
span.innerText = tabs[i].url + ' ' + tabs[i].summaryTime;
1422
span.classList.add('margin-left-5');
1523

1624
div.appendChild(img);
1725
div.appendChild(span);
1826
table.appendChild(div);
1927
}
20-
};
28+
}

0 commit comments

Comments
 (0)