chore: add support for Content scripts (#182)

* chore: add support for Content scripts

* chore: run script bae on browser

* chore: content script supporting canva website

* chore: add 2 minutes debounce for onclik events

* chore: add debounce for keypress events
This commit is contained in:
Juan Sebastian velez Posada
2023-03-28 17:59:15 -05:00
committed by GitHub
parent c2ead5149d
commit 7d795f854f
14 changed files with 346 additions and 419 deletions

View File

@@ -8,6 +8,8 @@ import config from '../config/config';
import { SendHeartbeat } from '../types/heartbeats';
import { GrandTotal, SummariesPayload } from '../types/summaries';
import { ApiKeyPayload, AxiosUserResponse, User } from '../types/user';
import { generateProjectFromDevSites, IS_FIREFOX } from '../utils';
import { getApiKey } from '../utils/apiKey';
import changeExtensionState from '../utils/changeExtensionState';
import contains from '../utils/contains';
import getDomainFromUrl from '../utils/getDomainFromUrl';
@@ -106,20 +108,12 @@ class WakaTimeCore {
return userPayload.data.data;
}
async getApiKey(): Promise<string> {
const storage = await browser.storage.sync.get({
apiKey: config.apiKey,
});
const apiKey = storage.apiKey as string;
return apiKey;
}
/**
* Depending on various factors detects the current active tab URL or domain,
* and sends it to WakaTime for logging.
*/
async recordHeartbeat(): Promise<void> {
const apiKey = await this.getApiKey();
const apiKey = await getApiKey();
if (!apiKey) {
return changeExtensionState('notLogging');
}
@@ -151,7 +145,7 @@ class WakaTimeCore {
}
// Checks dev websites
const project = this.generateProjectFromDevSites(currentActiveTab.url as string);
const project = generateProjectFromDevSites(currentActiveTab.url as string);
if (items.loggingStyle == 'blacklist') {
if (!contains(currentActiveTab.url as string, items.blacklist as string)) {
@@ -303,17 +297,6 @@ class WakaTimeCore {
return items.loggingType;
}
generateProjectFromDevSites(url: string): string | null {
const githubUrls = ['https://github.com/', 'https://github.dev/'];
for (const githubUrl of githubUrls) {
if (url.startsWith(githubUrl)) {
const newUrl = url.replace(githubUrl, '');
return newUrl.split('/')[1] || null;
}
}
return null;
}
/**
* Creates payload for the heartbeat and returns it as JSON.
*
@@ -326,7 +309,7 @@ class WakaTimeCore {
preparePayload(heartbeat: SendHeartbeat, type: string): Record<string, unknown> {
let browserName = 'chrome';
let userAgent;
if (navigator.userAgent.includes('Firefox')) {
if (IS_FIREFOX) {
browserName = 'firefox';
userAgent = navigator.userAgent.match(/Firefox\/\S+/g)![0];
} else {
@@ -393,7 +376,7 @@ class WakaTimeCore {
* @param requests
*/
async sendCachedHeartbeatsRequest(): Promise<void> {
const apiKey = await this.getApiKey();
const apiKey = await getApiKey();
if (!apiKey) {
return changeExtensionState('notLogging');
}