@ohbug/browser 2.1.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1 +1,706 @@
1
- import{Client as lt}from"@ohbug/core";import{getGlobal as mt}from"@ohbug/utils";var N=()=>{let e={};if(navigator){let{language:t,userAgent:r,connection:o}=navigator;e.language=t,e.userAgent=r,o&&(e.connection={downlink:o.downlink,effectiveType:o.effectiveType,rtt:o.rtt})}if(document){let{title:t,referrer:r}=document;e.title=t,e.referrer=r}if(window.location){let{href:t}=window.location;e.url=t}return e};var v="2.1.1";import{defineExtension as ct}from"@ohbug/core";import{getGlobal as ot}from"@ohbug/utils";import{getGlobal as ve,getOhbugObject as De,parseUrl as D,replace as w}from"@ohbug/utils";var a=ve(),R;function C(e,t){var l;let{client:r}=De(),o=D((l=a==null?void 0:a.location)==null?void 0:l.href),n=D(e),s=D(t);n.path||(n=o),R=t;let i=e,c=t;o.protocol===s.protocol&&o.host===s.host&&(c=s.relative),o.protocol===n.protocol&&o.host===n.host&&(i=n.relative),i!==c&&r.addAction(`navigation to ${t}`,{from:e,to:t},"navigation")}function G(e){return function(r,o,n){return n&&C(R,String(n)),e.apply(this,[r,o,n])}}var q,M,d={pushState:(q=a==null?void 0:a.history)==null?void 0:q.pushState,replaceState:(M=a==null?void 0:a.history)==null?void 0:M.replaceState,onpopstate:a==null?void 0:a.onpopstate};function we(){d.pushState=w(a==null?void 0:a.history,"pushState",G),d.replaceState=w(a==null?void 0:a.history,"replaceState",G),d.onpopstate=w(a,"onpopstate",e=>function(...r){var n;let o=(n=a==null?void 0:a.location)==null?void 0:n.href;return C(R,o),e==null?void 0:e.apply(this,r)})}function P(e){let{oldURL:t,newURL:r}=e;C(t,r)}function X(){var e;we(),(e=a==null?void 0:a.addEventListener)==null||e.call(a,"hashchange",P,!0)}function K(){var e;a.history.pushState=d.pushState,a.history.replaceState=d.replaceState,a.onpopstate=d.onpopstate,(e=a==null?void 0:a.removeEventListener)==null||e.call(a,"hashchange",P,!0)}import{getGlobal as Re,getOhbugObject as Ce,getSelector as ke}from"@ohbug/utils";var h=Re();function J(e){if(e.target){let{client:t}=Ce(),{tagName:r,id:o,className:n,name:s,src:i,outerHTML:c,nodeType:l}=e.target,y=ke(e);t.addAction("click node",{tagName:r,id:o,className:n,name:s,src:i,outerHTML:c,nodeType:l,selector:y},"click")}}function $(){var e,t;(t=(e=h==null?void 0:h.document)==null?void 0:e.addEventListener)==null||t.call(e,"click",J)}function I(){var e,t;(t=(e=h==null?void 0:h.document)==null?void 0:e.removeEventListener)==null||t.call(e,"click",J)}import{getOhbugObject as je,replace as He}from"@ohbug/utils";var k=["log","info","warn","error"],z=Object.keys(k).reduce((e,t)=>({...e,[t]:console[t]}),{log:null,info:null,warn:null,error:null});function Q(){let{client:e}=je();k.forEach(t=>{z[t]=He(console,t,r=>function(...n){return n.some(i=>typeof i=="string"&&i.includes("Ohbug"))||e.addAction(`console.${t}`,n,"console"),r.apply(this,n)})})}function j(){console&&k.forEach(e=>{console[e]=z[e]})}import{getGlobal as Ke,getOhbugObject as Je,replace as V}from"@ohbug/utils";import{EventTypes as $e}from"@ohbug/core";import{getOhbugObject as Te}from"@ohbug/utils";import{EventTypes as We}from"@ohbug/core";function H(e){let{message:t,filename:r,lineno:o,colno:n,error:{stack:s,name:i}}=e,c={name:i,message:t,filename:r,lineno:o,colno:n,stack:s},{client:l}=Te(),y=l.createEvent({category:"error",type:We.UNCAUGHT_ERROR,detail:c});l.notify(y)}import{getOhbugObject as _e,getSelector as Se}from"@ohbug/utils";import{EventTypes as Ue}from"@ohbug/core";function T(e){let t=e.target||e.srcElement,{outerHTML:r}=t,o=Se(e),n={outerHTML:r,src:t&&t.src,tagName:t&&t.tagName,id:t&&t.id,className:t&&t.className,name:t&&t.name,nodeType:t&&t.nodeType,selector:o},{client:s}=_e(),i=s.createEvent({category:"error",type:Ue.RESOURCE_ERROR,detail:n});s.notify(i)}import{getOhbugObject as Ae}from"@ohbug/utils";import{EventTypes as Le}from"@ohbug/core";function W(e){let t={message:e.reason.message||e.reason,stack:e.reason.stack},{client:r}=Ae(),o=r.createEvent({category:"error",type:Le.UNHANDLEDREJECTION_ERROR,detail:t});r.notify(o)}import{getOhbugObject as Fe}from"@ohbug/utils";import{EventTypes as Be}from"@ohbug/core";function E(e){let t=e.message?e:{message:e},{client:r}=Fe(),o=r.createEvent({category:"error",type:Be.UNKNOWN_ERROR,detail:t});r.notify(o)}import{getOhbugObject as Ne}from"@ohbug/utils";import{EventTypes as Ge}from"@ohbug/core";function _(e){let{client:t}=Ne(),r=t.createEvent({category:"error",type:Ge.AJAX_ERROR,detail:e});t.notify(r)}import{getOhbugObject as qe}from"@ohbug/utils";import{EventTypes as Me}from"@ohbug/core";function S(e){let{client:t}=qe(),r=t.createEvent({category:"error",type:Me.FETCH_ERROR,detail:e});t.notify(r)}import{getOhbugObject as Pe}from"@ohbug/utils";import{EventTypes as Xe}from"@ohbug/core";function U(e){let{client:t}=Pe(),r=t.createEvent({category:"error",type:Xe.WEBSOCKET_ERROR,detail:e});t.notify(r)}function O(e){try{let{type:t}=e;if(t==="error"){let r=e,{message:o,error:n}=r,s=e.target||e.srcElement;o&&n?H(r):s&&T(r)}else t==="unhandledrejection"?W(e):E(e)}catch(t){E(t)}}import{EventTypes as A}from"@ohbug/core";function m(e,t){try{switch(e){case A.AJAX_ERROR:_(t);break;case A.FETCH_ERROR:S(t);break;case A.WEBSOCKET_ERROR:U(t);break;default:break}}catch(r){E(r)}}var Ie=Ke(),L="XMLHttpRequest"in Ie,g=L?{open:XMLHttpRequest.prototype.open,send:XMLHttpRequest.prototype.send}:{};function Y(){if(!L)return;let{client:e}=Je(),t={method:"",url:""},r=XMLHttpRequest==null?void 0:XMLHttpRequest.prototype;g.open=V(r,"open",o=>function(...s){let[i,c]=s;return t.method=i,t.url=c,o.apply(this,s)}),g.send=V(r,"send",o=>function(...s){return this.addEventListener("readystatechange",function(){if(this.readyState===4&&t.url!==e.__config.endpoint){let c={req:{url:t.url,method:t.method,data:s[0]||{}},res:{status:this.status,statusText:this.statusText,response:this.response}};e.addAction("ajax",c,"ajax"),(!this.status||this.status>=400)&&m($e.AJAX_ERROR,c)}}),o.apply(this,s)})}function Z(){if(L&&g.open&&g.send){let e=XMLHttpRequest==null?void 0:XMLHttpRequest.prototype;e.open=g.open,e.send=g.send}}import{getGlobal as ze,getOhbugObject as Qe,replace as Ve}from"@ohbug/utils";import{EventTypes as ee}from"@ohbug/core";var b=ze(),B="fetch"in b,F=B?b.fetch:null;function te(){if(!B)return;let{client:e}=Qe();F=Ve(b,"fetch",t=>function(...o){return t.apply(this,o).then(n=>{let[s,i]=o,c={req:{url:s,method:i&&i.method,data:i&&i.body||{}},res:{status:n.status,statusText:n.statusText}};return e.addAction("fetch",c,"fetch"),(!n.status||n.status>=400)&&m(ee.FETCH_ERROR,c),n},n=>{let[s,i]=o,c={req:{url:s,method:i&&i.method,data:i&&i.body||{}},res:{status:400,statusText:"unknownError"}};throw m(ee.FETCH_ERROR,c),n})})}function re(){B&&F&&(b.fetch=F)}import{getGlobal as Ye}from"@ohbug/utils";import{EventTypes as Ze}from"@ohbug/core";var et=Ye();function oe(){if(!("WebSocket"in et))return;let e=WebSocket==null?void 0:WebSocket.prototype,t=Object.getOwnPropertyDescriptor(e,"onerror");Object.defineProperty(e,"onerror",{set(){var n;let r=arguments,o=r[0];(n=t==null?void 0:t.set)==null||n.call(this,function(i){let{target:{url:c,readyState:l,protocol:y,extensions:ge,binaryType:ye,bufferedAmount:xe},timeStamp:Oe}=i,be={url:c,timeStamp:Oe,readyState:l,protocol:y,extensions:ge,binaryType:ye,bufferedAmount:xe};m(Ze.WEBSOCKET_ERROR,be),o.apply(this,r)})}})}import{getGlobal as tt}from"@ohbug/utils";var p=tt();function ne(e){O(e)}function ie(){var e;(e=p==null?void 0:p.addEventListener)==null||e.call(p,"error",ne,!0)}function ae(){var e;(e=p==null?void 0:p.removeEventListener)==null||e.call(p,"error",ne,!0)}import{getGlobal as rt}from"@ohbug/utils";var u=rt();function se(e){O(e)}function ce(){var e;(e=u==null?void 0:u.addEventListener)==null||e.call(u,"unhandledrejection",se,!0)}function pe(){var e;(e=u==null?void 0:u.removeEventListener)==null||e.call(u,"unhandledrejection",se,!0)}function nt(){X(),$()}function ue(){K(),I()}function it(){Y(),te(),oe()}function le(){Z(),re()}function at(){!ot().addEventListener||(ie(),ce())}function me(){ae(),pe()}function fe(){at(),it(),nt(),Q()}import{getGlobal as st}from"@ohbug/utils";var x=st();function de(){var e;(e=x==null?void 0:x.addEventListener)==null||e.call(x,"unload",()=>{me(),le(),ue(),j()},!0)}var he=ct({name:"OhbugBrowser",onSetup:()=>{fe(),de()}});import{getOhbugObject as pt}from"@ohbug/utils";var ut=()=>{let e=new WeakSet;return(t,r)=>{if(typeof r=="object"&&r!==null){if(e.has(r))return;e.add(r)}return r}};function Ee(e){let{client:t}=pt(),r=t.__config.endpoint;return new Promise((o,n)=>{let s=JSON.stringify(e,ut());if(navigator.sendBeacon){let i=navigator.sendBeacon(r,s);o(i)}else{let i=new XMLHttpRequest;i.onreadystatechange=()=>{if(i.readyState===XMLHttpRequest.DONE){if(i.status>=200&&i.status<300)return o(i.response);n(i)}},i.open("POST",r),i.setRequestHeader("Content-Type","application/json;charset=UTF-8"),i.send(s)}})}function ft(e){let t=mt(),r={platform:"ohbug-browser",version:v},o=new lt({sdk:r,config:e,device:N,notifier:Ee});return t.__OHBUG__={client:o},o.use(he),o.__logger.info(`%c @ohbug/browser %c Detected Ohbug v${v} %c`,"background:#333; padding: 2px 1px; color: #FFF","background:#FF6F61; padding: 2px 1px; color: #FFF","background:transparent"),o}var f={__client:null,setup(e){var t;return f.__client?((t=f.__client.__logger)==null||t.warn("init() has been called. Ignored."),f.__client):(f.__client=ft(e),f.__client)}};var $r=f;export{$r as default};
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+
21
+ // src/client.ts
22
+ import { Client } from "@ohbug/core";
23
+ import { getGlobal as getGlobal10 } from "@ohbug/utils";
24
+
25
+ // src/device.ts
26
+ var device = () => {
27
+ const ohbugDevice = {};
28
+ if (navigator) {
29
+ const {
30
+ language,
31
+ userAgent,
32
+ connection
33
+ } = navigator;
34
+ ohbugDevice.language = language;
35
+ ohbugDevice.userAgent = userAgent;
36
+ if (connection) {
37
+ ohbugDevice.connection = {
38
+ downlink: connection.downlink,
39
+ effectiveType: connection.effectiveType,
40
+ rtt: connection.rtt
41
+ };
42
+ }
43
+ }
44
+ if (document) {
45
+ const { title, referrer } = document;
46
+ ohbugDevice.title = title;
47
+ ohbugDevice.referrer = referrer;
48
+ }
49
+ if (window.location) {
50
+ const { href: url } = window.location;
51
+ ohbugDevice.url = url;
52
+ }
53
+ return ohbugDevice;
54
+ };
55
+
56
+ // src/version.ts
57
+ var version = "2.3.0";
58
+
59
+ // src/extension.ts
60
+ import { defineExtension } from "@ohbug/core";
61
+
62
+ // src/capture/index.ts
63
+ import { getGlobal as getGlobal8 } from "@ohbug/utils";
64
+
65
+ // src/capture/action/captureUrlChange.ts
66
+ import { getGlobal, getOhbugObject, parseUrl, replace } from "@ohbug/utils";
67
+ var global = getGlobal();
68
+ var lastHref;
69
+ function handleUrlChange(from, to) {
70
+ var _a2;
71
+ const { client } = getOhbugObject();
72
+ const parsedHref = parseUrl((_a2 = global == null ? void 0 : global.location) == null ? void 0 : _a2.href);
73
+ let parsedFrom = parseUrl(from);
74
+ const parsedTo = parseUrl(to);
75
+ if (!parsedFrom.path) {
76
+ parsedFrom = parsedHref;
77
+ }
78
+ lastHref = to;
79
+ let targetFrom = from;
80
+ let targetTo = to;
81
+ if (parsedHref.protocol === parsedTo.protocol && parsedHref.host === parsedTo.host) {
82
+ targetTo = parsedTo.relative;
83
+ }
84
+ if (parsedHref.protocol === parsedFrom.protocol && parsedHref.host === parsedFrom.host) {
85
+ targetFrom = parsedFrom.relative;
86
+ }
87
+ if (targetFrom === targetTo)
88
+ return;
89
+ client.addAction(
90
+ `navigation to ${to}`,
91
+ {
92
+ from,
93
+ to
94
+ },
95
+ "navigation"
96
+ );
97
+ }
98
+ function historyReplacement(original) {
99
+ return function call(data, title, url) {
100
+ if (url) {
101
+ handleUrlChange(lastHref, String(url));
102
+ }
103
+ return original.apply(this, [data, title, url]);
104
+ };
105
+ }
106
+ var _a, _b;
107
+ var historyOriginal = {
108
+ pushState: (_a = global == null ? void 0 : global.history) == null ? void 0 : _a.pushState,
109
+ replaceState: (_b = global == null ? void 0 : global.history) == null ? void 0 : _b.replaceState,
110
+ onpopstate: global == null ? void 0 : global.onpopstate
111
+ };
112
+ function historyListener() {
113
+ historyOriginal.pushState = replace(
114
+ global == null ? void 0 : global.history,
115
+ "pushState",
116
+ historyReplacement
117
+ );
118
+ historyOriginal.replaceState = replace(
119
+ global == null ? void 0 : global.history,
120
+ "replaceState",
121
+ historyReplacement
122
+ );
123
+ historyOriginal.onpopstate = replace(
124
+ global,
125
+ "onpopstate",
126
+ (origin) => function call(...args) {
127
+ var _a2;
128
+ const current = (_a2 = global == null ? void 0 : global.location) == null ? void 0 : _a2.href;
129
+ handleUrlChange(lastHref, current);
130
+ return origin == null ? void 0 : origin.apply(this, args);
131
+ }
132
+ );
133
+ }
134
+ function hashListener(e) {
135
+ const { oldURL, newURL } = e;
136
+ handleUrlChange(oldURL, newURL);
137
+ }
138
+ function captureUrlChange() {
139
+ var _a2;
140
+ historyListener();
141
+ (_a2 = global == null ? void 0 : global.addEventListener) == null ? void 0 : _a2.call(global, "hashchange", hashListener, true);
142
+ }
143
+ function removeCaptureUrlChange() {
144
+ var _a2;
145
+ global.history.pushState = historyOriginal.pushState;
146
+ global.history.replaceState = historyOriginal.replaceState;
147
+ global.onpopstate = historyOriginal.onpopstate;
148
+ (_a2 = global == null ? void 0 : global.removeEventListener) == null ? void 0 : _a2.call(global, "hashchange", hashListener, true);
149
+ }
150
+
151
+ // src/capture/action/captureClick.ts
152
+ import { getGlobal as getGlobal2, getOhbugObject as getOhbugObject2, getSelector } from "@ohbug/utils";
153
+ var global2 = getGlobal2();
154
+ function listener(e) {
155
+ if (e.target) {
156
+ const { client } = getOhbugObject2();
157
+ const { tagName, id, className, name, src, nodeType } = e.target;
158
+ if (tagName.toUpperCase() !== "HTML" && tagName.toUpperCase() !== "BODY") {
159
+ const selector = getSelector(e);
160
+ client.addAction(
161
+ "click node",
162
+ {
163
+ tagName,
164
+ id: id || void 0,
165
+ className: className || void 0,
166
+ name: name || void 0,
167
+ src: src || void 0,
168
+ nodeType: nodeType || void 0,
169
+ selector: selector || void 0
170
+ },
171
+ "click"
172
+ );
173
+ }
174
+ }
175
+ }
176
+ function captureClick() {
177
+ var _a2, _b2;
178
+ (_b2 = (_a2 = global2 == null ? void 0 : global2.document) == null ? void 0 : _a2.addEventListener) == null ? void 0 : _b2.call(_a2, "click", listener);
179
+ }
180
+ function removeCaptureClick() {
181
+ var _a2, _b2;
182
+ (_b2 = (_a2 = global2 == null ? void 0 : global2.document) == null ? void 0 : _a2.removeEventListener) == null ? void 0 : _b2.call(_a2, "click", listener);
183
+ }
184
+
185
+ // src/capture/console/captureConsole.ts
186
+ import { getOhbugObject as getOhbugObject3, replace as replace2 } from "@ohbug/utils";
187
+ var levels = ["log", "info", "warn", "error"];
188
+ var consoleOriginal = Object.keys(levels).reduce(
189
+ (acc, cur) => __spreadProps(__spreadValues({}, acc), {
190
+ [cur]: console[cur]
191
+ }),
192
+ {
193
+ log: null,
194
+ info: null,
195
+ warn: null,
196
+ error: null
197
+ }
198
+ );
199
+ function captureConsole() {
200
+ const { client } = getOhbugObject3();
201
+ levels.forEach((level) => {
202
+ consoleOriginal[level] = replace2(
203
+ console,
204
+ level,
205
+ (origin) => function call(...args) {
206
+ const isOhbugConsole = args.some((arg) => typeof arg === "string" && arg.includes("Ohbug"));
207
+ if (!isOhbugConsole) {
208
+ client.addAction(`console.${level}`, args, "console");
209
+ }
210
+ return origin.apply(this, args);
211
+ }
212
+ );
213
+ });
214
+ }
215
+ function removeCaptureConsole() {
216
+ if (console) {
217
+ levels.forEach((level) => {
218
+ console[level] = consoleOriginal[level];
219
+ });
220
+ }
221
+ }
222
+
223
+ // src/capture/network/captureAjaxError.ts
224
+ import { getGlobal as getGlobal3, getOhbugObject as getOhbugObject11, replace as replace3 } from "@ohbug/utils";
225
+ import { EventTypes as EventTypes9 } from "@ohbug/core";
226
+
227
+ // src/handle/uncaughtErrorHandler.ts
228
+ import { getOhbugObject as getOhbugObject4 } from "@ohbug/utils";
229
+ import { EventTypes } from "@ohbug/core";
230
+ import ErrorStackParser from "error-stack-parser";
231
+ function uncaughtErrorHandler(error) {
232
+ var _a2, _b2, _c;
233
+ const [stackFrame] = ErrorStackParser.parse(error.error);
234
+ const detail = {
235
+ name: (_a2 = error == null ? void 0 : error.error) == null ? void 0 : _a2.name,
236
+ message: (error == null ? void 0 : error.message) || ((_b2 = error == null ? void 0 : error.error) == null ? void 0 : _b2.message),
237
+ filename: (error == null ? void 0 : error.filename) || (stackFrame == null ? void 0 : stackFrame.fileName),
238
+ lineno: (error == null ? void 0 : error.lineno) || (stackFrame == null ? void 0 : stackFrame.lineNumber),
239
+ colno: (error == null ? void 0 : error.colno) || (stackFrame == null ? void 0 : stackFrame.columnNumber),
240
+ stack: (_c = error == null ? void 0 : error.error) == null ? void 0 : _c.stack
241
+ };
242
+ const { client } = getOhbugObject4();
243
+ const event = client.createEvent({
244
+ category: "error",
245
+ type: EventTypes.UNCAUGHT_ERROR,
246
+ detail
247
+ });
248
+ client.notify(event);
249
+ }
250
+
251
+ // src/handle/resourceErrorHandler.ts
252
+ import { getOhbugObject as getOhbugObject5, getSelector as getSelector2 } from "@ohbug/utils";
253
+ import { EventTypes as EventTypes2 } from "@ohbug/core";
254
+ function resourceErrorHandler(error) {
255
+ const target = error.target || error.srcElement;
256
+ const selector = getSelector2(error);
257
+ const detail = {
258
+ src: target && target.src,
259
+ tagName: target && target.tagName,
260
+ id: target && target.id,
261
+ className: target && target.className,
262
+ name: target && target.name,
263
+ nodeType: target && target.nodeType,
264
+ selector
265
+ };
266
+ const { client } = getOhbugObject5();
267
+ const event = client.createEvent({
268
+ category: "error",
269
+ type: EventTypes2.RESOURCE_ERROR,
270
+ detail
271
+ });
272
+ client.notify(event);
273
+ }
274
+
275
+ // src/handle/unhandledrejectionErrorHandler.ts
276
+ import { getOhbugObject as getOhbugObject6 } from "@ohbug/utils";
277
+ import { EventTypes as EventTypes3 } from "@ohbug/core";
278
+ import ErrorStackParser2 from "error-stack-parser";
279
+ function unhandledrejectionErrorHandler(error) {
280
+ var _a2, _b2, _c;
281
+ const [stackFrame] = ErrorStackParser2.parse(error.reason);
282
+ const detail = {
283
+ name: (_a2 = error == null ? void 0 : error.reason) == null ? void 0 : _a2.name,
284
+ message: ((_b2 = error == null ? void 0 : error.reason) == null ? void 0 : _b2.message) || (error == null ? void 0 : error.reason),
285
+ filename: stackFrame == null ? void 0 : stackFrame.fileName,
286
+ lineno: stackFrame == null ? void 0 : stackFrame.lineNumber,
287
+ colno: stackFrame == null ? void 0 : stackFrame.columnNumber,
288
+ stack: (_c = error == null ? void 0 : error.reason) == null ? void 0 : _c.stack
289
+ };
290
+ const { client } = getOhbugObject6();
291
+ const event = client.createEvent({
292
+ category: "error",
293
+ type: EventTypes3.UNHANDLEDREJECTION_ERROR,
294
+ detail
295
+ });
296
+ client.notify(event);
297
+ }
298
+
299
+ // src/handle/unknownErrorHandler.ts
300
+ import { getOhbugObject as getOhbugObject7 } from "@ohbug/utils";
301
+ import { EventTypes as EventTypes4 } from "@ohbug/core";
302
+ function unknownErrorHandler(error) {
303
+ const detail = error.message ? error : { message: error };
304
+ const { client } = getOhbugObject7();
305
+ const event = client.createEvent({
306
+ category: "error",
307
+ type: EventTypes4.UNKNOWN_ERROR,
308
+ detail
309
+ });
310
+ client.notify(event);
311
+ }
312
+
313
+ // src/handle/ajaxErrorHandler.ts
314
+ import { getOhbugObject as getOhbugObject8 } from "@ohbug/utils";
315
+ import { EventTypes as EventTypes5 } from "@ohbug/core";
316
+ function ajaxErrorHandler(detail) {
317
+ const { client } = getOhbugObject8();
318
+ const event = client.createEvent({
319
+ category: "error",
320
+ type: EventTypes5.AJAX_ERROR,
321
+ detail
322
+ });
323
+ client.notify(event);
324
+ }
325
+
326
+ // src/handle/fetchErrorHandler.ts
327
+ import { getOhbugObject as getOhbugObject9 } from "@ohbug/utils";
328
+ import { EventTypes as EventTypes6 } from "@ohbug/core";
329
+ function fetchErrorHandler(detail) {
330
+ const { client } = getOhbugObject9();
331
+ const event = client.createEvent({
332
+ category: "error",
333
+ type: EventTypes6.FETCH_ERROR,
334
+ detail
335
+ });
336
+ client.notify(event);
337
+ }
338
+
339
+ // src/handle/websocketErrorHandler.ts
340
+ import { getOhbugObject as getOhbugObject10 } from "@ohbug/utils";
341
+ import { EventTypes as EventTypes7 } from "@ohbug/core";
342
+ function websocketErrorHandler(detail) {
343
+ const { client } = getOhbugObject10();
344
+ const event = client.createEvent({
345
+ category: "error",
346
+ type: EventTypes7.WEBSOCKET_ERROR,
347
+ detail
348
+ });
349
+ client.notify(event);
350
+ }
351
+
352
+ // src/dispatch/scriptDispatcher.ts
353
+ function scriptDispatcher(e) {
354
+ try {
355
+ const { type } = e;
356
+ if (type === "error") {
357
+ const targetEvent = e;
358
+ const { message, error } = targetEvent;
359
+ const immutableTarget = e.target || e.srcElement;
360
+ if (message && error) {
361
+ uncaughtErrorHandler(targetEvent);
362
+ } else if (immutableTarget) {
363
+ resourceErrorHandler(targetEvent);
364
+ }
365
+ } else if (type === "unhandledrejection") {
366
+ unhandledrejectionErrorHandler(e);
367
+ } else {
368
+ unknownErrorHandler(e);
369
+ }
370
+ } catch (error) {
371
+ unknownErrorHandler(error);
372
+ }
373
+ }
374
+
375
+ // src/dispatch/networkDispatcher.ts
376
+ import { EventTypes as EventTypes8 } from "@ohbug/core";
377
+ function networkDispatcher(type, detail) {
378
+ try {
379
+ switch (type) {
380
+ case EventTypes8.AJAX_ERROR:
381
+ ajaxErrorHandler(detail);
382
+ break;
383
+ case EventTypes8.FETCH_ERROR:
384
+ fetchErrorHandler(detail);
385
+ break;
386
+ case EventTypes8.WEBSOCKET_ERROR:
387
+ websocketErrorHandler(detail);
388
+ break;
389
+ default:
390
+ break;
391
+ }
392
+ } catch (error) {
393
+ unknownErrorHandler(error);
394
+ }
395
+ }
396
+
397
+ // src/capture/network/captureAjaxError.ts
398
+ var global3 = getGlobal3();
399
+ var access = "XMLHttpRequest" in global3;
400
+ var xhrOriginal = access ? {
401
+ open: XMLHttpRequest.prototype.open,
402
+ send: XMLHttpRequest.prototype.send
403
+ } : {};
404
+ function captureAjaxError() {
405
+ if (!access)
406
+ return;
407
+ const { client } = getOhbugObject11();
408
+ const desc = {
409
+ method: "",
410
+ url: ""
411
+ };
412
+ const xhrProto = XMLHttpRequest == null ? void 0 : XMLHttpRequest.prototype;
413
+ xhrOriginal.open = replace3(
414
+ xhrProto,
415
+ "open",
416
+ (origin) => function call(...args) {
417
+ const [method, url] = args;
418
+ desc.method = method;
419
+ desc.url = url;
420
+ return origin.apply(this, args);
421
+ }
422
+ );
423
+ xhrOriginal.send = replace3(
424
+ xhrProto,
425
+ "send",
426
+ (origin) => function call(...args) {
427
+ this.addEventListener("readystatechange", function handle() {
428
+ if (this.readyState === 4) {
429
+ if (desc.url !== client.__config.endpoint) {
430
+ const detail = {
431
+ req: {
432
+ url: desc.url,
433
+ method: desc.method
434
+ }
435
+ };
436
+ client.addAction("ajax", detail, "ajax");
437
+ if (!this.status || this.status >= 400) {
438
+ networkDispatcher(EventTypes9.AJAX_ERROR, detail);
439
+ }
440
+ }
441
+ }
442
+ });
443
+ return origin.apply(this, args);
444
+ }
445
+ );
446
+ }
447
+ function removeCaptureAjaxError() {
448
+ if (access && xhrOriginal.open && xhrOriginal.send) {
449
+ const xhrProto = XMLHttpRequest == null ? void 0 : XMLHttpRequest.prototype;
450
+ xhrProto.open = xhrOriginal.open;
451
+ xhrProto.send = xhrOriginal.send;
452
+ }
453
+ }
454
+
455
+ // src/capture/network/captureFetchError.ts
456
+ import { getGlobal as getGlobal4, getOhbugObject as getOhbugObject12, replace as replace4 } from "@ohbug/utils";
457
+ import { EventTypes as EventTypes10 } from "@ohbug/core";
458
+ var global4 = getGlobal4();
459
+ var access2 = "fetch" in global4;
460
+ var fetchOriginal = access2 ? global4.fetch : null;
461
+ function captureFetchError() {
462
+ if (!access2)
463
+ return;
464
+ const { client } = getOhbugObject12();
465
+ fetchOriginal = replace4(
466
+ global4,
467
+ "fetch",
468
+ (origin) => function call(...args) {
469
+ return origin.apply(this, args).then(
470
+ (res) => {
471
+ const [url, conf] = args;
472
+ const detail = {
473
+ req: {
474
+ url,
475
+ method: conf && conf.method
476
+ }
477
+ };
478
+ client.addAction("fetch", detail, "fetch");
479
+ if (!res.status || res.status >= 400) {
480
+ networkDispatcher(EventTypes10.FETCH_ERROR, detail);
481
+ }
482
+ return res;
483
+ },
484
+ (err) => {
485
+ const [url, conf] = args;
486
+ const detail = {
487
+ req: {
488
+ url,
489
+ method: conf && conf.method
490
+ }
491
+ };
492
+ networkDispatcher(EventTypes10.FETCH_ERROR, detail);
493
+ throw err;
494
+ }
495
+ );
496
+ }
497
+ );
498
+ }
499
+ function removeCaptureFetchError() {
500
+ if (access2 && fetchOriginal) {
501
+ global4.fetch = fetchOriginal;
502
+ }
503
+ }
504
+
505
+ // src/capture/network/captureWebSocketError.ts
506
+ import { getGlobal as getGlobal5 } from "@ohbug/utils";
507
+ import { EventTypes as EventTypes11 } from "@ohbug/core";
508
+ var global5 = getGlobal5();
509
+ function captureWebSocketError() {
510
+ if (!("WebSocket" in global5))
511
+ return;
512
+ const wsProto = WebSocket == null ? void 0 : WebSocket.prototype;
513
+ const backup = Object.getOwnPropertyDescriptor(wsProto, "onerror");
514
+ Object.defineProperty(wsProto, "onerror", {
515
+ set() {
516
+ var _a2;
517
+ const args = arguments;
518
+ const arg = args[0];
519
+ (_a2 = backup == null ? void 0 : backup.set) == null ? void 0 : _a2.call(this, function call(e) {
520
+ const {
521
+ target: {
522
+ url,
523
+ readyState
524
+ },
525
+ timeStamp
526
+ } = e;
527
+ const detail = {
528
+ url,
529
+ timeStamp,
530
+ readyState
531
+ };
532
+ networkDispatcher(EventTypes11.WEBSOCKET_ERROR, detail);
533
+ arg.apply(this, args);
534
+ });
535
+ }
536
+ });
537
+ }
538
+
539
+ // src/capture/script/captureUncaughtError.ts
540
+ import { getGlobal as getGlobal6 } from "@ohbug/utils";
541
+ var global6 = getGlobal6();
542
+ function listener2(e) {
543
+ scriptDispatcher(e);
544
+ }
545
+ function captureUncaughtError() {
546
+ var _a2;
547
+ (_a2 = global6 == null ? void 0 : global6.addEventListener) == null ? void 0 : _a2.call(global6, "error", listener2, true);
548
+ }
549
+ function removeCaptureUncaughtError() {
550
+ var _a2;
551
+ (_a2 = global6 == null ? void 0 : global6.removeEventListener) == null ? void 0 : _a2.call(global6, "error", listener2, true);
552
+ }
553
+
554
+ // src/capture/script/captureUnhandledrejectionError.ts
555
+ import { getGlobal as getGlobal7 } from "@ohbug/utils";
556
+ var global7 = getGlobal7();
557
+ function listener3(e) {
558
+ scriptDispatcher(e);
559
+ }
560
+ function captureUnhandledrejectionError() {
561
+ var _a2;
562
+ (_a2 = global7 == null ? void 0 : global7.addEventListener) == null ? void 0 : _a2.call(global7, "unhandledrejection", listener3, true);
563
+ }
564
+ function removeCaptureUnhandledrejectionError() {
565
+ var _a2;
566
+ (_a2 = global7 == null ? void 0 : global7.removeEventListener) == null ? void 0 : _a2.call(global7, "unhandledrejection", listener3, true);
567
+ }
568
+
569
+ // src/capture/index.ts
570
+ function captureAction() {
571
+ captureUrlChange();
572
+ captureClick();
573
+ }
574
+ function removeCaptureAction() {
575
+ removeCaptureUrlChange();
576
+ removeCaptureClick();
577
+ }
578
+ function captureNetwork() {
579
+ captureAjaxError();
580
+ captureFetchError();
581
+ captureWebSocketError();
582
+ }
583
+ function removeCaptureNetwork() {
584
+ removeCaptureAjaxError();
585
+ removeCaptureFetchError();
586
+ }
587
+ function captureScript() {
588
+ const global9 = getGlobal8();
589
+ if (!global9.addEventListener)
590
+ return;
591
+ captureUncaughtError();
592
+ captureUnhandledrejectionError();
593
+ }
594
+ function removeCaptureScript() {
595
+ removeCaptureUncaughtError();
596
+ removeCaptureUnhandledrejectionError();
597
+ }
598
+ function handleCapture() {
599
+ captureScript();
600
+ captureNetwork();
601
+ captureAction();
602
+ captureConsole();
603
+ }
604
+
605
+ // src/destroy.ts
606
+ import { getGlobal as getGlobal9 } from "@ohbug/utils";
607
+ var global8 = getGlobal9();
608
+ function handleDestroy() {
609
+ var _a2;
610
+ (_a2 = global8 == null ? void 0 : global8.addEventListener) == null ? void 0 : _a2.call(
611
+ global8,
612
+ "unload",
613
+ () => {
614
+ removeCaptureScript();
615
+ removeCaptureNetwork();
616
+ removeCaptureAction();
617
+ removeCaptureConsole();
618
+ },
619
+ true
620
+ );
621
+ }
622
+
623
+ // src/extension.ts
624
+ var extension = defineExtension({
625
+ name: "OhbugBrowser",
626
+ onSetup: () => {
627
+ handleCapture();
628
+ handleDestroy();
629
+ }
630
+ });
631
+
632
+ // src/notifier.ts
633
+ import { getOhbugObject as getOhbugObject13 } from "@ohbug/utils";
634
+ var getCircularReplacer = () => {
635
+ const seen = /* @__PURE__ */ new WeakSet();
636
+ return (_, value) => {
637
+ if (typeof value === "object" && value !== null) {
638
+ if (seen.has(value)) {
639
+ return;
640
+ }
641
+ seen.add(value);
642
+ }
643
+ return value;
644
+ };
645
+ };
646
+ function notifier(event) {
647
+ const { client } = getOhbugObject13();
648
+ const url = client.__config.endpoint;
649
+ return new Promise((resolve, reject) => {
650
+ const json = JSON.stringify(event, getCircularReplacer());
651
+ if (navigator.sendBeacon) {
652
+ const result = navigator.sendBeacon(url, json);
653
+ resolve(result);
654
+ } else {
655
+ const xhr = new XMLHttpRequest();
656
+ xhr.onreadystatechange = () => {
657
+ if (xhr.readyState === XMLHttpRequest.DONE) {
658
+ if (xhr.status >= 200 && xhr.status < 300) {
659
+ return resolve(xhr.response);
660
+ }
661
+ reject(xhr);
662
+ }
663
+ };
664
+ xhr.open("POST", url);
665
+ xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
666
+ xhr.send(json);
667
+ }
668
+ });
669
+ }
670
+
671
+ // src/client.ts
672
+ function createClient(config) {
673
+ const global9 = getGlobal10();
674
+ const sdk = {
675
+ platform: "ohbug-browser",
676
+ version
677
+ };
678
+ const client = new Client({ sdk, config, device, notifier });
679
+ global9.__OHBUG__ = { client };
680
+ client.use(extension);
681
+ client.__logger.info(
682
+ `%c @ohbug/browser %c Detected Ohbug v${version} %c`,
683
+ "background:#333; padding: 2px 1px; color: #FFF",
684
+ "background:#FF6F61; padding: 2px 1px; color: #FFF",
685
+ "background:transparent"
686
+ );
687
+ return client;
688
+ }
689
+ var BrowserClient = {
690
+ __client: null,
691
+ setup(config) {
692
+ var _a2;
693
+ if (BrowserClient.__client) {
694
+ (_a2 = BrowserClient.__client.__logger) == null ? void 0 : _a2.warn("init() has been called. Ignored.");
695
+ return BrowserClient.__client;
696
+ }
697
+ BrowserClient.__client = createClient(config);
698
+ return BrowserClient.__client;
699
+ }
700
+ };
701
+
702
+ // src/index.ts
703
+ var src_default = BrowserClient;
704
+ export {
705
+ src_default as default
706
+ };