Skip to content

Commit 45bc7b0

Browse files
committed
add dayly feature
1 parent 1359e77 commit 45bc7b0

File tree

5 files changed

+112
-30
lines changed

5 files changed

+112
-30
lines changed

index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
<p class="title">Web Activity Time Tracker</p>
1919
</div>
2020
<div id="buttons">
21-
<input type="button" value="Today" class="active"/>
22-
<input type="button" value="All"/>
23-
<input type="button" value="By days"/>
21+
<input type="button" value="Today" class="active" id="btnToday"/>
22+
<input type="button" value="All" id="btnAll"/>
23+
<input type="button" value="By days" id="btnByDays"/>
2424
</div>
2525
<div id="chart" class="margin-top-7"></div>
2626
<hr>
2727
<div class="list-of-site" id="resultTable"></div>
2828
<hr>
29-
<div class="inline-flex">
29+
<div class="inline-flex" id="total">
3030
<span class="span-total">Total</span>
3131
<span class="span-time" id="totalTime"></span>
3232
</div>

scripts/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function backgroundCheck() {
2828
activity.setCurrentActiveTab(tab.url);
2929
chrome.idle.queryState(SETTINGS_INTERVAL_INACTIVITY, function (state) {
3030
if (state === 'active') {
31-
tab.summaryTime += 1;
31+
tab.incSummaryTime();
3232
chrome.browserAction.setBadgeText({
3333
tabId: activeTab.id,
3434
text: String(convertSummaryTimeToBadgeString(tab.summaryTime))

scripts/storage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

33
class LocalStorage {
4-
load(name, callback){
4+
load(name, callback, options){
55
chrome.storage.local.get(name, function (item){
66
if (item[name] !== undefined){
77
var result = JSON.parse(item[name]);
88
if (result !== undefined)
9-
callback(result);
9+
callback(result, options);
1010
}
1111
});
1212
}

scripts/tab.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
'use strict';
22

33
class Tab {
4-
constructor(url, favicon){
4+
constructor(url, favicon) {
55
this.url = url;
66
this.favicon = favicon;
77
this.summaryTime = 0;
8+
this.days = [];
9+
}
10+
11+
incSummaryTime() {
12+
var today = new Date().toLocaleDateString();
13+
var day = this.days.find(x => x.date == today);
14+
if (day === undefined) {
15+
this.addNewDay(today);
16+
}
17+
else {
18+
this.summaryTime += 1;
19+
day['summary'] += 1;
20+
}
21+
}
22+
23+
addNewDay(today){
24+
this.summaryTime += 1;
25+
this.days.push(
26+
{
27+
'date': today,
28+
'summary': this.summaryTime
29+
}
30+
);
831
}
932
};

scripts/webact.js

Lines changed: 81 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,103 @@
22

33
var storage = new LocalStorage();
44
var totalTime;
5+
var tabsFromStorage;
6+
7+
document.addEventListener('DOMContentLoaded', function () {
8+
document.getElementById('btnToday').addEventListener('click', function () {
9+
document.getElementById('btnToday').classList.add('active');
10+
document.getElementById('btnAll').classList.remove('active');
11+
document.getElementById('btnByDays').classList.remove('active');
12+
getDataFromStorageToday();
13+
});
14+
document.getElementById('btnAll').addEventListener('click', function () {
15+
document.getElementById('btnAll').classList.add('active');
16+
document.getElementById('btnToday').classList.remove('active');
17+
document.getElementById('btnByDays').classList.remove('active');
18+
getDataFromStorageAll();
19+
});
20+
document.getElementById('btnByDays').addEventListener('click', function () {
21+
document.getElementById('btnByDays').classList.add('active');
22+
document.getElementById('btnAll').classList.remove('active');
23+
document.getElementById('btnToday').classList.remove('active');
24+
25+
document.getElementById('resultTable').innerHTML = null;
26+
document.getElementById('chart').innerHTML = null;
27+
});
28+
});
529

6-
getDataFromStorage();
30+
getDataFromStorageToday();
731

8-
function getDataFromStorage() {
32+
function getDataFromStorageToday() {
933
storage.load(STORAGE_TABS, getTabsFromStorage);
1034
}
1135

12-
function getTabsFromStorage(tabs) {
36+
function getDataFromStorageAll() {
37+
storage.load(STORAGE_TABS, getTabsFromStorage, true);
38+
}
39+
40+
function getTabsFromStorage(tabs, options) {
41+
tabsFromStorage = tabs;
42+
var targetTabs = [];
43+
1344
var table = document.getElementById('resultTable');
1445
table.innerHTML = null;
15-
tabs = tabs.sort(function (a, b) {
16-
return b.summaryTime - a.summaryTime;
17-
});
46+
document.getElementById('chart').innerHTML = null;
1847

19-
totalTime = setTotalTime(tabs);
48+
if (options !== undefined && options === true) {
49+
targetTabs = tabs.sort(function (a, b) {
50+
return b.summaryTime - a.summaryTime;
51+
});
52+
53+
totalTime = setTotalTime(targetTabs);
54+
} else {
55+
var today = new Date().toLocaleDateString();
56+
57+
targetTabs = tabs.filter(x => x.days.find(s => s.date === today));
58+
targetTabs = targetTabs.sort(function (a, b) {
59+
return b.days.find(s => s.date === today) - a.days.find(s => s.date === today);
60+
});
61+
62+
totalTime = setTotalTime(targetTabs, today);
63+
}
2064

2165
var currentTab = getCurrentTab();
2266

2367
var tabsForChart = [];
24-
for (var i = 0; i < tabs.length; i++) {
68+
for (var i = 0; i < targetTabs.length; i++) {
2569
var div = document.createElement('div');
2670
div.classList.add('inline-flex');
2771

2872
var img = document.createElement('img');
2973
img.classList.add('favicon');
3074
img.setAttribute('height', 15);
31-
img.setAttribute('src', tabs[i].favicon);
75+
img.setAttribute('src', targetTabs[i].favicon);
3276

3377
var spanUrl = document.createElement('span');
3478
spanUrl.classList.add('span-url');
35-
spanUrl.innerText = tabs[i].url;
36-
if (tabs[i].url == currentTab) {
79+
spanUrl.innerText = targetTabs[i].url;
80+
if (targetTabs[i].url == currentTab) {
3781
spanUrl.classList.add('span-active-url');
3882
}
3983

84+
var summaryTime;
85+
if (today !== undefined) {
86+
summaryTime = targetTabs[i].days.find(x => x.date == today).summary;
87+
} else {
88+
summaryTime = targetTabs[i].summaryTime;
89+
}
90+
4091
var spanPercentage = document.createElement('span');
4192
spanPercentage.classList.add('span-percentage');
42-
spanPercentage.innerText = getPercentage(tabs[i].summaryTime);
93+
spanPercentage.innerText = getPercentage(summaryTime);
4394

4495
if (i <= 5)
45-
addTabForChart(tabsForChart, tabs[i].url, tabs[i].summaryTime);
46-
else addTabOthersForChart(tabsForChart, tabs[i].summaryTime);
96+
addTabForChart(tabsForChart, targetTabs[i].url, summaryTime);
97+
else addTabOthersForChart(tabsForChart, summaryTime);
4798

4899
var spanTime = document.createElement('span');
49100
spanTime.classList.add('span-time');
50-
spanTime.innerText = convertSummaryTimeToString(tabs[i].summaryTime);
101+
spanTime.innerText = convertSummaryTimeToString(summaryTime);
51102

52103
div.appendChild(img);
53104
div.appendChild(spanUrl);
@@ -60,9 +111,15 @@ function getTabsFromStorage(tabs) {
60111
setActiveTooltipe(currentTab);
61112
}
62113

63-
function setTotalTime(tabs) {
64-
var summaryTimeList = tabs.map(function (a) { return a.summaryTime; });
65-
var total = summaryTimeList.reduce(function (a, b) { return a + b; })
114+
function setTotalTime(tabs, today) {
115+
var total;
116+
if (today !== undefined) {
117+
var summaryTimeList = tabs.map(function (a) { return a.days.find(s => s.date === today).summary; });
118+
total = summaryTimeList.reduce(function (a, b) { return a + b; })
119+
} else {
120+
var summaryTimeList = tabs.map(function (a) { return a.summaryTime; });
121+
total = summaryTimeList.reduce(function (a, b) { return a + b; })
122+
}
66123
document.getElementById('totalTime').innerText = convertSummaryTimeToString(total);
67124

68125
return total;
@@ -102,7 +159,7 @@ function addTabOthersForChart(tabsForChart, summaryTime) {
102159
);
103160
}
104161
else {
105-
tab['summary'] = tab['summary'] + summaryTime;
162+
tab['summary'] += summaryTime;
106163
tab['percentage'] = getPercentageForChart(tab['summary']);
107164
}
108165
}
@@ -121,7 +178,9 @@ function drawChart(tabs) {
121178
.call(donut); // draw chart in div
122179
}
123180

124-
function setActiveTooltipe(currentTab){
125-
var event = new Event("mouseenter");
126-
document.getElementById(currentTab).dispatchEvent(event);
181+
function setActiveTooltipe(currentTab) {
182+
if (currentTab !== '') {
183+
var event = new Event("mouseenter");
184+
document.getElementById(currentTab).dispatchEvent(event);
185+
}
127186
}

0 commit comments

Comments
 (0)