chore: remove duplicated logic, move recordHeartbeat to a single place
This commit is contained in:
@@ -8,7 +8,7 @@ import config from '../config/config';
|
||||
import { SendHeartbeat } from '../types/heartbeats';
|
||||
import { GrandTotal, SummariesPayload } from '../types/summaries';
|
||||
import { ApiKeyPayload, AxiosUserResponse, User } from '../types/user';
|
||||
import { generateProjectFromDevSites, IS_FIREFOX } from '../utils';
|
||||
import { IS_FIREFOX, generateProjectFromDevSites } from '../utils';
|
||||
import { getApiKey } from '../utils/apiKey';
|
||||
import changeExtensionState from '../utils/changeExtensionState';
|
||||
import contains from '../utils/contains';
|
||||
@@ -112,7 +112,7 @@ class WakaTimeCore {
|
||||
* Depending on various factors detects the current active tab URL or domain,
|
||||
* and sends it to WakaTime for logging.
|
||||
*/
|
||||
async recordHeartbeat(): Promise<void> {
|
||||
async recordHeartbeat(payload = {}): Promise<void> {
|
||||
const apiKey = await getApiKey();
|
||||
if (!apiKey) {
|
||||
return changeExtensionState('notLogging');
|
||||
@@ -129,62 +129,69 @@ class WakaTimeCore {
|
||||
if (items.loggingEnabled === true) {
|
||||
await changeExtensionState('allGood');
|
||||
|
||||
const newState = await browser.idle.queryState(config.detectionIntervalInSeconds);
|
||||
|
||||
if (newState === 'active') {
|
||||
// Get current tab URL.
|
||||
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
||||
if (tabs.length == 0) return;
|
||||
|
||||
const currentActiveTab = tabs[0];
|
||||
|
||||
if (!items.trackSocialMedia) {
|
||||
if (contains(currentActiveTab.url as string, items.socialMediaSites as string)) {
|
||||
return changeExtensionState('blacklisted');
|
||||
}
|
||||
}
|
||||
|
||||
// Checks dev websites
|
||||
const project = generateProjectFromDevSites(currentActiveTab.url as string);
|
||||
|
||||
if (items.loggingStyle == 'blacklist') {
|
||||
if (!contains(currentActiveTab.url as string, items.blacklist as string)) {
|
||||
await this.sendHeartbeat(
|
||||
{
|
||||
hostname: items.hostname as string,
|
||||
project,
|
||||
url: currentActiveTab.url as string,
|
||||
},
|
||||
apiKey,
|
||||
);
|
||||
} else {
|
||||
await changeExtensionState('blacklisted');
|
||||
console.log(`${currentActiveTab.url} is on a blacklist.`);
|
||||
}
|
||||
}
|
||||
|
||||
if (items.loggingStyle == 'whitelist') {
|
||||
const heartbeat = this.getHeartbeat(
|
||||
currentActiveTab.url as string,
|
||||
items.whitelist as string,
|
||||
);
|
||||
if (heartbeat.url) {
|
||||
await this.sendHeartbeat(
|
||||
{
|
||||
...heartbeat,
|
||||
hostname: items.hostname as string,
|
||||
project: heartbeat.project ?? project,
|
||||
},
|
||||
apiKey,
|
||||
);
|
||||
} else {
|
||||
await changeExtensionState('whitelisted');
|
||||
console.log(`${currentActiveTab.url} is not on a whitelist.`);
|
||||
}
|
||||
let newState = '';
|
||||
// Detects we are running this code in the extension scope
|
||||
if (browser.idle as browser.Idle.Static | undefined) {
|
||||
newState = await browser.idle.queryState(config.detectionIntervalInSeconds);
|
||||
if (newState !== 'active') {
|
||||
return changeExtensionState('notLogging');
|
||||
}
|
||||
}
|
||||
|
||||
// Get current tab URL.
|
||||
let url = '';
|
||||
if (browser.tabs as browser.Tabs.Static | undefined) {
|
||||
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
||||
if (tabs.length == 0) return;
|
||||
const currentActiveTab = tabs[0];
|
||||
url = currentActiveTab.url as string;
|
||||
} else {
|
||||
url = document.URL;
|
||||
}
|
||||
|
||||
if (!items.trackSocialMedia) {
|
||||
if (contains(url, items.socialMediaSites as string)) {
|
||||
return changeExtensionState('blacklisted');
|
||||
}
|
||||
}
|
||||
|
||||
// Checks dev websites
|
||||
const project = generateProjectFromDevSites(url);
|
||||
|
||||
if (items.loggingStyle == 'blacklist') {
|
||||
if (!contains(url, items.blacklist as string)) {
|
||||
await this.sendHeartbeat(
|
||||
{
|
||||
hostname: items.hostname as string,
|
||||
project,
|
||||
url,
|
||||
},
|
||||
apiKey,
|
||||
payload,
|
||||
);
|
||||
} else {
|
||||
await changeExtensionState('blacklisted');
|
||||
console.log(`${url} is on a blacklist.`);
|
||||
}
|
||||
}
|
||||
|
||||
if (items.loggingStyle == 'whitelist') {
|
||||
const heartbeat = this.getHeartbeat(url, items.whitelist as string);
|
||||
if (heartbeat.url) {
|
||||
await this.sendHeartbeat(
|
||||
{
|
||||
...heartbeat,
|
||||
hostname: items.hostname as string,
|
||||
project: heartbeat.project ?? project,
|
||||
},
|
||||
apiKey,
|
||||
payload,
|
||||
);
|
||||
} else {
|
||||
await changeExtensionState('whitelisted');
|
||||
console.log(`${url} is not on a whitelist.`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
await changeExtensionState('notLogging');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,7 +272,11 @@ class WakaTimeCore {
|
||||
* @param heartbeat
|
||||
* @param debug
|
||||
*/
|
||||
async sendHeartbeat(heartbeat: SendHeartbeat, apiKey: string): Promise<void> {
|
||||
async sendHeartbeat(
|
||||
heartbeat: SendHeartbeat,
|
||||
apiKey: string,
|
||||
navigationPayload: Record<string, unknown>,
|
||||
): Promise<void> {
|
||||
let payload;
|
||||
|
||||
const loggingType = await this.getLoggingType();
|
||||
@@ -274,12 +285,20 @@ class WakaTimeCore {
|
||||
if (loggingType == 'domain') {
|
||||
heartbeat.url = getDomainFromUrl(heartbeat.url);
|
||||
payload = this.preparePayload(heartbeat, 'domain');
|
||||
await this.sendPostRequestToApi(payload, apiKey, heartbeat.hostname);
|
||||
await this.sendPostRequestToApi(
|
||||
{ ...payload, ...navigationPayload },
|
||||
apiKey,
|
||||
heartbeat.hostname,
|
||||
);
|
||||
}
|
||||
// Send entity in heartbeat
|
||||
else if (loggingType == 'url') {
|
||||
payload = this.preparePayload(heartbeat, 'url');
|
||||
await this.sendPostRequestToApi(payload, apiKey, heartbeat.hostname);
|
||||
await this.sendPostRequestToApi(
|
||||
{ ...payload, ...navigationPayload },
|
||||
apiKey,
|
||||
heartbeat.hostname,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user