Skip to content

Commit 5059a65

Browse files
committed
refact project
1 parent 35c64c3 commit 5059a65

File tree

9 files changed

+128
-100
lines changed

9 files changed

+128
-100
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title>WebActivity Time Tracker</title>
66
<link href="style/webact.css" rel="stylesheet"/>
77
<script src="scripts/webact.js"></script>
8+
<script src="scripts/common.js"></script>
89
</head>
910
<body>
1011
<div id="resultTable">

manifest.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
"storage"
2222
],
2323
"background": {
24-
"scripts": ["scripts/webTimer.js", "scripts/tab.js", "scripts/background.js"],
24+
"scripts": ["scripts/common.js",
25+
"scripts/storage.js",
26+
"scripts/activity.js",
27+
"scripts/tab.js",
28+
"scripts/background.js"],
2529
"persistent": false
2630
},
2731
"browser_action": {

scripts/activity.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
class Activity {
4+
addTab(tab) {
5+
if (this.isValidPage(tab) === true) {
6+
if (tab.id && (tab.id != 0)) {
7+
tabs = tabs || [];
8+
var domain = this.extractHostname(tab.url);
9+
if (this.isNewUrl(domain)) {
10+
var newTab = new Tab(domain, tab.favIconUrl);
11+
tabs.push(newTab);
12+
}
13+
}
14+
}
15+
}
16+
17+
isValidPage(tab) {
18+
if (!tab || !tab.url || (tab.url.indexOf('http:') == -1 && tab.url.indexOf('https:') == -1)
19+
|| tab.url.indexOf('chrome://') !== -1
20+
|| tab.url.indexOf('chrome-extension://') !== -1)
21+
return false;
22+
return true;
23+
}
24+
25+
isNewUrl(domain) {
26+
if (tabs.length > 0)
27+
return tabs.find(o => o.url === domain) === undefined;
28+
else return true;
29+
}
30+
31+
getTab(domain) {
32+
return tabs.find(o => o.url === domain);
33+
}
34+
35+
extractHostname(url) {
36+
var hostname;
37+
38+
if (url.indexOf("//") > -1) {
39+
hostname = url.split('/')[2];
40+
}
41+
else {
42+
hostname = url.split('/')[0];
43+
}
44+
45+
hostname = hostname.split(':')[0];
46+
hostname = hostname.split('?')[0];
47+
48+
return hostname;
49+
}
50+
51+
loadDataFromStorage(){
52+
var tabs = storage.load(STORAGE_TABS);
53+
}
54+
};

scripts/background.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
var timer = new WebTimer();
1+
'use strict';
22

3-
window.onload = function () {
4-
chrome.tabs.onActivated.addListener(function (info) {
5-
chrome.tabs.get(info.tabId, function (tab) {
6-
timer.addTab(tab);
7-
});
8-
});
9-
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
10-
timer.addTab(tab);
3+
var tabs = [];
4+
var activity = new Activity();
5+
var storage = new LocalStorage();
6+
7+
chrome.tabs.onActivated.addListener(function (info) {
8+
chrome.tabs.get(info.tabId, function (tab) {
9+
activity.addTab(tab);
1110
});
12-
};
11+
});
12+
13+
chrome.windows.getLastFocused({populate: true}, function (window){
14+
var s = window;
15+
});

scripts/common.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var STORAGE_TABS = 'tabs';
2+
3+
function isEmpty(obj) {
4+
for (var prop in obj) {
5+
if (obj.hasOwnProperty(prop))
6+
return false;
7+
}
8+
9+
return JSON.stringify(obj) === JSON.stringify({});
10+
}
11+
12+
function convertSummaryTimeToString(summaryTime) {
13+
var sec = (summaryTime / 1000).toFixed(1);
14+
var min = (summaryTime / (1000 * 60)).toFixed(1);
15+
var hours = (summaryTime / (1000 * 60 * 60)).toFixed(1);
16+
var days = (summaryTime / (1000 * 60 * 60 * 24)).toFixed(1);
17+
18+
if (sec < 60) {
19+
return sec + " sec";
20+
} else if (min < 60) {
21+
return min + " min";
22+
} else if (hours < 24) {
23+
return hours + " hours";
24+
} else {
25+
return days + " days"
26+
}
27+
}

scripts/storage.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
class LocalStorage {
4+
load(name){
5+
chrome.storage.local.get(name, function (item){
6+
return item[name];
7+
});
8+
}
9+
10+
save(name, value){
11+
chrome.storage.local.set(name);
12+
}
13+
};

scripts/tab.js

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
var Tab = function (domain, favicon) {
2-
this.url = domain;
3-
this.startTime = new Date();
4-
this.favicon = favicon;
5-
};
1+
'use strict';
62

7-
Tab.prototype = {
8-
url: {},
9-
startTime: {},
10-
summaryTime: {},
11-
favicon: {},
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;
3+
class Tab {
4+
constructor(url, favicon){
5+
this.url = url;
6+
this.favicon = favicon;
7+
this.summaryTime = '';
218
}
229
};

scripts/webTimer.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

scripts/webact.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
window.onload = function () {
22
var resultTabs = {};
33
//var tabs = chrome.extension.getBackgroundPage().timer.tabs;
4-
chrome.storage.sync.get('tabs', function(val){
4+
setInterval(getDataFromStorage, 3000);
5+
};
6+
7+
function getDataFromStorage(){
8+
chrome.storage.local.get('tabs', function(val){
59
resultTabs = JSON.parse(val.tabs);
610
getTabsFromStorage(resultTabs);
711
});
8-
};
12+
}
913

1014
function getTabsFromStorage(tabs){
15+
var table = document.getElementById('resultTable');
16+
table.innerHTML = null;
1117
for (var i = 0; i < tabs.length; i++) {
12-
var table = document.getElementById('resultTable');
1318
var div = document.createElement('div');
1419
div.classList.add('inline-flex');
1520

@@ -18,7 +23,7 @@ function getTabsFromStorage(tabs){
1823
img.setAttribute('src', tabs[i].favicon);
1924

2025
var span = document.createElement('span');
21-
span.innerText = tabs[i].url + ' ' + tabs[i].summaryTime;
26+
span.innerText = tabs[i].url + ' ' + convertSummaryTimeToString(tabs[i].summaryTime);
2227
span.classList.add('margin-left-5');
2328

2429
div.appendChild(img);

0 commit comments

Comments
 (0)