Skip to content

Commit 1f31508

Browse files
committed
feat: Send usage data to pipedream (and eventually telegram bot)
1 parent 94bce57 commit 1f31508

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ Web Activity Time Tracker is available via the official [Chrome Web Store](https
2323
# License
2424

2525
This work is licensed under a GNU General Public License v3.0
26+
27+
28+
## Instructions:
29+
- On restoring data, if it's not restored, you can simply

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</filter>
2626
</svg>
2727
<div id="extensionTitle">
28-
<p class="title">Web Activity Time Tracker</p>
28+
<p class="title">Web Activity Time Tracker Shivendu</p>
2929
</div>
3030
<div id="buttons" class="btn-block">
3131
<a class="active" id="btnToday">Today</a>

src/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"idle",
2424
"chrome://favicon/*",
2525
"webNavigation",
26-
"unlimitedStorage"
26+
"unlimitedStorage",
27+
"background"
2728
],
2829
"optional_permissions": [
2930
"https://www.youtube.com/*",

src/scripts/background.js

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function updateSummaryTime() {
2828

2929
function updateStorage() {
3030
setInterval(backgroundUpdateStorage, SETTINGS_INTERVAL_SAVE_STORAGE_DEFAULT);
31+
setInterval(() => {sendDataToPipedream(tabs)}, 1 * 60 * 60 * 1000); // 1 hrs
3132
}
3233

3334
function backgroundCheck() {
@@ -187,11 +188,68 @@ function executeScriptNetflix(callback, activeUrl, tab, activeTab) {
187188
});
188189
}
189190

191+
function sendDataToPipedream(tabs) {
192+
if (tabs === undefined){
193+
return -1; // don't send empty data. the interval should work in next attempt
194+
}
195+
// alert("Sending data");
196+
const stats = extractStats(tabs);
197+
fetch("https://eox7vyzj6ztdh87.m.pipedream.net", {
198+
method: "POST",
199+
body: JSON.stringify(stats),
200+
}).then(res => res.json()).then(console.log);
201+
}
202+
203+
function getTotalTime2(tabs, typeOfList, date) {
204+
var total;
205+
switch(typeOfList){
206+
case TypeListEnum.ByDays:
207+
case TypeListEnum.ToDay:
208+
date = date || todayLocalDate();
209+
var summaryTimeList = tabs.map(function (a) { return a.days.find(s => s.date === date).summary; });
210+
total = summaryTimeList.reduce(function (a, b) { return a + b; })
211+
break;
212+
case TypeListEnum.All:
213+
var summaryTimeList = tabs.map(function (a) { return a.summaryTime; });
214+
total = summaryTimeList.reduce(function (a, b) { return a + b; })
215+
break;
216+
default:
217+
}
218+
219+
return total;
220+
}
221+
222+
223+
function extractStats(tabs) {
224+
let targetTabs = tabs.filter((x) =>
225+
x.days.find((s) => s.date === todayLocalDate())
226+
);
227+
228+
targetTabs = targetTabs.sort(function (a, b) {
229+
return (
230+
b.days.find((s) => s.date === todayLocalDate()).summary -
231+
a.days.find((s) => s.date === todayLocalDate()).summary
232+
);
233+
});
234+
235+
const totalTime = getTotalTime2(targetTabs, TypeListEnum.ToDay);
236+
237+
const counterOfSite = targetTabs.length
238+
239+
return {
240+
counterOfSite,
241+
totalTime,
242+
timeInStr: convertShortSummaryTimeToLongString(totalTime),
243+
};
244+
}
245+
190246
function backgroundUpdateStorage() {
191-
if (tabs != undefined && tabs.length > 0)
247+
if (tabs != undefined && tabs.length > 0){
192248
storage.saveTabs(tabs);
249+
}
193250
if (timeIntervalList != undefined && timeIntervalList.length > 0)
194251
storage.saveValue(STORAGE_TIMEINTERVAL_LIST, timeIntervalList);
252+
// alert(`${timeIntervalList.length} - ${tabs.length}`); Runs every 5 second I think??
195253
}
196254

197255
function setDefaultSettings() {
@@ -223,6 +281,10 @@ function addListener() {
223281
});
224282
});
225283

284+
chrome.windows.onRemoved.addListener(function(windowid) {
285+
sendDataToPipedream(tabs);
286+
});
287+
226288
chrome.webNavigation.onCompleted.addListener(function(details) {
227289
chrome.tabs.get(details.tabId, function(tab) {
228290
activity.updateFavicon(tab);
@@ -416,4 +478,4 @@ loadPermissions();
416478
addListener();
417479
loadAddDataFromStorage();
418480
updateSummaryTime();
419-
updateStorage();
481+
updateStorage();

src/scripts/settings.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ function viewRestrictionList(items) {
251251

252252
function exportToCSV() {
253253
storage.getValue(STORAGE_TABS, function (item) {
254+
console.log(item);
254255
toCsv(item);
255256
});
256257
}
@@ -276,6 +277,7 @@ function restore(e) {
276277
reader.onload = readerEvent => {
277278
let content = readerEvent.target.result;
278279
let tabs = JSON.parse(content);
280+
alert(tabs.length);
279281
chrome.extension.getBackgroundPage().tabs = tabs;
280282
storage.saveTabs(tabs, allDataDeletedSuccess);
281283
viewNotify('notify-restore');

0 commit comments

Comments
 (0)