Skip to content

Commit 5174e45

Browse files
Merge branch 'master' into dev
2 parents c4ace00 + 78a2835 commit 5174e45

12 files changed

Lines changed: 1795 additions & 256 deletions

File tree

build/stonks.js

Lines changed: 303 additions & 57 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"web-vitals": "^4.1.1"
2222
},
2323
"devDependencies": {
24+
"@types/jsdom": "^27.0.0",
2425
"esbuild": "^0.23.0",
2526
"simple-git-hooks": "^2.13.1",
2627
"typescript": "^5.5.4"

pnpm-lock.yaml

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { BodyToSend, Event, ViewArguments } from "./types";
2+
import { detectBot } from "./utils/bot";
23
import { createDebugModal } from "./utils/create-modal";
34
import { defaultCollectorUrl } from "./utils/default-collector-url";
5+
import { extractHostName } from "./utils/extract-hostname";
46
import { parseUtmParams } from "./utils/parse-utm-params";
57
import { parseProps } from "./utils/props-parser";
68

@@ -21,33 +23,19 @@ import { parseProps } from "./utils/props-parser";
2123
};
2224

2325
const stonksScript = <HTMLScriptElement>document.currentScript; // ToDo
24-
const useHashRouting =
25-
stonksScript?.getAttribute("data-hash-routing") !== null; // ToDo
26-
const environment = {
27-
isLocalhost:
28-
/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(
29-
location.hostname,
30-
) || location.protocol === "file:",
31-
isHeadlessBrowser: !!(
32-
window._phantom ||
33-
window.__nightmare ||
34-
window.navigator.webdriver ||
35-
window.Cypress
36-
),
37-
};
26+
const useHashRouting = stonksScript?.getAttribute("data-hash-routing") !== null; // ToDo
27+
const isLocalEnviroment =
28+
(/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(location.hostname) &&
29+
// Protocol check prevents desktop app schemes 'tauri://localhost' being treated as localhost
30+
(location.protocol === "http:" || location.protocol === "https:")) ||
31+
location.protocol === "file:"
3832

39-
if (environment.isLocalhost) {
40-
const debugUrl = stonksScript?.getAttribute("data-debug");
33+
if (isLocalEnviroment) {
34+
const { hostname, devmode } = extractHostName(stonksScript, isLocalEnviroment);
4135

42-
console.log(
43-
`[onedollarstats]\nScript successfully connected! ${debugUrl ? `Tracking your localhost as ${debugUrl}` : "Debug domain not set"}`,
44-
);
36+
console.log(`[onedollarstats]\nScript successfully connected! ${hostname ? `Tracking your localhost as ${hostname}` : "Debug domain not set"}`);
4537

46-
if (debugUrl)
47-
createDebugModal(
48-
debugUrl,
49-
stonksScript?.getAttribute("data-url") || defaultCollectorUrl,
50-
);
38+
if (devmode && hostname) createDebugModal(hostname, stonksScript?.getAttribute("data-url") || defaultCollectorUrl);
5139
}
5240

5341
async function sendWithBeaconOrFetch(
@@ -79,32 +67,9 @@ import { parseProps } from "./utils/props-parser";
7967
const analyticsUrl =
8068
stonksScript?.getAttribute("data-url") || defaultCollectorUrl;
8169

82-
let urlToSend: URL = new URL(location.href);
83-
const debugAttribute = stonksScript.getAttribute("data-debug");
84-
const hostnameAttribute = stonksScript?.getAttribute("data-hostname");
85-
86-
let isDebug: boolean = false;
87-
if (debugAttribute) {
88-
try {
89-
const debugUrl = new URL(
90-
`https://${debugAttribute}${urlToSend.pathname}`,
91-
);
92-
if (urlToSend.hostname !== debugUrl.hostname) {
93-
isDebug = true;
94-
urlToSend = debugUrl;
95-
}
96-
} catch {
97-
return;
98-
}
99-
} else if (hostnameAttribute) {
100-
try {
101-
urlToSend = new URL(
102-
`https://${hostnameAttribute}${urlToSend.pathname}`,
103-
);
104-
} catch {
105-
// Invalid hostname, silently fall back to default (location.hostname)
106-
}
107-
}
70+
const { hostname, devmode } = extractHostName(stonksScript, isLocalEnviroment);
71+
72+
const urlToSend: URL = new URL(hostname ? `https://${hostname}${location.pathname}` : location.href);
10873

10974
urlToSend.search = "";
11075
if ("path" in data && data.path) {
@@ -137,17 +102,17 @@ import { parseProps } from "./utils/props-parser";
137102
t: data.type,
138103
h: useHashRouting, // ToDo: why we send hash routing
139104
r: referrer,
140-
p: data.props,
141-
},
105+
p: data.props
106+
}
142107
],
108+
debug: devmode
143109
};
144110

145111
if (data.utm && Object.keys(data.utm).length > 0) {
146112
body.qs = data.utm; // ToDo
147113
}
148114

149-
if (isDebug) {
150-
body.debug = isDebug;
115+
if (body.debug) {
151116
let logMessage = `[onedollarstats]\nEvent name: ${data.type}\nEvent collected from: ${cleanUrl}`;
152117
if (data.props && Object.keys(data.props).length > 0)
153118
logMessage += `\nProps: ${JSON.stringify(data.props, null, 2)}`;
@@ -395,11 +360,15 @@ import { parseProps } from "./utils/props-parser";
395360
}
396361

397362
function shouldBlockEvent(): boolean {
398-
if (environment.isLocalhost && !stonksScript?.getAttribute("data-debug")) {
363+
const { hostname, devmode } = extractHostName(stonksScript, isLocalEnviroment);
364+
365+
if (isLocalEnviroment && (!devmode || !hostname)) {
399366
return true;
400367
}
401368

402-
if (environment.isHeadlessBrowser) {
369+
const {isBot, botKind} = detectBot()
370+
371+
if (isBot && botKind !== "human") {
403372
return true;
404373
} // ToDo: create env var to allow headless browsers, need it for tests, so put to env var.
405374

0 commit comments

Comments
 (0)