Skip to content

Commit caebcf3

Browse files
committed
Refact time intervals
1 parent 84f2f35 commit caebcf3

File tree

12 files changed

+139
-78
lines changed

12 files changed

+139
-78
lines changed

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</div>
3131
<div id="buttons" class="btn-block">
3232
<a class="active" id="btnToday">Today</a>
33-
<a id="btnAll">All</a>
33+
<a id="btnAll">All time</a>
3434
<a id="btnByDays">By days</a>
3535
<a id="settings">Settings</a>
3636
</div>

src/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
"name": "Web Activity Time Tracker",
55
"short_name": "Web Time Tracker",
6-
"version": "1.2.0",
6+
"version": "1.2.2",
77
"minimum_chrome_version": "26",
88

99
"description": "Track and limit time your activity in the browser.",
@@ -35,6 +35,7 @@
3535
"scripts/activity.js",
3636
"scripts/tab.js",
3737
"scripts/background.js",
38+
"scripts/timeInterval.js",
3839
"scripts/restriction.js"],
3940
"persistent": false
4041
},

src/scripts/activity.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Activity {
1010
if (currentTab !== tab.url) {
1111
isDifferentUrl = true;
1212
}
13-
13+
1414
if (this.isNewUrl(domain) && !this.isInBlackList(domain)) {
1515
var favicon = tab.favIconUrl;
1616
if (favicon === undefined) {
@@ -25,10 +25,10 @@ class Activity {
2525
var tabUrl = this.getTab(domain);
2626
if (tabUrl !== undefined)
2727
tabUrl.incCounter();
28+
this.addTimeInterval(domain);
2829
}
2930
}
3031
}
31-
else this.clearCurrentActiveTab();
3232
}
3333

3434
isValidPage(tab) {
@@ -109,12 +109,24 @@ class Activity {
109109
currentTab = '';
110110
}
111111

112+
addTimeInterval(domain) {
113+
var item = timeIntervalList.find(o => o.domain === domain);
114+
if (item != undefined) {
115+
item.addInterval();
116+
} else {
117+
var newInterval = new TimeInterval(new Date().toLocaleDateString("en-US"), domain);
118+
timeIntervalList.push(newInterval);
119+
newInterval.addInterval();
120+
}
121+
}
122+
123+
112124
closeIntervalForCurrentTab() {
113125
if (currentTab !== '') {
114-
var tabUrl = this.getTab(currentTab);
115-
if (tabUrl !== undefined)
116-
tabUrl.closeInterval();
117-
currentTab = '';
126+
var item = timeIntervalList.find(o => o.domain === currentTab);
127+
if (item != undefined)
128+
item.closeInterval();
118129
}
130+
currentTab = '';
119131
}
120132
};

src/scripts/background.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

33
var tabs;
4+
var timeIntervalList;
45
var currentTab;
6+
var isNeedDeleteTimeIntervalFromTabs = false;
57
var activity = new Activity();
68
var storage = new LocalStorage();
79

