11import type { BodyToSend , Event , ViewArguments } from "./types" ;
2+ import { detectBot } from "./utils/bot" ;
23import { createDebugModal } from "./utils/create-modal" ;
34import { defaultCollectorUrl } from "./utils/default-collector-url" ;
5+ import { extractHostName } from "./utils/extract-hostname" ;
46import { parseUtmParams } from "./utils/parse-utm-params" ;
57import { 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- / ^ l o c a l h o s t $ | ^ 1 2 7 ( \. [ 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+ ( / ^ l o c a l h o s t $ | ^ 1 2 7 ( \. [ 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