-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
executable file
·190 lines (167 loc) · 5.35 KB
/
background.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// it starts here.
function init() {
initDataStores().then(function() {
// return addListeners();
return setupTimer();
}).then(function() {
// if additional things to do
});
}
var db, timer;
var timer_interval = 60000;
// init data stores
function initDataStores() {
const dbPromise = idb.open('mobbedGlob', 1, upgradeDB => {
console.log('running onupgradeneeded');
if (!upgradeDB.objectStoreNames.contains('usageStore')) {
usageStore = upgradeDB.createObjectStore('usageStore',
{keyPath: 'pk', autoIncrement: true}
);
usageStore.createIndex('tabId', 'tabId', {unique: false});
usageStore.createIndex('windowId', 'windowId', {unique: false});
usageStore.createIndex('date', 'date', {unique: false});
}
// add more data stores as needed
});
dbPromise.then(function(resolved_db) {
db = resolved_db;
console.log('running onsuccess');
}).catch(function(e) {
console.error('onerror!');
console.dir(e);
});
return Promise.all([dbPromise]);
}
count = 0;
function addUsage(action, tabId, params) {
count += 1;
var transaction = db.transaction(['usageStore'], 'readwrite');
var usageStore = transaction.objectStore('usageStore');
var item = {
action: action,
tabId: tabId,
windowId: count
};
for (let p in params) {
item[p] = params[p];
}
var request = usageStore.add(item).then(function(e) {
// console.log('Woot! Did it');
}).catch(function(e) {
// console.error('Error', e.name, e.message);
});
}
// add listeners
function addListeners() {
chrome.tabs.onCreated.addListener(function(tab) {
// find tuple with same id which is active
addUsage("created", tab.id, {url: tab.url, sessionId: tab.sessionId});
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status !== 'complete') return;
addUsage("updated", tab.id, {url: tab.url, sessionId: tab.sessionId});
});
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
addUsage("removed", tabId);
})
return Promise.resolve();
}
function getStore(mode) {
mode = (mode == 'rw') ? 'readwrite' : 'readonly';
var transaction = db.transaction(['usageStore'], mode);
var usageStore = transaction.objectStore('usageStore');
return usageStore;
}
function getSavedTab(tabId, windowId) {
return new Promise(function(resolve, reject) {
var usageIndex = getStore().index('tabId');
usageIndex.getAll(IDBKeyRange.only(tabId)).then(function(tabs) {
for (let tab of tabs) {
if (tab.windowId == windowId) {
resolve(tab);
return;
}
}
reject();
}, function() {
reject();
});
});
}
function addTabUsage(tab) {
tab.startTime = Date.now() - timer_interval;
tab.endTime = Date.now();
tab.duration = tab.endTime - tab.startTime;
tab.date = formatDate();
var request = getStore('rw').add(tab).then(function(e) {
// console.log('Woot! Did it');
}).catch(function(e) {
// console.log('Error', e.name, e.message);
});
// console.log("Added ", tab.url, " with duration ", tab.duration);
}
function updateTabUsage(tab, new_tab) {
if (!tab.startTime) {
tab.startTime = Date.now() - timer_interval;
}
tab.endTime = Date.now();
tab.duration = tab.endTime - tab.startTime;
tab.url = new_tab.url || tab.url;
var request = getStore('rw').put(tab).then(function(e) {
// console.log('Woot! Did it');
}).catch(function(e) {
// console.log('Error', e.name, e.message);
});
// console.log("Updated ", tab.url, " with duration ", tab.duration);
}
function formatDate(date) {
var d, month, day, year;
if (!date) {
d = new Date()
} else {
d = new Date(date);
}
month = '' + (d.getMonth() + 1);
day = '' + d.getDate();
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
function getDomain(url) {
var getLocation = function(href) {
var l = document.createElement("a");
l.href = href;
return l;
};
var l = getLocation(url);
// l.hostname + l.pathname
return l.hostname;
}
// reviewTabs captures all tabs then updates the data stores
function reviewTabs() {
chrome.tabs.query({}, function(tabs) {
tabs.forEach(function(tab) {
tab.tabId = tab.id;
tab.domain = getDomain(tab.url);
getSavedTab(tab.tabId, tab.windowId).then(function(saved_tab) {
// tab found.
// now if same domain - update the time period, else create new entry
if (tab.domain == saved_tab.domain) {
updateTabUsage(saved_tab, tab);
} else {
addTabUsage(tab);
}
}, function() {
// tab not found - create new entry
addTabUsage(tab);
})
});
});
}
// timer runs every minute.
function setupTimer() {
timer = setInterval(reviewTabs, timer_interval)
}
// fire up the grill
init();