From 9e819390c0ed308e855c1e973fb45c9723f8bd9a Mon Sep 17 00:00:00 2001 From: Mario Basic Date: Wed, 3 Jun 2015 01:33:46 +0200 Subject: [PATCH] Started working on 2 min heartbeats. --- assets/js/UrlHelper.js | 12 ++++ assets/js/WakaTime.js | 66 +++++++++++++++++- assets/js/events.js | 37 ++++++++++ assets/js/options.js | 21 +++++- gulpfile.js | 4 +- manifest.json | 3 +- options.html | 19 +++++ public/js/events.js | 155 ++++++++++++++++++++++++++++++++++++++++- public/js/options.js | 18 ++++- 9 files changed, 324 insertions(+), 11 deletions(-) create mode 100644 assets/js/UrlHelper.js diff --git a/assets/js/UrlHelper.js b/assets/js/UrlHelper.js new file mode 100644 index 0000000..52e3b06 --- /dev/null +++ b/assets/js/UrlHelper.js @@ -0,0 +1,12 @@ +class UrlHelper { + + static getDomainFromUrl(url) + { + var parts = url.split('/'); + + return parts[0] + "//" + parts[2]; + } + +} + +export default UrlHelper; diff --git a/assets/js/WakaTime.js b/assets/js/WakaTime.js index 6c48115..5ef15a4 100644 --- a/assets/js/WakaTime.js +++ b/assets/js/WakaTime.js @@ -1,6 +1,66 @@ -/** - * I think that I am going to need this. - */ +var UrlHelper = require('./UrlHelper'); + class WakaTime { + detectionIntervalInSeconds = 60; //default + + loggingType = 'domain'; //default + + recordHeartbeat() + { + console.log('recording heartbeat.'); + + chrome.idle.queryState(this.detectionIntervalInSeconds, (newState) => { + + console.log(newState); + + if(newState === 'active') + { + // Get current tab URL. + chrome.tabs.query({active: true}, (tabs) => { + console.log(tabs[0].url); + + this.sendHeartbeat(tabs[0].url); + }); + } + }) + } + + sendHeartbeat(entity) + { + chrome.storage.sync.get({ + loggingType: this.loggingType + }, function(items) { + + if(items.loggingType == 'domain') { + console.log('sending entity with type domain'); + + // Get only the domain from the entity. + // And send that in heartbeat + console.log(UrlHelper.getDomainFromUrl(entity)); + + var domain = UrlHelper.getDomainFromUrl(entity); + + var data = { + entity: domain, + type: 'domain', + time: Math.round((new Date()).getTime() / 1000), + is_debugging: false + }; + + console.log(data); + + + } + else if (items.loggingType == 'url') { + console.log('sending entity with type url'); + + // Send entity in heartbeat + } + + }); + } + } + +export default WakaTime; diff --git a/assets/js/events.js b/assets/js/events.js index e69de29..a183d12 100644 --- a/assets/js/events.js +++ b/assets/js/events.js @@ -0,0 +1,37 @@ +// What to do when the event page first loads? +// +// Functional style +// +// 1. Run a function every 2 mins DONE +// 2. Check if the user is active (not idle) +// If the user is active, get the current active tab URL +// and send heartbeat to wakatime. +// + +var WakaTime = require('./WakaTime'); + +/** + * Whenever an alarms sets off, this function + * gets called to detect the alarm name and + * do appropriate action. + * + * @param alarm + */ +function resolveAlarm(alarm) { + // |alarm| can be undefined because onAlarm also gets called from + // window.setTimeout on old chrome versions. + if (alarm && alarm.name == 'heartbeatAlarm') { + + console.log('heartbeatAlarm triggered'); + + var wakatime = new WakaTime; + + wakatime.recordHeartbeat(); + } +} + +// Add a listener to resolve alarms +chrome.alarms.onAlarm.addListener(resolveAlarm); + +// Create a new alarm for heartbeats. +chrome.alarms.create('heartbeatAlarm', {periodInMinutes: 1}); diff --git a/assets/js/options.js b/assets/js/options.js index 8a4f2d1..450d4c5 100644 --- a/assets/js/options.js +++ b/assets/js/options.js @@ -4,16 +4,31 @@ require('bootstrap'); var $ = require('jquery'); +function detectCheckedRadio(name) +{ + for(var i = 0; i < document.getElementsByName(name).length; i++) + { + var button = document.getElementsByName(name)[i]; + + if(button.checked === true) + { + return button.value; + } + } +} + // Saves options to chrome.storage.sync. function save_options(e) { e.preventDefault(); var theme = document.getElementById('theme').value; var blacklist = document.getElementById('blacklist').value; + var loggingType = detectCheckedRadio('loggingType'); chrome.storage.sync.set({ theme: theme, - blacklist: blacklist + blacklist: blacklist, + loggingType: loggingType }, function() { // Update status to let user know options were saved. var status = $('#status'); @@ -36,10 +51,12 @@ function restore_options() { // Use default value color = 'red' and likesColor = true. chrome.storage.sync.get({ theme: 'light', - blacklist: '' + blacklist: '', + loggingType: 'domain' }, function(items) { document.getElementById('theme').value = items.theme; document.getElementById('blacklist').value = items.blacklist; + document.getElementById(items.loggingType + 'Type').checked = true; }); } diff --git a/gulpfile.js b/gulpfile.js index 9786064..b3d29b9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -19,7 +19,7 @@ elixir(function (mix) { mix.copy('vendor/bower_components/font-awesome/less', 'assets/less/font-awesome'); mix.copy('vendor/bower_components/font-awesome/fonts', 'public/fonts'); mix.less('app.less'); - mix.browserify('app.js', null, 'assets/js'); - //mix.browserify('events.js', 'public/js/events.js', 'assets/js'); + //mix.browserify('app.js', null, 'assets/js'); + mix.browserify('events.js', 'public/js/events.js', 'assets/js'); //mix.browserify('options.js', 'public/js/options.js', 'assets/js'); }); diff --git a/manifest.json b/manifest.json index 1b4f2f7..2a5a8a4 100644 --- a/manifest.json +++ b/manifest.json @@ -13,7 +13,8 @@ "https://wakatime.com/logout", "alarms", "tabs", - "storage" + "storage", + "idle" ], "background": { "scripts": [ diff --git a/options.html b/options.html index 7bcd032..229361c 100644 --- a/options.html +++ b/options.html @@ -36,6 +36,25 @@ + +
+ +
+
+ +
+
+ +
+
+
+
diff --git a/public/js/events.js b/public/js/events.js index 166f2e9..2e848f1 100644 --- a/public/js/events.js +++ b/public/js/events.js @@ -1,4 +1,157 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o