-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsites.js
144 lines (133 loc) · 3.81 KB
/
sites.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Stores the time that is spent on each site.
*
* The primary interface to this class is through setCurrentFocus.
*/
var globt=0;
var prev_url=null;
var prev_domain=null;
var start_time;
var db = firebase.firestore();
function Sites(config) {
this._config = config;
if (!localStorage.sites) {
localStorage.sites = JSON.stringify({});
}
this._currentSite = null;
this._siteRegexp = /^(\w+:\/\/[^\/]+).*$/;
this._startTime = null;
}
/**
* Returns the a dictionary of site -> seconds.
*/
Object.defineProperty(Sites.prototype, "sites", {
get: function() {
var s = JSON.parse(localStorage.sites);
var sites = {}
for (var site in s) {
if (s.hasOwnProperty(site) && !this._config.isIgnoredSite(site)) {
sites[site] = s[site];
}
}
return sites;
}
});
/**
* Returns just the site/domain from the url. Includes the protocol.
* chrome://extensions/some/other?blah=ffdf -> chrome://extensions
* @param {string} url The URL of the page, including the protocol.
* @return {string} The site, including protocol, but not paths.
*/
Sites.prototype.getSiteFromUrl = function(url) {
var match = url.match(this._siteRegexp);
if (match) {
return match[1];
}
return null;
};
Sites.prototype._updateTime = function() {
if (!this._currentSite || !this._startTime) {
return;
}
var delta = new Date() - this._startTime;
// console.log("Site: " + this._currentSite + " Delta = " + delta/1000);
if (delta/1000/60 > 2*this._config.updateTimePeriodMinutes) {
console.log("Delta of " + delta/1000 + " seconds too long; ignored.");
return;
}
var sites = this.sites;
if (!sites[this._currentSite]) {
sites[this._currentSite] = 0;
}
sites[this._currentSite] += delta/1000;
globt=delta/1000;
localStorage.sites = JSON.stringify(sites);
};
/**
* This method should be called whenever there is a potential focus change.
* Provide url=null if Chrome is out of focus.
*/
Sites.prototype.setCurrentFocus = function(url) {
//sum all time
// console.log("setCurrentFocus: " + url);
this._updateTime();
if (url == null) {
this._currentSite = null;
this._startTime = null;
chrome.browserAction.setIcon(
{path: {19: 'images/icon_paused19.png',
38: 'images/icon_paused38.png'}});
} else {
if(globt>1 && prev_url!=="" && prev_domain!==null){
var sy = start_time.split("-")[0];
var sm = start_time.split("-")[1];
var sd = start_time.slice(8,10);
var sh = start_time.slice(11,13);
var smin = start_time.split("-")[3];
var ss = start_time.split("-")[4];
siteInfo={
url:prev_url,
site:prev_domain,
time:globt,
start_time: new Date(sy,sm,sd,sh,smin,ss,"00"),
uploading_time: new Date()
}
console.log(siteInfo);
uploadInfo(siteInfo);
}
prev_url=url;
prev_domain=this.getSiteFromUrl(prev_url);
this._currentSite = this.getSiteFromUrl(url);
this._startTime = new Date();
start_time= getTimestamp();
chrome.browserAction.setIcon(
{path: {19: 'images/icon19.png',
38: 'images/icon38.png'}});
}
};
/**
* Clear all statistics.
*/
Sites.prototype.clear = function() {
localStorage.sites = JSON.stringify({});
this._config.lastClearTime = new Date().getTime();
};
uploadInfo = (siteInfo) =>{
db.collection("tracking_test").add(siteInfo)
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
};
function getTimestamp() {
var e = new Date,
t = e.getFullYear(),
o = ("00" + (e.getMonth() + 1)).slice(-2),
n = ("00" + e.getDate()).slice(-2),
r = ("00" + e.getHours()).slice(-2),
s = ("00" + e.getMinutes()).slice(-2),
i = t + "-" + o + "-" + n + "_" + r + "-" + s;
return i;
}