Skip to content

Commit

Permalink
Merge pull request #230 from wakatime/sebas-executing-service-worker
Browse files Browse the repository at this point in the history
Post message to service worker to sendHeartbeat
  • Loading branch information
alanhamlett authored Aug 29, 2023
2 parents 7d4fb6b + 3a430d6 commit 193692d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import browser from 'webextension-polyfill';
import WakaTimeCore from './core/WakaTimeCore';
import { PostHeartbeatMessage } from './types/heartbeats';

// Add a listener to resolve alarms
browser.alarms.onAlarm.addListener(async (alarm) => {
Expand All @@ -22,7 +23,7 @@ browser.alarms.create('heartbeatAlarm', { periodInMinutes: 2 });
* 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 ');
console.log('recording a heartbeat - active tab changed');
await WakaTimeCore.recordHeartbeat();
});

Expand Down Expand Up @@ -62,6 +63,12 @@ self.addEventListener('activate', async () => {
await WakaTimeCore.createDB();
});

browser.runtime.onMessage.addListener(async (request: PostHeartbeatMessage) => {
if (request.recordHeartbeat === true) {
await WakaTimeCore.recordHeartbeat(request.projectDetails);
}
});

/**
* "Persistent" service worker via bug exploit
* https://stackoverflow.com/questions/66618136/persistent-service-worker-in-chrome-extension
Expand Down
2 changes: 1 addition & 1 deletion src/manifests/chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
"page": "options.html"
},
"permissions": ["alarms", "tabs", "storage", "idle"],
"version": "3.0.18"
"version": "3.0.19"
}
2 changes: 1 addition & 1 deletion src/manifests/firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@
"page": "options.html"
},
"permissions": ["<all_urls>", "alarms", "tabs", "storage", "idle"],
"version": "3.0.18"
"version": "3.0.19"
}
12 changes: 12 additions & 0 deletions src/types/heartbeats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ export interface SendHeartbeat {
project: string | null;
url: string;
}

export interface ProjectDetails {
category: string;
editor: string;
language: string;
project: string;
}

export interface PostHeartbeatMessage {
projectDetails?: ProjectDetails;
recordHeartbeat: boolean;
}
5 changes: 1 addition & 4 deletions src/wakatimeScript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import WakaTimeCore from './core/WakaTimeCore';

const twoMinutes = 120000;

interface DesignProject {
Expand Down Expand Up @@ -55,9 +53,8 @@ const init = async () => {
const { hostname } = document.location;

const projectDetails = getParser[hostname]?.();

if (projectDetails) {
await WakaTimeCore.recordHeartbeat(projectDetails);
chrome.runtime.sendMessage({ projectDetails, recordHeartbeat: true });
}
};

Expand Down

0 comments on commit 193692d

Please sign in to comment.