Major optimizations. Added a function for extension state. Tooltip is now being change also.

This commit is contained in:
Mario Basic
2015-06-13 22:07:39 +02:00
parent 3331d187b6
commit b8ca450969
21 changed files with 1211 additions and 1010 deletions

View File

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

View File

@@ -2,8 +2,10 @@
global.jQuery = require('jquery');
require('bootstrap');
import React from 'react';
import WakaTime from './components/WakaTime.react.js';
var React = require('react');
// React components
var WakaTime = require('./components/WakaTime.react');
React.render(
<WakaTime />,

View File

@@ -1,6 +1,6 @@
//jshint esnext:true
import React from 'react';
var React = require('react');
class MainList extends React.Component {
componentDidMount() {

View File

@@ -1,6 +1,6 @@
//jshint esnext:true
import React from 'react';
var React = require('react');
class Navbar extends React.Component {

View File

@@ -1,24 +1,37 @@
//jshint esnext:true
import React from "react";
import $ from 'jquery';
import NavBar from './NavBar.react.js';
import MainList from './MainList.react.js';
import changeExtensionIcon from '../helpers/changeExtensionIcon.js';
import WakaTimeOriginal from '../WakaTime.js';
var config = require('../config.js');
var React = require("react");
var $ = require('jquery');
var config = require('../config');
// React components
var NavBar = require('./NavBar.react');
var MainList = require('./MainList.react');
// Core
var WakaTimeOriginal = require('../core/WakaTime');
// Helpers
var changeExtensionState = require('../helpers/changeExtensionState');
class WakaTime extends React.Component {
state = {
user: {
full_name: null,
email: null,
photo: null
},
loggedIn: false,
loggingEnabled: config.loggingEnabled
};
constructor(){
super();
this.state = {
user: {
full_name: null,
email: null,
photo: null
},
loggedIn: false,
loggingEnabled: config.loggingEnabled
};
}
componentDidMount() {
@@ -33,10 +46,10 @@ class WakaTime extends React.Component {
}, (items) => {
this.setState({loggingEnabled: items.loggingEnabled});
if (items.loggingEnabled === true) {
changeExtensionIcon(config.colors.allGood);
changeExtensionState('allGood');
}
else {
changeExtensionIcon(config.colors.notLogging);
changeExtensionState('notLogging');
}
});
@@ -50,7 +63,7 @@ class WakaTime extends React.Component {
});
}
else {
changeExtensionIcon(config.colors.notSignedIn);
changeExtensionState('notSignedIn');
}
});
@@ -91,7 +104,7 @@ class WakaTime extends React.Component {
loggingEnabled: false
});
changeExtensionIcon(config.colors.notSignedIn);
changeExtensionState('notSignedIn');
});
}
@@ -101,7 +114,7 @@ class WakaTime extends React.Component {
loggingEnabled: false
});
changeExtensionIcon(config.colors.notLogging);
changeExtensionState('notLogging');
chrome.storage.sync.set({
loggingEnabled: false
@@ -113,7 +126,7 @@ class WakaTime extends React.Component {
loggingEnabled: true
});
changeExtensionIcon(config.colors.allGood);
changeExtensionState('allGood');
chrome.storage.sync.set({
loggingEnabled: true

View File

@@ -1,4 +1,6 @@
export default {
var config = {
// Extension name
name: 'WakaTime',
// Time for idle state of the browser
// The user is considered idle if there was
// no activity in the browser for x seconds
@@ -20,6 +22,19 @@ export default {
notSignedIn: 'red',
lightTheme: 'white'
},
// Tooltips for each of the extension states
tooltips: {
allGood: '',
notLogging: 'Not logging',
notSignedIn: 'Not signed In'
},
// Default theme
theme: 'light'
};
theme: 'light',
states: [
'allGood',
'notLogging',
'notSignedIn'
]
};
export default config;

View File

@@ -1,16 +1,24 @@
import UrlHelper from './UrlHelper.js';
import $ from 'jquery';
import currentTimestamp from './helpers/currentTimestamp.js';
import changeExtensionIcon from './helpers/changeExtensionIcon.js';
var in_array = require('./helpers/in_array');
var config = require('./config.js');
var $ = require('jquery');
var config = require('./../config');
// Helpers
var getDomainFromUrl = require('./../helpers/getDomainFromUrl');
var currentTimestamp = require('./../helpers/currentTimestamp');
var changeExtensionState = require('../helpers/changeExtensionState');
var in_array = require('./../helpers/in_array');
class WakaTime {
constructor(props) {
constructor() {
this.tabsWithDevtoolsOpen = [];
}
/**
* Settter for tabsWithDevtoolsOpen
*
* @param tabs
*/
setTabsWithDevtoolsOpen(tabs) {
this.tabsWithDevtoolsOpen = tabs;
}
@@ -55,7 +63,8 @@ class WakaTime {
loggingEnabled: config.loggingEnabled
}, (items) => {
if (items.loggingEnabled === true) {
changeExtensionIcon(config.colors.allGood);
changeExtensionState('allGood');
chrome.idle.queryState(config.detectionIntervalInSeconds, (newState) => {
@@ -64,7 +73,7 @@ class WakaTime {
chrome.tabs.query({active: true}, (tabs) => {
var debug = false;
// If the current active tab has devtools open
if(in_array(tabs[0].id, this.tabsWithDevtoolsOpen)) debug = true;
if (in_array(tabs[0].id, this.tabsWithDevtoolsOpen)) debug = true;
this.sendHeartbeat(tabs[0].url, debug);
});
@@ -72,7 +81,7 @@ class WakaTime {
});
}
else {
changeExtensionIcon(config.colors.notLogging);
changeExtensionState('notLogging');
}
});
}
@@ -80,7 +89,7 @@ class WakaTime {
// User is not logged in.
// Change extension icon to red color.
changeExtensionIcon(config.colors.notSignedIn);
changeExtensionState('notSignedIn');
}
});
}
@@ -129,7 +138,7 @@ class WakaTime {
* @param entity
* @param debug
*/
sendHeartbeat(entity, debug) {
sendHeartbeat(entity, debug) {
var payload = null;
@@ -139,7 +148,7 @@ class WakaTime {
// And send that in heartbeat
if (loggingType == 'domain') {
var domain = UrlHelper.getDomainFromUrl(entity);
var domain = getDomainFromUrl(entity);
payload = this._preparePayload(domain, 'domain', debug);

View File

@@ -3,6 +3,7 @@ var backgroundPageConnection = chrome.runtime.connect({
name: "devtools-page"
});
// Send a message to background page with the current active tabId
backgroundPageConnection.postMessage({
name: 'init',
tabId: chrome.devtools.inspectedWindow.tabId

View File

@@ -1,19 +1,15 @@
import WakaTime from "./WakaTime.js";
// Core
var WakaTime = require("./core/WakaTime");
// initialize class
var wakatime = new WakaTime;
// Holds currently open connections (ports) with devtools
// Uses tabId as index key.
var connections = {};
/**
* Whenever an alarms sets off, this function
* gets called to detect the alarm name and
* do appropriate action.
*
* @param alarm
*/
function resolveAlarm(alarm) {
// Add a listener to resolve alarms
chrome.alarms.onAlarm.addListener(function(alarm){
// |alarm| can be undefined because onAlarm also gets called from
// window.setTimeout on old chrome versions.
if (alarm && alarm.name == 'heartbeatAlarm') {
@@ -22,10 +18,7 @@ function resolveAlarm(alarm) {
wakatime.recordHeartbeat();
}
}
// Add a listener to resolve alarms
chrome.alarms.onAlarm.addListener(resolveAlarm);
});
// Create a new alarm for heartbeats.
chrome.alarms.create('heartbeatAlarm', {periodInMinutes: 2});
@@ -65,6 +58,10 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
});
/**
* This is in charge of detecting if devtools are opened or closed
* and sending a heartbeat depending on that.
*/
chrome.runtime.onConnect.addListener(function (port) {
if (port.name == "devtools-page") {

View File

@@ -1,8 +1,12 @@
var config = require('../config');
/**
* It changes the extension icon color.
* Supported values are: 'red', 'white', 'gray' and ''.
*
* @param color
*/
export default function changeExtensionIcon(color = '') {
function changeExtensionIcon(color = '') {
var path = null;
@@ -18,9 +22,9 @@ export default function changeExtensionIcon(color = '') {
if (color === '') {
chrome.storage.sync.get({
theme: 'light'
theme: config.theme
}, function (items) {
if (items.theme == 'light') {
if (items.theme == config.theme) {
path = './graphics/wakatime-logo-38.png';
chrome.browserAction.setIcon({
@@ -38,3 +42,5 @@ export default function changeExtensionIcon(color = '') {
}
}
export default changeExtensionIcon;

View File

@@ -0,0 +1,34 @@
var config = require('../config');
// Helpers
var changeExtensionIcon = require('./changeExtensionIcon');
var changeExtensionTooltip = require('./changeExtensionTooltip');
var in_array = require('./in_array');
/**
* Sets the current state of the extension.
*
* @param state
*/
function changeExtensionState(state){
if (! in_array(state, config.states)) {
throw new Error('Not a valid state!');
}
switch (state) {
case 'allGood':
changeExtensionIcon(config.colors.allGood);
changeExtensionTooltip(config.tooltips.allGood);
break;
case 'notLogging':
changeExtensionIcon(config.colors.notLogging);
changeExtensionTooltip(config.tooltips.notLogging);
break;
case 'notSignedIn':
changeExtensionIcon(config.colors.notSignedIn);
changeExtensionTooltip(config.tooltips.notSignedIn);
break;
}
}
export default changeExtensionState;

View File

@@ -0,0 +1,20 @@
var config = require('../config');
/**
* It changes the extension title
*
* @param text
*/
function changeExtensionTooltip(text) {
if (text === '') {
text = config.name;
}
else {
text = config.name + ' - ' + text;
}
chrome.browserAction.setTitle({title: text});
}
export default changeExtensionTooltip;

View File

@@ -1,6 +1,10 @@
/**
* Returns UNIX timestamp
*
* @returns {number}
*/
export default function(){
function currentTimestamp(){
return Math.round((new Date()).getTime() / 1000);
}
export default currentTimestamp;

View File

@@ -0,0 +1,13 @@
/**
* Returns domain from given URL.
*
* @param url
* @returns {string}
*/
function getDomainFromUrl(url) {
var parts = url.split('/');
return parts[0] + "//" + parts[2];
}
export default getDomainFromUrl;

View File

@@ -1,3 +1,10 @@
/**
* Returns boolean if needle is found in haystack or not.
*
* @param needle
* @param haystack
* @returns {boolean}
*/
function in_array(needle, haystack) {
for (var i = 0; i < haystack.length; i ++) {
if (needle == haystack[i]) {

View File

@@ -1,40 +0,0 @@
/*!
devtools-detect
Detect if DevTools is open
https://github.com/sindresorhus/devtools-detect
by Sindre Sorhus
MIT License
*/
(function () {
'use strict';
var devtools = {open: false};
var threshold = 160;
var emitEvent = function (state) {
window.dispatchEvent(new CustomEvent('devtoolschange', {
detail: {
open: state
}
}));
};
setInterval(function () {
if ((window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized) || window.outerWidth - window.innerWidth > threshold ||
window.outerHeight - window.innerHeight > threshold) {
if (!devtools.open) {
emitEvent(true);
}
devtools.open = true;
} else {
if (devtools.open) {
emitEvent(false);
}
devtools.open = false;
}
}, 500);
if (typeof module !== 'undefined' && module.exports) {
module.exports = devtools;
} else {
window.devtools = devtools;
}
})();

View File

@@ -2,9 +2,9 @@
global.jQuery = require('jquery');
require('bootstrap');
var config = require('./config');
var $ = require("jquery");
import $ from "jquery";
var config = require('./config');
function detectCheckedRadio(name) {
for (var i = 0; i < document.getElementsByName(name).length; i ++) {