-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathclient.js
More file actions
137 lines (117 loc) · 3.79 KB
/
client.js
File metadata and controls
137 lines (117 loc) · 3.79 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
"use strict";
var AWClient = require("../aw-client-js/out/aw-client.js").AWClient;
var ua_parser = require("ua-parser-js");
var retry = require("p-retry") // See aw-watcher-web issue #41
function emitNotification(title, message) {
chrome.notifications.create({
"type": "basic",
"iconUrl": chrome.extension.getURL("media/logo/logo-128.png"),
"title": title,
"message": message,
});
}
function logHttpError(error) {
// No response property for network errors
if (error.response) {
console.error("Status code: " + err.response.status + ", response: " + err.response.data.message);
} else {
console.error("Unexpected error: " + error);
}
}
function getBrowserName() {
if (navigator.brave?.isBrave())
return "brave";
// Get browser name from UAParser (somewhat expensive operation)
var agent_parsed = ua_parser(navigator.userAgent);
var browserName = agent_parsed.browser.name.toLowerCase();
return browserName;
}
var client = {
testing: null,
awc: null,
lastSyncSuccess: true,
browserName: null,
setup: function() {
console.log("Setting up client");
client.browserName = getBrowserName();
// Check if in dev mode
chrome.management.getSelf(function(info) {
client.testing = info.installType === "development";
console.log("testing: " + client.testing);
client.awc = new AWClient("aw-client-web", {testing: client.testing});
client.createBucket();
// Needed in order to show testing information in popup
chrome.storage.local.set({"testing": client.testing, "baseURL": client.awc.baseURL});
});
},
getBucketId: function () {
return "aw-watcher-web-" + client.browserName.toLowerCase();
},
updateSyncStatus: function(){
chrome.storage.local.set({
"lastSyncSuccess": client.lastSyncSuccess,
"lastSync": new Date().toISOString()
});
},
createBucket: function(){
if (this.testing === null)
return;
// TODO: We might want to get the hostname somehow, maybe like this:
// https://stackoverflow.com/questions/28223087/how-can-i-allow-firefox-or-chrome-to-read-a-pcs-hostname-or-other-assignable
var bucket_id = this.getBucketId();
var eventtype = "web.tab.current";
var hostname = "unknown";
function attempt() {
return client.awc.ensureBucket(bucket_id, eventtype, hostname)
.catch( (err) => {
console.error("Failed to create bucket, retrying...");
logHttpError(err);
return Promise.reject(err);
}
);
}
retry(attempt, { forever: true });
},
sendHeartbeat: function(timestamp, data, pulsetime) {
if (this.testing === null)
return;
// First get tags if they exist
chrome.storage.local.get(['tags'], (result) => {
var payload = {
"data": data,
"duration": 0.0,
"timestamp": timestamp.toISOString(),
};
// Only add tags if they exist
if (result.tags) {
payload.data.tags = result.tags;
}
var attempt = () => {
return client.awc.heartbeat(client.getBucketId(), pulsetime, payload);
};
retry(attempt, { retries: 3 }).then(
(res) => {
if (!client.lastSyncSuccess) {
emitNotification(
"Now connected again",
"Connection to ActivityWatch server established again"
);
}
client.lastSyncSuccess = true;
client.updateSyncStatus();
}, (err) => {
if(client.lastSyncSuccess) {
emitNotification(
"Unable to send event to server",
"Please ensure that ActivityWatch is running"
);
}
client.lastSyncSuccess = false;
client.updateSyncStatus();
logHttpError(err);
}
);
});
}
};
module.exports = client;