diff --git a/src/background.ts b/src/background.ts index e69de29..b441701 100644 --- a/src/background.ts +++ b/src/background.ts @@ -0,0 +1,49 @@ +import browser from 'webextension-polyfill'; +import WakaTimeCore from './core/WakaTimeCore'; + +// Add a listener to resolve alarms +browser.alarms.onAlarm.addListener(async (alarm) => { + // |alarm| can be undefined because onAlarm also gets called from + // window.setTimeout on old chrome versions. + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (alarm?.name == 'heartbeatAlarm') { + console.log('recording a heartbeat - alarm triggered'); + + await WakaTimeCore.recordHeartbeat(); + } +}); + +/** + * Whenever a active tab is changed it records a heartbeat with that tab url. + */ +browser.tabs.onActivated.addListener(async () => { + console.log('recording a heartbeat - active tab changed'); + await WakaTimeCore.recordHeartbeat(); +}); + +/** + * Whenever a active window is changed it records a heartbeat with the active tab url. + */ +browser.windows.onFocusChanged.addListener(async (windowId) => { + if (windowId != browser.windows.WINDOW_ID_NONE) { + console.log('recording a heartbeat - active window changed'); + await WakaTimeCore.recordHeartbeat(); + } else { + console.log('lost focus'); + } +}); + +/** + * 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. + */ +browser.tabs.onUpdated.addListener(async (tabId, changeInfo) => { + if (changeInfo.status === 'complete') { + // Get current tab URL. + const [tab] = await browser.tabs.query({ active: true, currentWindow: true }); + // If tab updated is the same as active tab + if (tabId == tab.id) { + await WakaTimeCore.recordHeartbeat(); + } + } +}); diff --git a/src/components/MainList.test.tsx b/src/components/MainList.test.tsx index 9ef54a5..514587f 100644 --- a/src/components/MainList.test.tsx +++ b/src/components/MainList.test.tsx @@ -2,6 +2,16 @@ import React from 'react'; import { renderWithProviders } from '../utils/test-utils'; import MainList from './MainList'; +jest.mock('webextension-polyfill', () => { + return { + runtime: { + getManifest: () => { + return { version: 'test-version' }; + }, + }, + }; +}); + describe('MainList', () => { let loggingEnabled: boolean; let totalTimeLoggedToday: string; diff --git a/src/config/config.test.ts b/src/config/config.test.ts index 0c3d068..b44d77a 100644 --- a/src/config/config.test.ts +++ b/src/config/config.test.ts @@ -1,5 +1,15 @@ import config from './config'; +jest.mock('webextension-polyfill', () => { + return { + runtime: { + getManifest: () => { + return { version: 'test-version' }; + }, + }, + }; +}); + describe('wakatime config', () => { it('snapshot of config', () => { expect(config).toMatchInlineSnapshot(` diff --git a/src/config/config.ts b/src/config/config.ts index b6f15da..20a1106 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -1,3 +1,5 @@ +import browser from 'webextension-polyfill'; + /** * Logging */