Added devtools detection.

This commit is contained in:
Mario Basic
2015-06-13 15:53:17 +02:00
parent e7a864f028
commit 1a2bac7cc3
11 changed files with 215 additions and 120 deletions

View File

@@ -2,10 +2,12 @@ import UrlHelper from './UrlHelper.js';
import $ from 'jquery';
import currentTimestamp from './helpers/currentTimestamp.js';
import changeExtensionIcon from './helpers/changeExtensionIcon.js';
import devtools from './libs/devtools-detect.js';
var in_array = require('./helpers/in_array');
class WakaTime {
tabsWithDevtoolsOpen = [];
constructor(props) {
this.detectionIntervalInSeconds = 60; //default
@@ -14,6 +16,12 @@ class WakaTime {
this.heartbeatApiUrl = 'https://wakatime.com/api/v1/users/current/heartbeats';
this.currentUserApiUrl = 'https://wakatime.com/api/v1/users/current';
this.tabsWithDevtoolsOpen = [];
}
setTabsWithDevtoolsOpen(tabs) {
this.tabsWithDevtoolsOpen = tabs;
}
/**
@@ -63,7 +71,11 @@ class WakaTime {
if (newState === 'active') {
// Get current tab URL.
chrome.tabs.query({active: true}, (tabs) => {
this.sendHeartbeat(tabs[0].url);
var debug = false;
// If the current active tab has devtools open
if(in_array(tabs[0].id, this.tabsWithDevtoolsOpen)) debug = true;
this.sendHeartbeat(tabs[0].url, debug);
});
}
});
@@ -124,14 +136,12 @@ class WakaTime {
* sends an ajax post request to the API.
*
* @param entity
* @param debug
*/
sendHeartbeat(entity) {
sendHeartbeat(entity, debug) {
var payload = null;
// TODO: Detect if devTools are open
console.log(devtools.open);
this._getLoggingType().done((loggingType) => {
// Get only the domain from the entity.
@@ -140,7 +150,7 @@ class WakaTime {
var domain = UrlHelper.getDomainFromUrl(entity);
payload = this._preparePayload(domain, 'domain');
payload = this._preparePayload(domain, 'domain', debug);
console.log(payload);
@@ -149,7 +159,7 @@ class WakaTime {
}
// Send entity in heartbeat
else if (loggingType == 'url') {
payload = this._preparePayload(entity, 'url');
payload = this._preparePayload(entity, 'url', debug);
console.log(payload);

9
assets/js/devtools.js Normal file
View File

@@ -0,0 +1,9 @@
// Create a connection to the background page
var backgroundPageConnection = chrome.runtime.connect({
name: "devtools-page"
});
backgroundPageConnection.postMessage({
name: 'init',
tabId: chrome.devtools.inspectedWindow.tabId
});

View File

@@ -1,5 +1,11 @@
import WakaTime from "./WakaTime.js";
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
@@ -14,8 +20,6 @@ function resolveAlarm(alarm) {
console.log('recording a heartbeat - alarm triggered');
var wakatime = new WakaTime;
wakatime.recordHeartbeat();
}
}
@@ -35,8 +39,6 @@ chrome.tabs.onActivated.addListener(function (activeInfo) {
console.log('recording a heartbeat - active tab changed');
var wakatime = new WakaTime;
wakatime.recordHeartbeat();
});
@@ -55,11 +57,45 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (tabId == tabs[0].id) {
console.log('recording a heartbeat - tab updated');
var wakatime = new WakaTime;
wakatime.recordHeartbeat();
}
});
}
});
chrome.runtime.onConnect.addListener(function (port) {
if (port.name == "devtools-page") {
// Listen to messages sent from the DevTools page
port.onMessage.addListener(function (message, sender, sendResponse) {
if (message.name == "init") {
connections[message.tabId] = port;
wakatime.setTabsWithDevtoolsOpen(Object.keys(connections));
wakatime.recordHeartbeat();
}
});
port.onDisconnect.addListener(function (port) {
var tabs = Object.keys(connections);
for (var i = 0, len = tabs.length; i < len; i ++) {
if (connections[tabs[i]] == port) {
delete connections[tabs[i]];
break;
}
}
wakatime.setTabsWithDevtoolsOpen(Object.keys(connections));
wakatime.recordHeartbeat();
});
}
});

View File

@@ -0,0 +1,12 @@
function in_array(needle, haystack) {
for (var i = 0; i < haystack.length; i ++) {
if (needle == haystack[i]) {
return true;
break;
}
}
return false;
}
export default in_array;