Skip to content

Commit

Permalink
Merge pull request #264 from wakatime/get-project-name-only-from-vali…
Browse files Browse the repository at this point in the history
…d-url

Properly check if a url match is a project / repository
  • Loading branch information
alanhamlett authored Aug 26, 2024
2 parents 410ccc0 + 3d1038d commit 83a50a8
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 77 deletions.
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"idb": "^7.1.1",
"jquery": "^3.6.3",
"moment": "^2.29.4",
"node-html-parser": "^6.1.13",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.0.5",
Expand Down
28 changes: 22 additions & 6 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import browser from 'webextension-polyfill';
import WakaTimeCore from './core/WakaTimeCore';
import { PostHeartbeatMessage } from './types/heartbeats';
import { getHtmlContentByTabId } from './utils';

// Add a listener to resolve alarms
browser.alarms.onAlarm.addListener(async (alarm) => {
Expand All @@ -22,9 +23,10 @@ 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 () => {
browser.tabs.onActivated.addListener(async (activeInfo) => {
console.log('recording a heartbeat - active tab changed');
await WakaTimeCore.recordHeartbeat();
const html = await getHtmlContentByTabId(activeInfo.tabId);
await WakaTimeCore.recordHeartbeat(html);
});

/**
Expand All @@ -33,7 +35,17 @@ browser.tabs.onActivated.addListener(async () => {
browser.windows.onFocusChanged.addListener(async (windowId) => {
if (windowId != browser.windows.WINDOW_ID_NONE) {
console.log('recording a heartbeat - active window changed');
await WakaTimeCore.recordHeartbeat();
const tabs: browser.Tabs.Tab[] = await browser.tabs.query({
active: true,
currentWindow: true,
});

let html = '';
const tabId = tabs[0]?.id;
if (tabId) {
html = await getHtmlContentByTabId(tabId);
}
await WakaTimeCore.recordHeartbeat(html);
}
});

Expand All @@ -50,7 +62,8 @@ browser.tabs.onUpdated.addListener(async (tabId, changeInfo) => {
});
// If tab updated is the same as active tab
if (tabId == tabs[0]?.id) {
await WakaTimeCore.recordHeartbeat();
const html = await getHtmlContentByTabId(tabId);
await WakaTimeCore.recordHeartbeat(html);
}
}
});
Expand All @@ -63,9 +76,12 @@ self.addEventListener('activate', async () => {
await WakaTimeCore.createDB();
});

browser.runtime.onMessage.addListener(async (request: PostHeartbeatMessage) => {
browser.runtime.onMessage.addListener(async (request: PostHeartbeatMessage, sender) => {
if (request.recordHeartbeat === true) {
await WakaTimeCore.recordHeartbeat(request.projectDetails);
if (sender.tab?.id) {
const html = await getHtmlContentByTabId(sender.tab.id);
await WakaTimeCore.recordHeartbeat(html, request.projectDetails);
}
}
});

Expand Down
1 change: 1 addition & 0 deletions src/components/MainList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('MainList', () => {
totalTimeLoggedToday = '1/1/1999';
});
it('should render properly', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { container } = renderWithProviders(
<MainList loggingEnabled={loggingEnabled} totalTimeLoggedToday={totalTimeLoggedToday} />,
);
Expand Down
1 change: 1 addition & 0 deletions src/components/NavBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest.mock('webextension-polyfill', () => {

describe('NavBar', () => {
it('should render properly', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { container } = renderWithProviders(<NavBar />);
expect(container).toMatchInlineSnapshot(`
<div>
Expand Down
4 changes: 2 additions & 2 deletions src/core/WakaTimeCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class WakaTimeCore {
* Depending on various factors detects the current active tab URL or domain,
* and sends it to WakaTime for logging.
*/
async recordHeartbeat(payload: Record<string, unknown> = {}): Promise<void> {
async recordHeartbeat(html: string, payload: Record<string, unknown> = {}): Promise<void> {
const apiKey = await getApiKey();
if (!apiKey) {
return changeExtensionState('notLogging');
Expand Down Expand Up @@ -164,7 +164,7 @@ class WakaTimeCore {
}

// Checks dev websites
const project = generateProjectFromDevSites(url);
const project = generateProjectFromDevSites(url, html);

// Check if code reviewing
const codeReviewing = isCodeReviewing(url);
Expand Down
2 changes: 1 addition & 1 deletion src/manifests/chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"options_ui": {
"page": "options.html"
},
"permissions": ["alarms", "tabs", "storage", "idle"],
"permissions": ["<all_urls>", "alarms", "tabs", "storage", "idle", "activeTab", "scripting"],
"version": "3.0.22"
}
2 changes: 1 addition & 1 deletion src/manifests/edge.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"options_ui": {
"page": "options.html"
},
"permissions": ["alarms", "tabs", "storage", "idle"],
"permissions": ["<all_urls>", "alarms", "tabs", "storage", "idle", "activeTab", "scripting"],
"version": "3.0.22"
}
2 changes: 1 addition & 1 deletion src/manifests/firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
"chrome_style": false,
"page": "options.html"
},
"permissions": ["<all_urls>", "alarms", "tabs", "storage", "idle"],
"permissions": ["<all_urls>", "alarms", "tabs", "storage", "idle", "activeTab", "scripting"],
"version": "3.0.22"
}
10 changes: 7 additions & 3 deletions src/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { createRoot } from 'react-dom/client';
import Options from './components/Options';

/* This is a fix for Bootstrap requiring jQuery */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
global.jQuery = require('jquery');
require('bootstrap');

const container = document.getElementById('wakatime-options');
const root = createRoot(container!);

root.render(<Options />);
if (container) {
const root = createRoot(container);
root.render(<Options />);
}
18 changes: 10 additions & 8 deletions src/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import checkCurrentUser from './utils/checkCurrentUser';
import 'bootstrap/dist/js/bootstrap';

const container = document.getElementById('wakatime');
const root = createRoot(container!);
const store = createStore('WakaTime-Options');
checkCurrentUser(store)(30 * 1000);
if (container) {
const root = createRoot(container);
const store = createStore();
checkCurrentUser(store)(30 * 1000);

root.render(
<Provider store={store}>
<WakaTime />
</Provider>,
);
root.render(
<Provider store={store}>
<WakaTime />
</Provider>,
);
}
1 change: 1 addition & 0 deletions src/types/heartbeats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface SendHeartbeat {
}

export interface ProjectDetails {
[key: string]: string;
category: string;
editor: string;
language: string;
Expand Down
Loading

0 comments on commit 83a50a8

Please sign in to comment.