diff --git a/.gitmodules b/.gitmodules index d4b7a1b..43231c9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "media"] path = media url = https://github.com/ActivityWatch/media.git +[submodule "aw-client-js"] + path = aw-client-js + url = git@github.com:ActivityWatch/aw-client-js.git diff --git a/Makefile b/Makefile index bfb7648..61a5ae3 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,13 @@ -.PHONY: build +.PHONY: build install + +install: + npm install + (cd aw-client-js; npm install; npm run compile) + +update: + npm run build # This is what Google and Mozilla wants us to upload when we release a new version to the Addon "store" -build: - zip -r -FS ./aw-watcher-web.zip manifest.json app/ media/ +build: install + npm run build + zip -r -FS ./aw-watcher-web.zip manifest.json static/ out/ media/ diff --git a/app/client.js b/app/client.js deleted file mode 100644 index cbd60a7..0000000 --- a/app/client.js +++ /dev/null @@ -1,113 +0,0 @@ -"use strict"; - -var client = { - testing: null, - lastSyncSuccess: true, - _getHost: function(){ - if(this.testing) { - return "http://127.0.0.1:5666/"; - } else { - return "http://127.0.0.1:5600/"; - } - }, - - setup: function() { - console.log("Setting up client"); - // Check if in dev mode - chrome.management.getSelf(function(info) { - console.log(JSON.stringify(info)); - client.testing = info.installType === "development"; - - // Needed in order to show testing information in popup - chrome.storage.local.set({"testing": client.testing}); - - client.createBucket(); - }); - }, - - getBucketId: function() { - // TODO: This works for Chrome and Firefox, but is a bit hacky and wont work in the general case - var browserName = /(Chrome|Firefox)\/([0-9.]+)/.exec(navigator.userAgent)[1]; - return "aw-watcher-web-" + browserName.toLowerCase(); - }, - - 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 payload = { - "client": "aw-watcher-web", - "hostname": "unknown", - "type": "web.tab.current" - }; - - var bucket_id = client.getBucketId(); - - var host = this._getHost(); - var url = host + "api/0/buckets/" + bucket_id; - var xhr = new XMLHttpRequest(); - xhr.open("POST", url, true); - xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); - xhr.onreadystatechange = function() { - if (xhr.readyState === XMLHttpRequest.DONE) { - if (xhr.status === 200){ - let resp = JSON.parse(xhr.responseText); - console.log("Bucket was successfully created"); - } - else if (xhr.status === 400){ - let resp = JSON.parse(xhr.responseText); - //console.log("Bucket already created"); - } - else { - console.error("Unable to create bucket (statuscode: " + xhr.status + ")"); - } - } - }; - xhr.send(JSON.stringify(payload)); - }, - - sendHeartbeat: function(timestamp, data, pulsetime) { - if (this.testing === null) - return; - var host = this._getHost(); - var url = host + "api/0/buckets/" + client.getBucketId() + "/heartbeat?pulsetime=" + pulsetime; - var xhr = new XMLHttpRequest(); - xhr.open("POST", url, true); - xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); - - xhr.onreadystatechange = function() { - // xhr.readyState === 4 means DONE with request - if (xhr.readyState === 4) { - if (xhr.status !== 200){ - // ERROR - console.error("Status code: " + xhr.status + ", response: " + xhr.responseText); - if(client.lastSyncSuccess) { - chrome.notifications.create({ - "type": "basic", - "iconUrl": chrome.extension.getURL("media/logo/logo.png"), - "title": "Unable to send event to server", - "message": "Please ensure that you are running an ActivityWatch server at: " + client._getHost(), - }); - client.lastSyncSuccess = false; - } - } else { - // SUCCESS - if(!client.lastSyncSuccess){ - chrome.notifications.create({ - "type": "basic", - "iconUrl": chrome.extension.getURL("media/logo/logo.png"), - "title": "Now connected again", - "message": "Now ActivityWatch server works at: " + client._getHost(), - }); - } - client.lastSyncSuccess = true; - chrome.storage.local.set({"lastSync": new Date().toISOString()}); - } - chrome.storage.local.set({"lastSyncSuccess": client.lastSyncSuccess}); - } - }; - var payload = JSON.stringify({"data": data, "timestamp": timestamp.toISOString()}); - xhr.send(payload); - } -}; diff --git a/aw-client-js b/aw-client-js new file mode 160000 index 0000000..bb6b5a6 --- /dev/null +++ b/aw-client-js @@ -0,0 +1 @@ +Subproject commit bb6b5a66882dd942b375faf947d026a4374f3ee8 diff --git a/manifest.json b/manifest.json index 2681133..8144e71 100644 --- a/manifest.json +++ b/manifest.json @@ -11,7 +11,7 @@ "browser_action": { "default_icon": "media/logo/logo.png", - "default_popup": "app/popup.html" + "default_popup": "static/popup.html" }, "applications": { @@ -22,8 +22,7 @@ "background": { "scripts": [ - "app/client.js", - "app/eventPage.js" + "out/app.js" ], "persistent": false }, diff --git a/package.json b/package.json new file mode 100644 index 0000000..6e5f982 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "browserify": "^16.2.2" + }, + "scripts": { + "build": "browserify src/*.js > out/app.js" + } +} diff --git a/src/client.js b/src/client.js new file mode 100644 index 0000000..26bd768 --- /dev/null +++ b/src/client.js @@ -0,0 +1,74 @@ +"use strict"; + +var AWClient = require("../aw-client-js/out/aw-client.js").AWClient; + + +var client = { + testing: null, + awc: null, + lastSyncSuccess: true, + + setup: function() { + console.log("Setting up client"); + // Check if in dev mode + chrome.management.getSelf(function(info) { + console.log(info); + client.testing = info.installType === "development"; + console.log("testing: " + client.testing); + + client.awc = new AWClient("aw-client-web", client.testing); + client.createBucket(); + + // Needed in order to show testing information in popup + chrome.storage.local.set({"testing": client.testing}); + }); + }, + + getBucketId: function() { + // TODO: This works for Chrome and Firefox, but is a bit hacky and wont work in the general case + var browserName = /(Chrome|Firefox)\/([0-9.]+)/.exec(navigator.userAgent)[1]; + return "aw-watcher-web-" + browserName.toLowerCase(); + }, + + 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"; + + client.awc.createBucket(bucket_id, eventtype, hostname) + .catch( (err) => { + console.error("Failed to create bucket ("+err.response.status+"): "+err.response.data.message); + } + ); + }, + + sendHeartbeat: function(timestamp, data, pulsetime) { + if (this.testing === null) + return; + + var payload = {"data": data, "timestamp": timestamp.toISOString()}; + this.awc.heartbeat(this.getBucketId(), pulsetime, payload).then( + (res) => { + client.lastSyncSuccess = true; + chrome.storage.local.set({"lastSync": new Date().toISOString()}); + }, (err) => { + if(client.lastSyncSuccess) { + chrome.notifications.create({ + "type": "basic", + "iconUrl": chrome.extension.getURL("media/logo/logo.png"), + "title": "Unable to send event to server", + "message": "Please ensure that ActivityWatch is running", + }); + client.lastSyncSuccess = false; + } + console.error("Status code: " + err.response.status + ", response: " + err.response.data.message); + } + ); + } +}; + +module.exports = client; diff --git a/app/eventPage.js b/src/eventPage.js similarity index 98% rename from app/eventPage.js rename to src/eventPage.js index 44f5400..d57b014 100644 --- a/app/eventPage.js +++ b/src/eventPage.js @@ -3,6 +3,7 @@ * https://developer.chrome.com/extensions/event_pages */ +var client = require("./client.js") "use strict"; diff --git a/app/popup_files/popup.js b/src/popup.js similarity index 95% rename from app/popup_files/popup.js rename to src/popup.js index 1bad7c8..fdf0f39 100644 --- a/app/popup_files/popup.js +++ b/src/popup.js @@ -1,5 +1,7 @@ "use strict"; +const client = require("./client.js"); + function getCurrentTabs(callback) { // Query filter to be passed to chrome.tabs.query - see // https://developer.chrome.com/extensions/tabs#method-query @@ -37,7 +39,7 @@ function renderStatus() { ""; msg += ""; document.getElementById('status').innerHTML = msg; - document.getElementById('webui-link').href = client._getHost(); + document.getElementById('webui-link').href = client.awc.baseURL; }); } diff --git a/app/popup.html b/static/popup.html similarity index 87% rename from app/popup.html rename to static/popup.html index 08779aa..50a2f63 100644 --- a/app/popup.html +++ b/static/popup.html @@ -4,10 +4,9 @@ ActivityWatch Popup - + - - + diff --git a/app/popup_files/style.css b/static/style.css similarity index 100% rename from app/popup_files/style.css rename to static/style.css