-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathbackground.js
More file actions
138 lines (121 loc) · 3.55 KB
/
background.js
File metadata and controls
138 lines (121 loc) · 3.55 KB
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
let title = null;
let url = null;
let favicon = null;
class ActiveBrowserWatcher {
/**
* @param {number} interval Polling interval
* @param {(activity) => void} changeCallback
*/
constructor(interval = 1000, changeCallback) {
this.startTime = null;
this.title = null; //Title
this.url = null;
this.favicon = null;
this.changeCallback = changeCallback;
this.interval = interval;
}
/**
* Storing the start time of the active window
* Collecting data of the window which will be active
*/
storeTime() {
const endTime = Date.now();
const startTime = this.startTime;
const title = this.title;
const url = this.url;
const favicon = this.favicon;
const data = {
title,
url,
favicon,
startTime,
endTime,
};
fetch('http://localhost:32768/api/browseractivities', {
method: 'POST',
body: data,
})
.then(console.log('done store'))
.catch(err => {
console.log(err);
});
this.changeCallback(data);
console.log(data);
}
/**
* Checks the active window is specific time interval
* and whenever the active window changes stores the time difference by calling {@link ActiveWindowWatcher.storeTime} function
*/
tracker() {
setInterval(() => {
let queryOptions = { active: true, currentWindow: true }; // to get current active tab from the current window
// eslint-disable-next-line no-undef
chrome.tabs.query(queryOptions, function currentTab(tabs) {
let currentTab = tabs[0]; // take the object from the returned promise
let currentTitle = currentTab.title; // take object title
let currentUrl = currentTab.url; // take object URL
let currentFavIcons = currentTab.favIconUrl;
title = currentTitle;
url = currentUrl;
favicon = currentFavIcons;
// Title
const activityTitle = document.getElementById('activityTitle');
const activityTitleUrl = document.getElementById('activityTitle');
activityTitle.innerHTML = 'Title: ' + currentTitle; //format it in html
activityTitleUrl.setAttribute('href', currentUrl);
console.log(activityTitle);
// URl
const activityUrl = document.getElementById('activityUrl');
const activityLink = document.getElementById('activityUrl');
activityUrl.innerHTML = 'URL: ' + currentUrl; //format it in html
activityLink.setAttribute('href', currentUrl);
// Favicon
const activityFavicon = document.getElementById('activityFavicon');
activityFavicon.setAttribute('src', currentFavIcons); //format Favicon in html
});
if (title === undefined) {
this.title = null;
this.url = null;
this.favicon = null;
return;
}
if (!this.title) {
this.startTime = Date.now();
this.title = title;
this.url = url;
this.favicon = favicon;
}
//If the active window is changed store the used time data.
if (title !== this.title) {
this.storeTime();
this.title = null;
this.url = null;
this.favicon = null;
}
console.log(title, url, favicon, this.startTime);
}, this.interval);
}
initialize() {
this.tracker();
}
}
// const activityTracker = new ActiveBrowserWatcher(1000);
// const activityTracker = new ActiveBrowserWatcher(1000, activity => {
// saveActivities(activity);
// });
// fetch('http://localhost:32768/api/browseractivities',{
// method: 'POST',
// body: activityTracker
// )
// });
const activityTracker = new ActiveBrowserWatcher(1000, activity => {
fetch('http://localhost:32768/api/browseractivities', {
method: 'POST',
body: activity,
})
.then(console.log('done'))
.catch(err => {
console.log(err);
});
});
activityTracker.initialize();