diff --git a/src/config/config.ts b/src/config/config.ts index 2cc1c1f..e499c09 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -53,7 +53,7 @@ export interface Config { /** * API key use to query wakatime api */ - apiKey: ''; + apiKey: string; apiUrl: string; colors: Colors; /** @@ -66,14 +66,14 @@ export interface Config { * no activity in the browser for x second */ detectionIntervalInSeconds: number; - devSites: string[]; + /** * Url to which to send the heartbeat */ heartbeatApiEndPoint: string; - hostname: string; + /** * Is logging enabled */ @@ -89,6 +89,7 @@ export interface Config { */ name: string; nonTrackableSites: string[]; + queueName: string; socialMediaSites: string[]; states: ExtensionStatus[]; /** @@ -163,6 +164,8 @@ const config: Config = { nonTrackableSites: ['chrome://', 'about:'], + queueName: 'heartbeatQueue', + socialMediaSites: [ 'facebook.com', 'instagram.com', diff --git a/src/core/WakaTimeCore.ts b/src/core/WakaTimeCore.ts index 0f253bd..8e67a7d 100644 --- a/src/core/WakaTimeCore.ts +++ b/src/core/WakaTimeCore.ts @@ -29,18 +29,10 @@ class WakaTimeCore { */ async createDB() { const dbConnection = await openDB('wakatime', 1, { - upgrade(db, oldVersion) { - // Create a store of objects - const store = db.createObjectStore('heartbeatQueue', { + upgrade(db) { + db.createObjectStore(config.queueName, { keyPath: 'id', }); - // Switch over the oldVersion, *without breaks*, to allow the database to be incrementally upgraded. - switch (oldVersion) { - case 0: - // Placeholder to execute when database is created (oldVersion is 0) - case 1: - store.createIndex('id', 'id'); - } }, }); this.db = dbConnection; @@ -115,7 +107,7 @@ class WakaTimeCore { if (!this.shouldSendHeartbeat(heartbeat)) return; // append heartbeat to queue - await this.db?.add('heartbeatQueue', heartbeat); + await this.db?.add(config.queueName, heartbeat); await this.sendHeartbeats(); } @@ -173,14 +165,15 @@ class WakaTimeCore { return; } - const heartbeats = (await this.db?.getAll('heartbeatQueue', undefined, 50)) as + const heartbeats = (await this.db?.getAll(config.queueName, undefined, 50)) as | Heartbeat[] | undefined; if (!heartbeats || heartbeats.length === 0) return; - await this.db?.delete( - 'heartbeatQueue', - heartbeats.map((heartbeat) => heartbeat.id), + await Promise.all( + heartbeats.map((heartbeat) => { + return this.db?.delete(config.queueName, heartbeat.id); + }), ); const userAgent = await this.getUserAgent(); @@ -214,7 +207,7 @@ class WakaTimeCore { console.error(data.error); return; } - if (response.status === 201) { + if (response.status === 202) { await Promise.all( (data.responses ?? []).map(async (resp, respNumber) => { if (resp[0].error) { @@ -222,7 +215,7 @@ class WakaTimeCore { console.error(resp[0].error); } else if (resp[1] === 201 && resp[0].data?.id) { await changeExtensionStatus('allGood'); - // await this.db?.delete('heartbeatQueue', resp[0].data.id); + // await this.db?.delete(config.queueName, resp[0].data.id); } else { if (resp[1] !== 400) { await this.putHeartbeatsBackInQueue(heartbeats.filter((h, i) => i === respNumber)); @@ -246,7 +239,7 @@ class WakaTimeCore { async putHeartbeatsBackInQueue(heartbeats: Heartbeat[]): Promise { await Promise.all( - heartbeats.map(async (heartbeat) => this.db?.add('heartbeatQueue', heartbeat)), + heartbeats.map(async (heartbeat) => this.db?.add(config.queueName, heartbeat)), ); } diff --git a/src/utils/settings.ts b/src/utils/settings.ts index ccef201..c4d8f5b 100644 --- a/src/utils/settings.ts +++ b/src/utils/settings.ts @@ -24,6 +24,7 @@ export const getSettings = async (): Promise => { blacklist: null, denyList: [], hostname: config.hostname, + loggingEnabled: config.loggingEnabled, loggingStyle: config.loggingStyle, loggingType: config.loggingType, socialMediaSites: config.socialMediaSites, diff --git a/src/utils/sites.ts b/src/utils/sites.ts index 7acc71c..efb6994 100644 --- a/src/utils/sites.ts +++ b/src/utils/sites.ts @@ -70,10 +70,7 @@ const GitHub: HeartbeatParser = (url: string) => { }; } - const body = document.getElementsByTagName('body').item(0); - if (!body) return; - - const repo = body + const repo = document .querySelector('meta[name=octolytics-dimension-repository_nwo]') ?.getAttribute('content'); if (repo?.split('/')[1] !== match[0]) return; diff --git a/src/wakatimeScript.ts b/src/wakatimeScript.ts index f8c92b7..caadf8d 100644 --- a/src/wakatimeScript.ts +++ b/src/wakatimeScript.ts @@ -40,6 +40,8 @@ chrome.runtime.onMessage.addListener( return; } + const heartbeat = site.parser(request.url); + sendResponse({ heartbeat: site.parser(request.url) }); } }, @@ -50,6 +52,10 @@ document.body.addEventListener('click', sendHeartbeat, true); document.body.addEventListener('keypress', sendHeartbeat, true); const checkIfInAMeeting = () => { + if (!window.location.href.startsWith('https://meet.google.com/')) { + return; + } + const isActiveMeeting = !!document.querySelector('[data-meeting-title]'); if (isActiveMeeting) { sendHeartbeat(); @@ -59,6 +65,4 @@ const checkIfInAMeeting = () => { }; // Google Meet -if (window.location.href.startsWith('https://meet.google.com/')) { - checkIfInAMeeting(); -} +checkIfInAMeeting();