Minor optimizations.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
class UrlHelper {
|
||||
|
||||
static getDomainFromUrl(url)
|
||||
{
|
||||
static getDomainFromUrl(url) {
|
||||
var parts = url.split('/');
|
||||
|
||||
return parts[0] + "//" + parts[2];
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
var UrlHelper = require('./UrlHelper');
|
||||
|
||||
var $ = require('jquery');
|
||||
|
||||
var currentTimestamp = require('./helpers/currentTimestamp');
|
||||
var changeExtensionIcon = require('./helpers/changeExtensionIcon');
|
||||
import UrlHelper from './UrlHelper.js';
|
||||
import $ from 'jquery';
|
||||
import currentTimestamp from './helpers/currentTimestamp.js';
|
||||
import changeExtensionIcon from './helpers/changeExtensionIcon.js';
|
||||
|
||||
class WakaTime {
|
||||
|
||||
@@ -18,16 +16,15 @@ class WakaTime {
|
||||
/**
|
||||
* Checks if the user is logged in.
|
||||
*
|
||||
* @return $.promise()
|
||||
* @returns {*}
|
||||
*/
|
||||
checkAuth()
|
||||
{
|
||||
checkAuth() {
|
||||
var deferredObject = $.Deferred();
|
||||
|
||||
$.ajax({
|
||||
url: this.currentUserApiUrl,
|
||||
dataType: 'json',
|
||||
success: (data) => {
|
||||
success: (data) => {
|
||||
|
||||
deferredObject.resolve(data.data);
|
||||
|
||||
@@ -46,14 +43,11 @@ class WakaTime {
|
||||
/**
|
||||
* Depending on various factors detects the current active tab URL or domain,
|
||||
* and sends it to WakaTime for logging.
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
recordHeartbeat()
|
||||
{
|
||||
recordHeartbeat() {
|
||||
this.checkAuth().done(data => {
|
||||
|
||||
if(data !== false){
|
||||
if (data !== false) {
|
||||
|
||||
// User is logged in.
|
||||
// Change extension icon to default color.
|
||||
@@ -61,8 +55,7 @@ class WakaTime {
|
||||
|
||||
chrome.idle.queryState(this.detectionIntervalInSeconds, (newState) => {
|
||||
|
||||
if(newState === 'active')
|
||||
{
|
||||
if (newState === 'active') {
|
||||
// Get current tab URL.
|
||||
chrome.tabs.query({active: true}, (tabs) => {
|
||||
this.sendHeartbeat(tabs[0].url);
|
||||
@@ -83,13 +76,13 @@ class WakaTime {
|
||||
/**
|
||||
* Creates payload for the heartbeat and returns it as JSON.
|
||||
*
|
||||
* @param string entity
|
||||
* @param string type 'domain' or 'url'
|
||||
* @param boolean debug = false
|
||||
* @return JSON
|
||||
* @param entity
|
||||
* @param type
|
||||
* @param debug
|
||||
* @returns {*}
|
||||
* @private
|
||||
*/
|
||||
_preparePayload(entity, type, debug = false)
|
||||
{
|
||||
_preparePayload(entity, type, debug = false) {
|
||||
return JSON.stringify({
|
||||
entity: entity,
|
||||
type: type,
|
||||
@@ -98,18 +91,19 @@ class WakaTime {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a promise with logging type variable.
|
||||
*
|
||||
* @return $.promise
|
||||
* @returns {*}
|
||||
* @private
|
||||
*/
|
||||
_getLoggingType()
|
||||
{
|
||||
_getLoggingType() {
|
||||
var deferredObject = $.Deferred();
|
||||
|
||||
chrome.storage.sync.get({
|
||||
loggingType: this.loggingType
|
||||
}, function(items) {
|
||||
}, function (items) {
|
||||
deferredObject.resolve(items.loggingType);
|
||||
});
|
||||
|
||||
@@ -120,20 +114,21 @@ class WakaTime {
|
||||
* Given the entity and logging type it creates a payload and
|
||||
* sends an ajax post request to the API.
|
||||
*
|
||||
* @param string entity
|
||||
* @return null
|
||||
* @param entity
|
||||
*/
|
||||
sendHeartbeat(entity)
|
||||
{
|
||||
sendHeartbeat(entity) {
|
||||
|
||||
var payload = null;
|
||||
|
||||
this._getLoggingType().done((loggingType) => {
|
||||
|
||||
// Get only the domain from the entity.
|
||||
// And send that in heartbeat
|
||||
if(loggingType == 'domain') {
|
||||
if (loggingType == 'domain') {
|
||||
|
||||
var domain = UrlHelper.getDomainFromUrl(entity);
|
||||
|
||||
var payload = this._preparePayload(domain, 'domain');
|
||||
payload = this._preparePayload(domain, 'domain');
|
||||
|
||||
console.log(payload);
|
||||
|
||||
@@ -142,7 +137,7 @@ class WakaTime {
|
||||
}
|
||||
// Send entity in heartbeat
|
||||
else if (loggingType == 'url') {
|
||||
var payload = this._preparePayload(entity, 'url');
|
||||
payload = this._preparePayload(entity, 'url');
|
||||
|
||||
console.log(payload);
|
||||
|
||||
@@ -155,9 +150,9 @@ class WakaTime {
|
||||
/**
|
||||
* Sends AJAX request with payload to the heartbeat API as JSON.
|
||||
*
|
||||
* @param JSON payload
|
||||
* @param string method = 'POST'
|
||||
* @return $.promise
|
||||
* @param payload
|
||||
* @param method
|
||||
* @returns {*}
|
||||
*/
|
||||
sendAjaxRequestToApi(payload, method = 'POST') {
|
||||
|
||||
@@ -169,7 +164,7 @@ class WakaTime {
|
||||
contentType: 'application/json',
|
||||
method: method,
|
||||
data: payload,
|
||||
success: (response) => {
|
||||
success: (response) => {
|
||||
|
||||
deferredObject.resolve(this);
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
global.jQuery = require('jquery');
|
||||
require('bootstrap');
|
||||
|
||||
var React = require('react');
|
||||
var WakaTime = require('./components/WakaTime.react');
|
||||
import React from 'react';
|
||||
import WakaTime from './components/WakaTime.react.js';
|
||||
|
||||
React.render(
|
||||
<WakaTime />,
|
||||
|
||||
@@ -1,53 +1,51 @@
|
||||
var React = require('react');
|
||||
import React from 'react';
|
||||
|
||||
class MainList extends React.Component
|
||||
{
|
||||
componentDidMount()
|
||||
{
|
||||
class MainList extends React.Component {
|
||||
componentDidMount() {
|
||||
|
||||
}
|
||||
|
||||
_openOptionsPage()
|
||||
{
|
||||
if (chrome.runtime.openOptionsPage) {
|
||||
// New way to open options pages, if supported (Chrome 42+).
|
||||
chrome.runtime.openOptionsPage();
|
||||
} else {
|
||||
// Reasonable fallback.
|
||||
window.open(chrome.runtime.getURL('options.html'));
|
||||
}
|
||||
_openOptionsPage() {
|
||||
if (chrome.runtime.openOptionsPage) {
|
||||
// New way to open options pages, if supported (Chrome 42+).
|
||||
chrome.runtime.openOptionsPage();
|
||||
} else {
|
||||
// Reasonable fallback.
|
||||
window.open(chrome.runtime.getURL('options.html'));
|
||||
}
|
||||
}
|
||||
|
||||
render()
|
||||
{
|
||||
render() {
|
||||
var loginLogoutButton = () => {
|
||||
if(this.props.loggedIn === true)
|
||||
{
|
||||
if (this.props.loggedIn === true) {
|
||||
return (
|
||||
<div>
|
||||
<a target="_blank" href="https://wakatime.com/settings/rules" className="list-group-item">
|
||||
<i className="fa fa-fw fa-filter"></i> Custom Rules
|
||||
</a>
|
||||
<a target="_blank" href="https://wakatime.com/dashboard" className="list-group-item">
|
||||
<i className="fa fa-fw fa-tachometer"></i> Dashboard
|
||||
</a>
|
||||
<a href="#" className="list-group-item" onClick={this.props.logoutUser}>
|
||||
<i className="fa fa-fw fa-sign-out"></i> Logout
|
||||
</a>
|
||||
<a target="_blank" href="https://wakatime.com/settings/rules" className="list-group-item">
|
||||
<i className="fa fa-fw fa-filter"></i>
|
||||
Custom Rules
|
||||
</a>
|
||||
<a target="_blank" href="https://wakatime.com/dashboard" className="list-group-item">
|
||||
<i className="fa fa-fw fa-tachometer"></i>
|
||||
Dashboard
|
||||
</a>
|
||||
<a href="#" className="list-group-item" onClick={this.props.logoutUser}>
|
||||
<i className="fa fa-fw fa-sign-out"></i>
|
||||
Logout
|
||||
</a>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<a target="_blank" href="https://wakatime.com/login" className="list-group-item">
|
||||
<i className="fa fa-fw fa-sign-in"></i> Login
|
||||
<i className="fa fa-fw fa-sign-in"></i>
|
||||
Login
|
||||
</a>
|
||||
);
|
||||
};
|
||||
|
||||
var signedInAs = () => {
|
||||
if(this.props.loggedIn === true)
|
||||
{
|
||||
if (this.props.loggedIn === true) {
|
||||
return (
|
||||
<div className="panel panel-default">
|
||||
<div className="panel-body">
|
||||
@@ -56,7 +54,9 @@ class MainList extends React.Component
|
||||
<img className="img-circle" width="48" height="48" src={this.props.user.photo} />
|
||||
</div>
|
||||
<div className="col-xs-10">
|
||||
Signed in as <b>{this.props.user.full_name}</b><br />
|
||||
Signed in as
|
||||
<b>{this.props.user.full_name}</b>
|
||||
<br />
|
||||
{this.props.user.email}
|
||||
</div>
|
||||
</div>
|
||||
@@ -66,14 +66,15 @@ class MainList extends React.Component
|
||||
}
|
||||
};
|
||||
|
||||
return(
|
||||
return (
|
||||
<div>
|
||||
|
||||
{signedInAs()}
|
||||
|
||||
<div className="list-group">
|
||||
<a href="#" className="list-group-item" onClick={this._openOptionsPage}>
|
||||
<i className="fa fa-fw fa-cogs"></i> Options
|
||||
<i className="fa fa-fw fa-cogs"></i>
|
||||
Options
|
||||
</a>
|
||||
|
||||
{loginLogoutButton()}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var React = require('react');
|
||||
import React from 'react';
|
||||
|
||||
class Navbar extends React.Component{
|
||||
class Navbar extends React.Component {
|
||||
|
||||
render() {
|
||||
return (
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
var React = require("react");
|
||||
var $ = require('jquery');
|
||||
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 NavBar = require('./NavBar.react');
|
||||
var MainList = require('./MainList.react');
|
||||
|
||||
var changeExtensionIcon = require('../helpers/changeExtensionIcon');
|
||||
|
||||
var WakaTimeOriginal = require('../WakaTime');
|
||||
|
||||
class WakaTime extends React.Component
|
||||
{
|
||||
class WakaTime extends React.Component {
|
||||
logoutUserUrl = 'https://wakatime.com/logout';
|
||||
|
||||
state = {
|
||||
@@ -21,56 +17,49 @@ class WakaTime extends React.Component
|
||||
loggedIn: false
|
||||
};
|
||||
|
||||
componentDidMount()
|
||||
{
|
||||
chrome.storage.sync.get({
|
||||
theme: 'light'
|
||||
}, function(items) {
|
||||
if(items.theme == 'light') {
|
||||
changeExtensionIcon();
|
||||
}
|
||||
else {
|
||||
changeExtensionIcon('white');
|
||||
}
|
||||
});
|
||||
componentDidMount() {
|
||||
chrome.storage.sync.get({
|
||||
theme: 'light'
|
||||
}, function (items) {
|
||||
if (items.theme == 'light') {
|
||||
changeExtensionIcon();
|
||||
}
|
||||
else {
|
||||
changeExtensionIcon('white');
|
||||
}
|
||||
});
|
||||
|
||||
var wakatime = new WakaTimeOriginal;
|
||||
var wakatime = new WakaTimeOriginal;
|
||||
|
||||
wakatime.checkAuth().done(data => {
|
||||
wakatime.checkAuth().done(data => {
|
||||
|
||||
if(data !== false){
|
||||
if (data !== false) {
|
||||
|
||||
this.setState({
|
||||
user: {
|
||||
full_name: data.full_name,
|
||||
email: data.email,
|
||||
photo: data.photo
|
||||
},
|
||||
loggedIn: true
|
||||
});
|
||||
this.setState({
|
||||
user: {
|
||||
full_name: data.full_name,
|
||||
email: data.email,
|
||||
photo: data.photo
|
||||
},
|
||||
loggedIn: true
|
||||
});
|
||||
|
||||
changeExtensionIcon();
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
changeExtensionIcon('red');
|
||||
|
||||
//TODO: Redirect user to wakatime login page.
|
||||
//
|
||||
}
|
||||
});
|
||||
changeExtensionIcon();
|
||||
}
|
||||
else {
|
||||
changeExtensionIcon('red');
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
logoutUser()
|
||||
{
|
||||
logoutUser() {
|
||||
var deferredObject = $.Deferred();
|
||||
|
||||
$.ajax({
|
||||
url: this.logoutUserUrl,
|
||||
method: 'GET',
|
||||
success: () => {
|
||||
success: () => {
|
||||
|
||||
deferredObject.resolve(this);
|
||||
|
||||
@@ -86,8 +75,7 @@ class WakaTime extends React.Component
|
||||
return deferredObject.promise();
|
||||
}
|
||||
|
||||
_logoutUser()
|
||||
{
|
||||
_logoutUser() {
|
||||
this.logoutUser().done(() => {
|
||||
|
||||
this.setState({
|
||||
@@ -104,9 +92,8 @@ class WakaTime extends React.Component
|
||||
});
|
||||
}
|
||||
|
||||
render()
|
||||
{
|
||||
return(
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
<NavBar />
|
||||
<div className="container">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var WakaTime = require('./WakaTime');
|
||||
import WakaTime from "./WakaTime.js";
|
||||
|
||||
/**
|
||||
* Whenever an alarms sets off, this function
|
||||
@@ -29,15 +29,15 @@ chrome.alarms.create('heartbeatAlarm', {periodInMinutes: 2});
|
||||
/**
|
||||
* Whenever a active tab is changed it records a heartbeat with that tab url.
|
||||
*/
|
||||
chrome.tabs.onActivated.addListener(function(activeInfo) {
|
||||
chrome.tabs.onActivated.addListener(function (activeInfo) {
|
||||
|
||||
chrome.tabs.get(activeInfo.tabId, function(tab) {
|
||||
chrome.tabs.get(activeInfo.tabId, function (tab) {
|
||||
|
||||
console.log('recording a heartbeat - active tab changed');
|
||||
console.log('recording a heartbeat - active tab changed');
|
||||
|
||||
var wakatime = new WakaTime;
|
||||
var wakatime = new WakaTime;
|
||||
|
||||
wakatime.recordHeartbeat();
|
||||
wakatime.recordHeartbeat();
|
||||
});
|
||||
|
||||
});
|
||||
@@ -46,15 +46,13 @@ chrome.tabs.onActivated.addListener(function(activeInfo) {
|
||||
* Whenever any tab is updated it checks if the updated tab is the tab that is
|
||||
* currently active and if it is, then it records a heartbeat.
|
||||
*/
|
||||
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
|
||||
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
|
||||
|
||||
if(changeInfo.status === 'complete')
|
||||
{
|
||||
if (changeInfo.status === 'complete') {
|
||||
// Get current tab URL.
|
||||
chrome.tabs.query({active: true}, (tabs) => {
|
||||
// If tab updated is the same as active tab
|
||||
if(tabId == tabs[0].id)
|
||||
{
|
||||
if (tabId == tabs[0].id) {
|
||||
console.log('recording a heartbeat - tab updated');
|
||||
|
||||
var wakatime = new WakaTime;
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
/**
|
||||
* It changes the extension icon color.
|
||||
* Supported values are: 'red', 'white' and ''.
|
||||
*
|
||||
* @param string color = ''
|
||||
* @return null
|
||||
*/
|
||||
export default function changeExtensionIcon(color = '') {
|
||||
|
||||
if(color !== ''){
|
||||
if (color !== '') {
|
||||
color = '-' + color;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/**
|
||||
* Returns UNIX timestamp
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
export default function(){
|
||||
return Math.round((new Date()).getTime() / 1000);
|
||||
|
||||
@@ -2,16 +2,13 @@
|
||||
global.jQuery = require('jquery');
|
||||
require('bootstrap');
|
||||
|
||||
var $ = require('jquery');
|
||||
import $ from "jquery";
|
||||
|
||||
function detectCheckedRadio(name)
|
||||
{
|
||||
for(var i = 0; i < document.getElementsByName(name).length; i++)
|
||||
{
|
||||
function detectCheckedRadio(name) {
|
||||
for (var i = 0; i < document.getElementsByName(name).length; i ++) {
|
||||
var button = document.getElementsByName(name)[i];
|
||||
|
||||
if(button.checked === true)
|
||||
{
|
||||
if (button.checked === true) {
|
||||
return button.value;
|
||||
}
|
||||
}
|
||||
@@ -19,45 +16,45 @@ function detectCheckedRadio(name)
|
||||
|
||||
// Saves options to chrome.storage.sync.
|
||||
function save_options(e) {
|
||||
e.preventDefault();
|
||||
e.preventDefault();
|
||||
|
||||
var theme = document.getElementById('theme').value;
|
||||
var blacklist = document.getElementById('blacklist').value;
|
||||
var loggingType = detectCheckedRadio('loggingType');
|
||||
var theme = document.getElementById('theme').value;
|
||||
var blacklist = document.getElementById('blacklist').value;
|
||||
var loggingType = detectCheckedRadio('loggingType');
|
||||
|
||||
chrome.storage.sync.set({
|
||||
theme: theme,
|
||||
blacklist: blacklist,
|
||||
loggingType: loggingType
|
||||
}, function() {
|
||||
// Update status to let user know options were saved.
|
||||
var status = $('#status');
|
||||
status.html('<strong>Well done!</strong> Options have been saved.');
|
||||
chrome.storage.sync.set({
|
||||
theme: theme,
|
||||
blacklist: blacklist,
|
||||
loggingType: loggingType
|
||||
}, function () {
|
||||
// Update status to let user know options were saved.
|
||||
var status = $('#status');
|
||||
status.html('<strong>Well done!</strong> Options have been saved.');
|
||||
|
||||
status.fadeIn(1500, function() {
|
||||
setTimeout(function() {
|
||||
status.fadeOut(1500, function() {
|
||||
status.html('');
|
||||
status.fadeIn(1500, function () {
|
||||
setTimeout(function () {
|
||||
status.fadeOut(1500, function () {
|
||||
status.html('');
|
||||
});
|
||||
}, 750);
|
||||
});
|
||||
}, 750);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Restores select box and checkbox state using the preferences
|
||||
// stored in chrome.storage.
|
||||
function restore_options() {
|
||||
// Use default value color = 'red' and likesColor = true.
|
||||
chrome.storage.sync.get({
|
||||
theme: 'light',
|
||||
blacklist: '',
|
||||
loggingType: 'domain'
|
||||
}, function(items) {
|
||||
document.getElementById('theme').value = items.theme;
|
||||
document.getElementById('blacklist').value = items.blacklist;
|
||||
document.getElementById(items.loggingType + 'Type').checked = true;
|
||||
});
|
||||
// Use default value color = 'red' and likesColor = true.
|
||||
chrome.storage.sync.get({
|
||||
theme: 'light',
|
||||
blacklist: '',
|
||||
loggingType: 'domain'
|
||||
}, function (items) {
|
||||
document.getElementById('theme').value = items.theme;
|
||||
document.getElementById('blacklist').value = items.blacklist;
|
||||
document.getElementById(items.loggingType + 'Type').checked = true;
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', restore_options);
|
||||
|
||||
Reference in New Issue
Block a user