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

Add hostname option to extension #177

Merged
merged 1 commit into from
Mar 15, 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
25 changes: 25 additions & 0 deletions src/components/Options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface State {
apiKey: string;
blacklist: string;
displayAlert: boolean;
hostname: string;
loading: boolean;
loggingStyle: string;
loggingType: string;
Expand All @@ -26,6 +27,7 @@ export default function Options(): JSX.Element {
apiKey: '',
blacklist: '',
displayAlert: false,
hostname: '',
loading: false,
loggingStyle: config.loggingStyle,
loggingType: config.loggingType,
Expand All @@ -41,6 +43,7 @@ export default function Options(): JSX.Element {
const items = await browser.storage.sync.get({
apiKey: config.apiKey,
blacklist: '',
hostname: config.hostname,
loggingStyle: config.loggingStyle,
loggingType: config.loggingType,
socialMediaSites: config.socialMediaSites,
Expand All @@ -52,6 +55,7 @@ export default function Options(): JSX.Element {
...state,
apiKey: items.apiKey as string,
blacklist: items.blacklist as string,
hostname: items.hostname as string,
loggingStyle: items.loggingStyle as string,
loggingType: items.loggingType as string,
socialMediaSites: items.socialMediaSites as string,
Expand Down Expand Up @@ -79,6 +83,7 @@ export default function Options(): JSX.Element {

const apiKey = state.apiKey;
const theme = state.theme;
const hostname = state.hostname;
const loggingType = state.loggingType;
const loggingStyle = state.loggingStyle;
const trackSocialMedia = state.trackSocialMedia;
Expand All @@ -91,6 +96,7 @@ export default function Options(): JSX.Element {
await browser.storage.sync.set({
apiKey,
blacklist,
hostname,
loggingStyle,
loggingType,
socialMediaSites,
Expand All @@ -105,6 +111,7 @@ export default function Options(): JSX.Element {
apiKey,
blacklist,
displayAlert: true,
hostname,
loggingStyle,
loggingType,
socialMediaSites,
Expand Down Expand Up @@ -248,6 +255,24 @@ export default function Options(): JSX.Element {
</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="col-lg-10 col-lg-offset-2 space-between align-items-center">
<div
Expand Down
1 change: 1 addition & 0 deletions src/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('wakatime config', () => {
https://www.udemy.com/
https://www.w3schools.com/",
"heartbeatApiUrl": "https://wakatime.com/api/v1/users/current/heartbeats",
"hostname": "",
"loggingEnabled": true,
"loggingStyle": "blacklist",
"loggingType": "domain",
Expand Down
4 changes: 4 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export interface Config {
* Url to which to send the heartbeat
*/
heartbeatApiUrl: string;

hostname: string;
/**
* Is logging enabled
*/
Expand Down Expand Up @@ -134,6 +136,8 @@ const config: Config = {
heartbeatApiUrl:
process.env.HEART_BEAT_API_URL ?? 'https://wakatime.com/api/v1/users/current/heartbeats',

hostname: '',

loggingEnabled: true,

loggingStyle: 'blacklist',
Expand Down
28 changes: 22 additions & 6 deletions src/core/WakaTimeCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class WakaTimeCore {
}
const items = await browser.storage.sync.get({
blacklist: '',
hostname: config.hostname,
loggingEnabled: config.loggingEnabled,
loggingStyle: config.loggingStyle,
socialMediaSites: config.socialMediaSites,
Expand Down Expand Up @@ -142,6 +143,7 @@ class WakaTimeCore {
if (!contains(currentActiveTab.url as string, items.blacklist as string)) {
await this.sendHeartbeat(
{
hostname: items.hostname as string,
project,
url: currentActiveTab.url as string,
},
Expand All @@ -160,7 +162,11 @@ class WakaTimeCore {
);
if (heartbeat.url) {
await this.sendHeartbeat(
{ ...heartbeat, project: heartbeat.project ?? project },
{
...heartbeat,
hostname: items.hostname as string,
project: heartbeat.project ?? project,
},
apiKey,
);
} else {
Expand Down Expand Up @@ -260,12 +266,12 @@ class WakaTimeCore {
if (loggingType == 'domain') {
heartbeat.url = getDomainFromUrl(heartbeat.url);
payload = this.preparePayload(heartbeat, 'domain');
await this.sendPostRequestToApi(payload, apiKey);
await this.sendPostRequestToApi(payload, apiKey, heartbeat.hostname);
}
// Send entity in heartbeat
else if (loggingType == 'url') {
payload = this.preparePayload(heartbeat, 'url');
await this.sendPostRequestToApi(payload, apiKey);
await this.sendPostRequestToApi(payload, apiKey, heartbeat.hostname);
}
}

Expand Down Expand Up @@ -333,13 +339,23 @@ class WakaTimeCore {
* @param method
* @returns {*}
*/
async sendPostRequestToApi(payload: Record<string, unknown>, apiKey = ''): Promise<void> {
async sendPostRequestToApi(
payload: Record<string, unknown>,
apiKey = '',
hostname = '',
): Promise<void> {
try {
const response = await fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, {
const request: RequestInit = {
body: JSON.stringify(payload),
credentials: 'omit',
method: 'POST',
});
};
if (hostname) {
request.headers = {
'X-Machine-Name': hostname,
};
}
const response = await fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, request);
await response.json();
} catch (err: unknown) {
if (this.db) {
Expand Down
2 changes: 1 addition & 1 deletion src/manifests/chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
"page": "options.html"
},
"permissions": ["alarms", "tabs", "storage", "idle"],
"version": "3.0.6"
"version": "3.0.7"
}
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 @@
"storage",
"idle"
],
"version": "3.0.6"
"version": "3.0.7"
}
1 change: 1 addition & 0 deletions src/types/heartbeats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface Datum {
}

export interface SendHeartbeat {
hostname: string;
project: string | null;
url: string;
}