implement service worker events
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -2,6 +2,16 @@ import React from 'react';
|
|||||||
import { renderWithProviders } from '../utils/test-utils';
|
import { renderWithProviders } from '../utils/test-utils';
|
||||||
import MainList from './MainList';
|
import MainList from './MainList';
|
||||||
|
|
||||||
|
jest.mock('webextension-polyfill', () => {
|
||||||
|
return {
|
||||||
|
runtime: {
|
||||||
|
getManifest: () => {
|
||||||
|
return { version: 'test-version' };
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
describe('MainList', () => {
|
describe('MainList', () => {
|
||||||
let loggingEnabled: boolean;
|
let loggingEnabled: boolean;
|
||||||
let totalTimeLoggedToday: string;
|
let totalTimeLoggedToday: string;
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
import config from './config';
|
import config from './config';
|
||||||
|
|
||||||
|
jest.mock('webextension-polyfill', () => {
|
||||||
|
return {
|
||||||
|
runtime: {
|
||||||
|
getManifest: () => {
|
||||||
|
return { version: 'test-version' };
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
describe('wakatime config', () => {
|
describe('wakatime config', () => {
|
||||||
it('snapshot of config', () => {
|
it('snapshot of config', () => {
|
||||||
expect(config).toMatchInlineSnapshot(`
|
expect(config).toMatchInlineSnapshot(`
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import browser from 'webextension-polyfill';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logging
|
* Logging
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user