Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post message to service worker to sendHeartbeat #230

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going forward, let's bump versions in different PRs from features and bugfixes.

}
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