chore: add hostname option to extension (#177)

This commit is contained in:
Juan Sebastian velez Posada
2023-03-15 14:14:21 -05:00
committed by GitHub
parent ae22ab5a9d
commit 1e9e233066
7 changed files with 55 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ interface State {
apiKey: string; apiKey: string;
blacklist: string; blacklist: string;
displayAlert: boolean; displayAlert: boolean;
hostname: string;
loading: boolean; loading: boolean;
loggingStyle: string; loggingStyle: string;
loggingType: string; loggingType: string;
@@ -26,6 +27,7 @@ export default function Options(): JSX.Element {
apiKey: '', apiKey: '',
blacklist: '', blacklist: '',
displayAlert: false, displayAlert: false,
hostname: '',
loading: false, loading: false,
loggingStyle: config.loggingStyle, loggingStyle: config.loggingStyle,
loggingType: config.loggingType, loggingType: config.loggingType,
@@ -41,6 +43,7 @@ export default function Options(): JSX.Element {
const items = await browser.storage.sync.get({ const items = await browser.storage.sync.get({
apiKey: config.apiKey, apiKey: config.apiKey,
blacklist: '', blacklist: '',
hostname: config.hostname,
loggingStyle: config.loggingStyle, loggingStyle: config.loggingStyle,
loggingType: config.loggingType, loggingType: config.loggingType,
socialMediaSites: config.socialMediaSites, socialMediaSites: config.socialMediaSites,
@@ -52,6 +55,7 @@ export default function Options(): JSX.Element {
...state, ...state,
apiKey: items.apiKey as string, apiKey: items.apiKey as string,
blacklist: items.blacklist as string, blacklist: items.blacklist as string,
hostname: items.hostname as string,
loggingStyle: items.loggingStyle as string, loggingStyle: items.loggingStyle as string,
loggingType: items.loggingType as string, loggingType: items.loggingType as string,
socialMediaSites: items.socialMediaSites as string, socialMediaSites: items.socialMediaSites as string,
@@ -79,6 +83,7 @@ export default function Options(): JSX.Element {
const apiKey = state.apiKey; const apiKey = state.apiKey;
const theme = state.theme; const theme = state.theme;
const hostname = state.hostname;
const loggingType = state.loggingType; const loggingType = state.loggingType;
const loggingStyle = state.loggingStyle; const loggingStyle = state.loggingStyle;
const trackSocialMedia = state.trackSocialMedia; const trackSocialMedia = state.trackSocialMedia;
@@ -91,6 +96,7 @@ export default function Options(): JSX.Element {
await browser.storage.sync.set({ await browser.storage.sync.set({
apiKey, apiKey,
blacklist, blacklist,
hostname,
loggingStyle, loggingStyle,
loggingType, loggingType,
socialMediaSites, socialMediaSites,
@@ -105,6 +111,7 @@ export default function Options(): JSX.Element {
apiKey, apiKey,
blacklist, blacklist,
displayAlert: true, displayAlert: true,
hostname,
loggingStyle, loggingStyle,
loggingType, loggingType,
socialMediaSites, socialMediaSites,
@@ -248,6 +255,24 @@ export default function Options(): JSX.Element {
</div> </div>
</div> </div>
<div className="form-group">
<label htmlFor="theme" className="col-lg-2 control-label">
Hostname
</label>
<div className="col-lg-10">
<input
type="text"
className="form-control"
value={state.hostname}
onChange={(e) => setState({ ...state, hostname: e.target.value })}
/>
<span className="help-block">
Optional name of local machine. By default &apos;Unknown Hostname&apos;.
</span>
</div>
</div>
<div className="form-group row"> <div className="form-group row">
<div className="col-lg-10 col-lg-offset-2 space-between align-items-center"> <div className="col-lg-10 col-lg-offset-2 space-between align-items-center">
<div <div

View File

@@ -44,6 +44,7 @@ describe('wakatime config', () => {
https://www.udemy.com/ https://www.udemy.com/
https://www.w3schools.com/", https://www.w3schools.com/",
"heartbeatApiUrl": "https://wakatime.com/api/v1/users/current/heartbeats", "heartbeatApiUrl": "https://wakatime.com/api/v1/users/current/heartbeats",
"hostname": "",
"loggingEnabled": true, "loggingEnabled": true,
"loggingStyle": "blacklist", "loggingStyle": "blacklist",
"loggingType": "domain", "loggingType": "domain",

View File

@@ -70,6 +70,8 @@ export interface Config {
* Url to which to send the heartbeat * Url to which to send the heartbeat
*/ */
heartbeatApiUrl: string; heartbeatApiUrl: string;
hostname: string;
/** /**
* Is logging enabled * Is logging enabled
*/ */
@@ -134,6 +136,8 @@ const config: Config = {
heartbeatApiUrl: heartbeatApiUrl:
process.env.HEART_BEAT_API_URL ?? 'https://wakatime.com/api/v1/users/current/heartbeats', process.env.HEART_BEAT_API_URL ?? 'https://wakatime.com/api/v1/users/current/heartbeats',
hostname: '',
loggingEnabled: true, loggingEnabled: true,
loggingStyle: 'blacklist', loggingStyle: 'blacklist',

View File

@@ -111,6 +111,7 @@ class WakaTimeCore {
} }
const items = await browser.storage.sync.get({ const items = await browser.storage.sync.get({
blacklist: '', blacklist: '',
hostname: config.hostname,
loggingEnabled: config.loggingEnabled, loggingEnabled: config.loggingEnabled,
loggingStyle: config.loggingStyle, loggingStyle: config.loggingStyle,
socialMediaSites: config.socialMediaSites, socialMediaSites: config.socialMediaSites,
@@ -142,6 +143,7 @@ class WakaTimeCore {
if (!contains(currentActiveTab.url as string, items.blacklist as string)) { if (!contains(currentActiveTab.url as string, items.blacklist as string)) {
await this.sendHeartbeat( await this.sendHeartbeat(
{ {
hostname: items.hostname as string,
project, project,
url: currentActiveTab.url as string, url: currentActiveTab.url as string,
}, },
@@ -160,7 +162,11 @@ class WakaTimeCore {
); );
if (heartbeat.url) { if (heartbeat.url) {
await this.sendHeartbeat( await this.sendHeartbeat(
{ ...heartbeat, project: heartbeat.project ?? project }, {
...heartbeat,
hostname: items.hostname as string,
project: heartbeat.project ?? project,
},
apiKey, apiKey,
); );
} else { } else {
@@ -260,12 +266,12 @@ class WakaTimeCore {
if (loggingType == 'domain') { if (loggingType == 'domain') {
heartbeat.url = getDomainFromUrl(heartbeat.url); heartbeat.url = getDomainFromUrl(heartbeat.url);
payload = this.preparePayload(heartbeat, 'domain'); payload = this.preparePayload(heartbeat, 'domain');
await this.sendPostRequestToApi(payload, apiKey); await this.sendPostRequestToApi(payload, apiKey, heartbeat.hostname);
} }
// Send entity in heartbeat // Send entity in heartbeat
else if (loggingType == 'url') { else if (loggingType == 'url') {
payload = this.preparePayload(heartbeat, 'url'); payload = this.preparePayload(heartbeat, 'url');
await this.sendPostRequestToApi(payload, apiKey); await this.sendPostRequestToApi(payload, apiKey, heartbeat.hostname);
} }
} }
@@ -333,13 +339,23 @@ class WakaTimeCore {
* @param method * @param method
* @returns {*} * @returns {*}
*/ */
async sendPostRequestToApi(payload: Record<string, unknown>, apiKey = ''): Promise<void> { async sendPostRequestToApi(
payload: Record<string, unknown>,
apiKey = '',
hostname = '',
): Promise<void> {
try { try {
const response = await fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, { const request: RequestInit = {
body: JSON.stringify(payload), body: JSON.stringify(payload),
credentials: 'omit', credentials: 'omit',
method: 'POST', method: 'POST',
}); };
if (hostname) {
request.headers = {
'X-Machine-Name': hostname,
};
}
const response = await fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, request);
await response.json(); await response.json();
} catch (err: unknown) { } catch (err: unknown) {
if (this.db) { if (this.db) {

View File

@@ -26,5 +26,5 @@
"page": "options.html" "page": "options.html"
}, },
"permissions": ["alarms", "tabs", "storage", "idle"], "permissions": ["alarms", "tabs", "storage", "idle"],
"version": "3.0.6" "version": "3.0.7"
} }

View File

@@ -39,5 +39,5 @@
"storage", "storage",
"idle" "idle"
], ],
"version": "3.0.6" "version": "3.0.7"
} }

View File

@@ -28,6 +28,7 @@ export interface Datum {
} }
export interface SendHeartbeat { export interface SendHeartbeat {
hostname: string;
project: string | null; project: string | null;
url: string; url: string;
} }