@@ -120,13 +122,15 @@ function checkPermissions(callback, activeUrl, tab, activeTab) {
120122
function backgroundUpdateStorage() {
121123
if (tabs != undefined && tabs.length > 0)
122124
storage.saveTabs(tabs);
125+
if (timeIntervalList != undefined && timeIntervalList.length > 0)
126+
storage.saveValue(STORAGE_TIMEINTERVAL_LIST, timeIntervalList);
123127
}
124128

125129
function setDefaultSettings() {
126-
storage.saveSettings(SETTINGS_INTERVAL_INACTIVITY, SETTINGS_INTERVAL_INACTIVITY_DEFAULT);
127-
storage.saveSettings(SETTINGS_INTERVAL_RANGE, SETTINGS_INTERVAL_RANGE_DEFAULT);
128-
storage.saveSettings(SETTINGS_VIEW_TIME_IN_BADGE, SETTINGS_VIEW_TIME_IN_BADGE_DEFAULT);
129-
storage.saveSettings(SETTINGS_INTERVAL_SAVE_STORAGE, SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT);
130+
storage.saveValue(SETTINGS_INTERVAL_INACTIVITY, SETTINGS_INTERVAL_INACTIVITY_DEFAULT);
131+
storage.saveValue(SETTINGS_INTERVAL_RANGE, SETTINGS_INTERVAL_RANGE_DEFAULT);
132+
storage.saveValue(SETTINGS_VIEW_TIME_IN_BADGE, SETTINGS_VIEW_TIME_IN_BADGE_DEFAULT);
133+
storage.saveValue(SETTINGS_INTERVAL_SAVE_STORAGE, SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT);
130134
}
131135

132136
function checkSettingsImEmpty() {
@@ -155,6 +159,7 @@ function addListener() {
155159
}
156160
if (details.reason == 'update') {
157161
checkSettingsImEmpty();
162+
isNeedDeleteTimeIntervalFromTabs = true;
158163
}
159164
});
160165
chrome.storage.onChanged.addListener(function (changes, namespace) {
@@ -166,10 +171,10 @@ function addListener() {
166171
loadRestrictionList();
167172
}
168173
if (key === SETTINGS_INTERVAL_INACTIVITY) {
169-
storage.getSettings(SETTINGS_INTERVAL_INACTIVITY, function (item) { setting_interval_inactivity = item; });
174+
storage.getValue(SETTINGS_INTERVAL_INACTIVITY, function (item) { setting_interval_inactivity = item; });
170175
}
171176
if (key === SETTINGS_VIEW_TIME_IN_BADGE) {
172-
storage.getSettings(SETTINGS_VIEW_TIME_IN_BADGE, function (item) { setting_view_in_badge = item; });
177+
storage.getValue(SETTINGS_VIEW_TIME_IN_BADGE, function (item) { setting_view_in_badge = item; });
173178
}
174179
}
175180
});
@@ -180,33 +185,58 @@ function addListener() {
180185
function loadTabs() {
181186
storage.loadTabs(STORAGE_TABS, function (items) {
182187
tabs = [];
183-
for (var i = 0; i < items.length; i++) {
184-
tabs.push(new Tab(items[i].url, items[i].favicon, items[i].days, items[i].summaryTime, items[i].counter));
188+
if (items != undefined) {
189+
for (var i = 0; i < items.length; i++) {
190+
tabs.push(new Tab(items[i].url, items[i].favicon, items[i].days, items[i].summaryTime, items[i].counter));
191+
}
192+
if (isNeedDeleteTimeIntervalFromTabs)
193+
deleteTimeIntervalFromTabs();
185194
}
186195
});
187196
}
188197

198+
function deleteTimeIntervalFromTabs() {
199+
tabs.forEach(function (item) {
200+
item.days.forEach(function (day) {
201+
if (day.time != undefined)
202+
day.time = [];
203+
})
204+
})
205+
}
206+
189207
function loadBlackList() {
190-
storage.getSettings(STORAGE_BLACK_LIST, function (items) {
208+
storage.getValue(STORAGE_BLACK_LIST, function (items) {
191209
setting_black_list = items;
192210
})
193211
}
194212

213+
function loadTimeIntervals() {
214+
storage.getValue(STORAGE_TIMEINTERVAL_LIST, function (items) {
215+
timeIntervalList = [];
216+
if (items != undefined) {
217+
for (var i = 0; i < items.length; i++) {
218+
timeIntervalList.push(new TimeInterval(items[i].day, items[i].domain, items[i].intervals));
219+
}
220+
}
221+
});
222+
}
223+
195224
function loadRestrictionList() {
196-
storage.getSettings(STORAGE_RESTRICTION_LIST, function (items) {
225+
storage.getValue(STORAGE_RESTRICTION_LIST, function (items) {
197226
setting_restriction_list = items;
198227
})
199228
}
200229

201230
function loadSettings() {
202-
storage.getSettings(SETTINGS_INTERVAL_INACTIVITY, function (item) { setting_interval_inactivity = item; });
203-
storage.getSettings(SETTINGS_VIEW_TIME_IN_BADGE, function (item) { setting_view_in_badge = item; });
231+
storage.getValue(SETTINGS_INTERVAL_INACTIVITY, function (item) { setting_interval_inactivity = item; });
232+
storage.getValue(SETTINGS_VIEW_TIME_IN_BADGE, function (item) { setting_view_in_badge = item; });
204233
}
205234

235+
addListener();
206236
loadTabs();
237+
loadTimeIntervals();
207238
loadBlackList();
208239
loadRestrictionList();
209240
loadSettings();
210-
addListener();
211241
updateSummaryTime();
212242
updateStorage();

src/scripts/block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ document.addEventListener('DOMContentLoaded', function () {
66
var blockSiteUrl = url.searchParams.get("url");
77
document.getElementById('site').innerText = blockSiteUrl;
88

9-
storage.getSettings(STORAGE_RESTRICTION_LIST, function (items) {
9+
storage.getValue(STORAGE_RESTRICTION_LIST, function (items) {
1010
restrictionList = items;
1111
if (restrictionList === undefined)
1212
restrictionList = [];

src/scripts/chart/chart-core.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,13 @@ function drawIntervalChart(data) {
419419
})
420420
.attr("height", function (d) {
421421
var offset = getMinutesTo(d.interval) - getMinutesFrom(d.interval);
422-
if (offset == 0)
423-
return 1;
422+
if (offset == 0){
423+
var offsetSeconds = getSecondsTo(d.interval) - getSecondsFrom(d.interval);
424+
if (offsetSeconds <= 3)
425+
return 0;
426+
else
427+
return 1;
428+
}
424429
else return offset * tickDistance;
425430
})
426431
.on("mouseover", mouseover)
@@ -458,4 +463,14 @@ function drawIntervalChart(data) {
458463
var time = interval.split('-')[1];
459464
return time.split(':')[1];
460465
}
466+
467+
function getSecondsFrom(interval) {
468+
var time = interval.split('-')[0];
469+
return time.split(':')[2];
470+
}
471+
472+
function getSecondsTo(interval) {
473+
var time = interval.split('-')[1];
474+
return time.split(':')[2];
475+
}
461476
}

src/scripts/common.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ var TypeListEnum = {
3030
var STORAGE_TABS = 'tabs';
3131
var STORAGE_BLACK_LIST = 'black_list';
3232
var STORAGE_RESTRICTION_LIST = 'restriction_list';
33+
var STORAGE_TIMEINTERVAL_LIST = 'time_interval';
3334

3435
var SETTINGS_INTERVAL_INACTIVITY_DEFAULT = InactivityInterval.second30;
3536
var SETTINGS_INTERVAL_CHECK_DEFAULT = 1000;
36-
var SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT = 3000;
37+
var SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT = 2000;
3738
var SETTINGS_INTERVAL_CHECK_STORAGE_DEFAULT = 3000;
3839
var SETTINGS_INTERVAL_RANGE_DEFAULT = RangeForDays.days7;
3940
var SETTINGS_VIEW_TIME_IN_BADGE_DEFAULT = true;

src/scripts/settings.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ document.addEventListener('DOMContentLoaded', function () {
2828
addNewRestrictionSiteClickHandler();
2929
});
3030
document.getElementById('viewTimeInBadge').addEventListener('change', function () {
31-
storage.saveSettings(SETTINGS_VIEW_TIME_IN_BADGE, this.checked);
31+
storage.saveValue(SETTINGS_VIEW_TIME_IN_BADGE, this.checked);
3232
});
3333
document.getElementById('intervalInactivity').addEventListener('change', function () {
34-
storage.saveSettings(SETTINGS_INTERVAL_INACTIVITY, this.value);
34+
storage.saveValue(SETTINGS_INTERVAL_INACTIVITY, this.value);
3535
});
3636
document.getElementById('rangeToDays').addEventListener('change', function () {
37-
storage.saveSettings(SETTINGS_INTERVAL_RANGE, this.value);
37+
storage.saveValue(SETTINGS_INTERVAL_RANGE, this.value);
3838
});
3939
document.getElementById('grantPermission').addEventListener('click', function () {
4040
grantPermission();
@@ -60,25 +60,25 @@ function setBlockEvent(btnName, blockName) {
6060
}
6161

6262
function loadSettings() {
63-
storage.getSettings(SETTINGS_INTERVAL_INACTIVITY, function (item) {
63+
storage.getValue(SETTINGS_INTERVAL_INACTIVITY, function (item) {
6464
document.getElementById('intervalInactivity').value = item;
6565
});
66-
storage.getSettings(SETTINGS_INTERVAL_RANGE, function (item) {
66+
storage.getValue(SETTINGS_INTERVAL_RANGE, function (item) {
6767
document.getElementById('rangeToDays').value = item;
6868
});
69-
storage.getSettings(SETTINGS_VIEW_TIME_IN_BADGE, function (item) {
69+
storage.getValue(SETTINGS_VIEW_TIME_IN_BADGE, function (item) {
7070
document.getElementById('viewTimeInBadge').checked = item;
7171
});
7272
storage.getMemoryUse(STORAGE_TABS, function (integer) {
7373
document.getElementById('memoryUse').innerHTML = (integer / 1024).toFixed(2) + 'Kb';
7474
});
75-
storage.getSettings(STORAGE_BLACK_LIST, function (items) {
75+
storage.getValue(STORAGE_BLACK_LIST, function (items) {
7676
if (items !== undefined)
7777
blackList = items;
7878
else blackList = [];
7979
viewBlackList(items);
8080
});
81-
storage.getSettings(STORAGE_RESTRICTION_LIST, function (items) {
81+
storage.getValue(STORAGE_RESTRICTION_LIST, function (items) {
8282
restrictionList = items;
8383
if (restrictionList === undefined)
8484
restrictionList = [];
@@ -137,7 +137,7 @@ function viewRestrictionList(items) {
137137
}
138138

139139
function exportToCSV() {
140-
storage.getSettings(STORAGE_TABS, function (item) {
140+
storage.getValue(STORAGE_TABS, function (item) {
141141
toCsv(item);
142142
});
143143
}
@@ -326,9 +326,9 @@ function updateItemFromResctrictoinList(domain, time) {
326326
}
327327

328328
function updateBlackList() {
329-
storage.saveSettings(STORAGE_BLACK_LIST, blackList);
329+
storage.saveValue(STORAGE_BLACK_LIST, blackList);
330330
}
331331

332332
function updateRestrictionList() {
333-
storage.saveSettings(STORAGE_RESTRICTION_LIST, restrictionList);
333+
storage.saveValue(STORAGE_RESTRICTION_LIST, restrictionList);
334334
}

src/scripts/storage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class LocalStorage {
2121
callback();
2222
}
2323

24-
saveSettings(name, value) {
24+
saveValue(name, value) {
2525
chrome.storage.local.set({ [name]: value });
2626
}
2727

28-
getSettings(name, callback) {
28+
getValue(name, callback) {
2929
chrome.storage.local.get(name, function (item) {
3030
if (item !== undefined) {
3131
callback(item[name]);

src/scripts/tab.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,16 @@ class Tab {
4141
}
4242
else {
4343
day['counter'] += 1;
44-
this.addInterval(day);
4544
}
4645
}
4746

4847
addNewDay(today) {
49-
var date = new Date();
50-
var stringDate = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
5148
this.days.push(
5249
{
5350
'date': today,
5451
'summary': 1,
55-
'counter': 1,
56-
'time': ["" + stringDate + '-'+ stringDate + ""]
52+
'counter': 1
5753
}
5854
);
5955
}
60-
61-
addInterval(day){
62-
var date = new Date();
63-
var stringDate = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
64-
if (day.time == undefined)
65-
day.time = [stringDate + '-'+ stringDate];
66-
else {
67-
day.time.push(stringDate + '-' + stringDate);
68-
}
69-
}
70-
71-
closeInterval(){
72-
var date = new Date();
73-
var stringDate = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
74-
var today = new Date().toLocaleDateString("en-US");
75-
var day = this.days.find(x => x.date == today);
76-
if (day != undefined && day.time !== undefined){
77-
var interval = day.time.pop();
78-
day.time.push(interval.split('-')[0] + '-' + stringDate);
79-
}
80-
}
8156
};

0 commit comments

Comments
 (0)