Started working on 2 min heartbeats.

This commit is contained in:
Mario Basic
2015-06-03 01:33:46 +02:00
parent d44bda3767
commit 9e819390c0
9 changed files with 324 additions and 11 deletions

12
assets/js/UrlHelper.js Normal file
View File

@@ -0,0 +1,12 @@
class UrlHelper {
static getDomainFromUrl(url)
{
var parts = url.split('/');
return parts[0] + "//" + parts[2];
}
}
export default UrlHelper;

View File

@@ -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;

View File

@@ -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});

View File

@@ -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;
});
}