visual-buried-point-platform-h5 1.3.6 → 2.0.2

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/src/index.ts DELETED
@@ -1,341 +0,0 @@
1
- import { Config, setConfig } from "./config";
2
- import {
3
- handleErr,
4
- handlePerf,
5
- handleHashchange,
6
- handleHistorystatechange,
7
- handleClick,
8
- handleBlur,
9
- handleResource,
10
- handleSum,
11
- handleAvg,
12
- handleMsg,
13
- handleHealth,
14
- handleApi,
15
- listenCircleListener,
16
- queryEventList,
17
- listenMessageListener,
18
- setPage,
19
- queryMetaConfigList,
20
- } from "./handlers";
21
- import {
22
- on,
23
- off,
24
- parseHash,
25
- isPlatform,
26
- randomUUID,
27
- detect,
28
- } from "./utils/tools";
29
- import { hackState, hackhook } from "./hack";
30
- import { GlobalVal } from "./config/global";
31
- import {
32
- lazyReportCache,
33
- report,
34
- onStartTrackReport,
35
- onDestroyTrackReport,
36
- reportVisualCustomEvent,
37
- onCustomReport as onCustomEventReport,
38
- } from "./utils/lazyReport";
39
- import { clearCache, getCache } from "./utils/cache";
40
- import { getDocumentElement } from "./utils/cloneDocument";
41
- import { ICustomData } from "./typings";
42
-
43
- let timer;
44
- let st;
45
- export default class Bombay {
46
- constructor(options, fn) {
47
- this.init(options);
48
- }
49
- init(options) {
50
- // 没有token,则不监听任何事件
51
- const platform = isPlatform();
52
- if (
53
- options &&
54
- options.enableJavaScriptBridge &&
55
- platform === "android" &&
56
- options.androidToken
57
- ) {
58
- options.token = options.androidToken;
59
- } else if (
60
- options &&
61
- options.enableJavaScriptBridge &&
62
- platform === "ios" &&
63
- options.iosToken
64
- ) {
65
- options.token = options.iosToken;
66
- }
67
- if (options && !options.token) {
68
- console.warn("请输入一个token");
69
- return;
70
- }
71
-
72
- setConfig(options);
73
- const onEvent = (type) => {
74
- const callback = () => {
75
- // 当纯HTML加载完成 上报一次性能数据
76
- if (type === "DOMContentLoaded") {
77
- window.dsy_app_call_js = function (params: any) {
78
- // 监听APP下发消息
79
- if (options.enableJavaScriptBridge) {
80
- GlobalVal.enableJavaScriptBridgeCircle = true;
81
- // 获取当前页面数据
82
- }
83
- };
84
- GlobalVal.userId = localStorage.getItem("userId")
85
- ? localStorage.getItem("userId")
86
- : "";
87
- GlobalVal.anonId = localStorage.getItem("anonId");
88
- if (!GlobalVal.anonId) {
89
- GlobalVal.anonId = randomUUID();
90
- localStorage.setItem("anonId", GlobalVal.anonId);
91
- }
92
- //清空路由记录
93
- GlobalVal.routerArray = [];
94
- //链路id, 单次访问系统时唯一
95
- GlobalVal.trackId = Date.now().toString();
96
- //运行环境和操作系统
97
- GlobalVal.accessPath = detect();
98
- //访问来源取referrer
99
- GlobalVal.referrer = localStorage.getItem("referrer")
100
- ? localStorage.getItem("referrer")
101
- : "";
102
- /**
103
- * 以下在内嵌的H5中会出现接口查询事件列表空
104
- * 而出现可能丢失浏览量数据
105
- */
106
- setTimeout(() => {
107
- lazyReportCache({
108
- type: "performance",
109
- subType: type.toLocaleLowerCase(),
110
- startTime: performance.now(), // 页面加载完成
111
- });
112
- let page = Config.enableSPA
113
- ? parseHash(location.hash.toLowerCase())
114
- : location.pathname.toLowerCase();
115
- setPage(page, true);
116
- }, 1000);
117
- }
118
- // 当页面完全加载时 上报埋点数据
119
- if (type === "load") {
120
- queryEventList(); // 查询已埋点事件
121
- //queryMetaConfigList(); //查询元素配置表
122
- // 是否上报页面性能
123
- Config.isPage && handlePerf();
124
- // 是否开启单页面
125
- Config.enableSPA && this.addListenRouterChange();
126
- // 是否上报错误消息
127
- Config.isError && this.addListenJs();
128
- // 是否上报ajax性能数据
129
- Config.isAjax && this.addListenAjax();
130
- // 是否录屏
131
- // Config.isRecord && this.addRrweb();
132
- // 行为是一个页面内的操作
133
- Config.isBehavior && this.addListenBehavior();
134
- Config.isResource && this.sendResource();
135
- // 上报健康数据
136
- Config.isRecord && this.addListenUnload();
137
- // 绑定全局变量
138
- window.__bb = this;
139
-
140
- // 开启可视化圈选操作 监听message
141
- listenMessageListener();
142
- if (GlobalVal.circle) {
143
- listenCircleListener();
144
- }
145
- // this.skData();
146
- }
147
- // 当页面进入后台或关闭前时,将所有的 cache 数据进行上报
148
- if (
149
- type === "beforeunload" ||
150
- type === "onHidden" ||
151
- type === "onunload"
152
- ) {
153
- // GlobalVal.leaveUrl = location.href;
154
- // handleHealth();
155
- // const data = getCache();
156
- // if (data.length) {
157
- // let _d = JSON.parse(sessionStorage.getItem("skipData"));
158
- // let d = _d ? _d.concat(data) : data;
159
- // sessionStorage.setItem("skipData", JSON.stringify(d));
160
- // clearTimeout(st);
161
- // }
162
- }
163
- window.removeEventListener(type, callback, true);
164
- };
165
- window.addEventListener(type, callback, true);
166
- };
167
- // 当页面完全加载完成时才会上报埋点事件 需要监听`load`及`DOMContentLoaded`;
168
- ["load", "DOMContentLoaded"].forEach((type) => onEvent(type));
169
- // 当页面进入后台或关闭前时,将所有的 cache 数据进行上报
170
- ["beforeunload", "onHidden", "onunload"].forEach((type) => onEvent(type));
171
- // 监听手机进入后台管理情况
172
- document.onvisibilitychange = () => {
173
- // 离开当前页面如切换到多任务窗口
174
- if (document.visibilityState === "hidden") {
175
- // handleHealth();
176
- }
177
- if (document.visibilityState === "visible") {
178
- // let page = Config.enableSPA
179
- // ? parseHash(location.hash.toLowerCase())
180
- // : location.pathname.toLowerCase();
181
- // setPage(page, true);
182
- }
183
- };
184
- // 监听页面加载完成
185
- document.onreadystatechange = () => {
186
- if (document.readyState === "complete") {
187
- console.log("监听页面加载完成");
188
- //获取H5 SDK初始化的页面标识为入口页面后续以此url比对
189
- GlobalVal.enterUrl = location.href;
190
- if (options.enableJavaScriptBridge) {
191
- getDocumentElement();
192
- timer = setInterval(() => {
193
- getDocumentElement();
194
- }, 5000);
195
- }
196
- }
197
- };
198
- }
199
-
200
- // 绑定用户ID
201
- setUserInfo(userId: any) {
202
- if (userId) {
203
- GlobalVal.userId = userId;
204
- localStorage.setItem("userId", userId);
205
- } else {
206
- GlobalVal.userId = "";
207
- localStorage.setItem("userId", "");
208
- }
209
- }
210
-
211
- // 调用自定义上报
212
- setCustomEvent(cData: ICustomData) {
213
- onCustomEventReport(cData);
214
- }
215
-
216
- // 注册
217
- onStartTrack(tData: any) {
218
- onStartTrackReport(tData);
219
- }
220
-
221
- // 销毁
222
- onDestroyTrack() {
223
- onDestroyTrackReport();
224
- }
225
-
226
- // 补上报skipData的数据
227
- skData() {
228
- st = setTimeout(() => {
229
- let d = JSON.parse(sessionStorage.getItem("skipData"));
230
- if (d) {
231
- let data = getCache();
232
- reportVisualCustomEvent(data, true);
233
- clearCache();
234
- }
235
- }, 5000);
236
- }
237
-
238
- // 发送资源
239
- sendResource() {
240
- "complete" === window.document.readyState
241
- ? handleResource()
242
- : this.addListenResource();
243
- }
244
-
245
- // 监听资源
246
- addListenResource() {
247
- on("load", handleResource);
248
- }
249
-
250
- // 监听行为
251
- addListenBehavior() {
252
- // Config.behavior.console.length && hackConsole();
253
- Config.behavior.click && this.addListenClick();
254
- }
255
-
256
- // 监听click
257
- addListenClick() {
258
- on("click", handleClick); // 非输入框点击,会过滤掉点击输入框
259
- // on("blur", handleBlur); // 输入框失焦
260
- }
261
-
262
- // 监听路由
263
- addListenRouterChange() {
264
- hackState("pushState");
265
- hackState("replaceState");
266
- on("hashchange", handleHashchange);
267
- on("historystatechanged", handleHistorystatechange);
268
- }
269
-
270
- addListenJs() {
271
- // js错误或静态资源加载错误
272
- on("error", handleErr);
273
- //promise错误
274
- on("unhandledrejection", handleErr);
275
- // window.addEventListener('rejectionhandled', rejectionhandled, true);
276
- }
277
-
278
- addListenAjax() {
279
- hackhook();
280
- }
281
-
282
- // beforeunload
283
- addListenUnload() {
284
- on("beforeunload", handleHealth);
285
- this.destroy();
286
- }
287
-
288
- // 移除路由
289
- removeListenRouterChange() {
290
- off("hashchange", handleHashchange);
291
- off("historystatechanged", handleHistorystatechange);
292
- }
293
-
294
- removeListenJs() {
295
- off("error", handleErr);
296
- off("unhandledrejection", handleErr);
297
- }
298
-
299
- // 监听资源
300
- removeListenResource() {
301
- off("beforeunload", handleHealth);
302
- }
303
-
304
- removeListenAjax() {}
305
-
306
- removeListenUnload() {
307
- off("load", handleResource);
308
- }
309
-
310
- removeRrweb() {}
311
-
312
- sum(key: string, val: number) {
313
- handleSum(key, val);
314
- }
315
-
316
- avg(key: string, val: number) {
317
- handleAvg(key, val);
318
- }
319
-
320
- msg(key: string) {
321
- handleMsg(key);
322
- }
323
-
324
- api(api, success, time, code, msg) {
325
- handleApi(api, success, time, code, msg, Date.now());
326
- }
327
-
328
- destroy() {
329
- timer && clearInterval(timer);
330
- st && clearTimeout(timer);
331
- Config.enableSPA && this.removeListenRouterChange();
332
- Config.isError && this.removeListenJs();
333
- Config.isAjax && this.removeListenAjax();
334
- Config.isRecord && this.removeRrweb();
335
- Config.isResource && this.removeListenResource();
336
- }
337
- }
338
-
339
- // export default (options: any, fn: any) => {
340
- // return new Bombay(options, fn);
341
- // };
package/src/reporter.ts DELETED
@@ -1,122 +0,0 @@
1
- import { Config } from "./config";
2
- import { ReportData } from "./typings";
3
- import { serialize, warn } from "./utils/tools";
4
- // post上报
5
- export function send(msg: ReportData) {
6
- // var body = msg[msg.t];
7
- // delete msg[msg.t];
8
- // var url = `${Config.reportUrl}?${serialize(msg)}`;
9
- // post(url, {
10
- // [msg.t]: body,
11
- // });
12
- // new Image().src = `${Config.reportUrl}?${serialize(msg)}`
13
- // request({
14
- // url: Config.reportUrl,
15
- // type: "POST",
16
- // async: false,
17
- // data: {
18
- // data: msg,
19
- // dataAbstract: "上报数据摘要",
20
- // },
21
- // });
22
- }
23
-
24
- export function post(url: string, body: { [x: string]: any }) {
25
- // var XMLHttpRequest = window.__oXMLHttpRequest_ || window.XMLHttpRequest;
26
- // if (typeof XMLHttpRequest === "function") {
27
- // try {
28
- // var xhr = new XMLHttpRequest();
29
- // xhr.open("POST", url, !0);
30
- // xhr.setRequestHeader("Content-Type", "text/plain");
31
- // xhr.setRequestHeader("ProjectId", Config.token);
32
- // xhr.send(JSON.stringify(body));
33
- // } catch (e) {
34
- // warn("[bombayjs] Failed to log, POST请求失败");
35
- // }
36
- // } else {
37
- // warn("[bombayjs] Failed to log, 浏览器不支持XMLHttpRequest");
38
- // }
39
- }
40
-
41
- // 健康检查上报
42
- export function sendBeacon(e: any) {
43
- "object" == typeof e && (e = serialize(e));
44
- e = `${Config.reportUrl}?${e}`;
45
- window && window.navigator && "function" == typeof window.navigator.sendBeacon
46
- ? window.navigator.sendBeacon(e)
47
- : warn("[arms] navigator.sendBeacon not surported");
48
- }
49
- /**
50
- * 对象参数的处理
51
- * @param data
52
- * @returns {string}
53
- */
54
- function getParams(data) {
55
- var arr = [];
56
- for (var param in data) {
57
- arr.push(encodeURIComponent(param) + "=" + encodeURIComponent(data[param]));
58
- }
59
- // arr.push(("randomNumber=" + Math.random()).replace(".", ""));
60
- return arr.join("&");
61
- }
62
-
63
- // 原生请求封装
64
- export function request(options: any = {}) {
65
- // 传入方式默认为对象
66
- // options = options || {};
67
- // 默认为GET请求
68
- options.type = (options.type || "GET").toUpperCase();
69
- // 返回值类型默认为json
70
- options.dataType = options.dataType || "json";
71
- // 默认为异步请求
72
- options.async = options.async || true;
73
- var params = getParams(options.data);
74
- var xhr;
75
- if (window.XMLHttpRequest) {
76
- /**
77
- * W3C标准
78
- * */
79
- xhr = new XMLHttpRequest();
80
- } else {
81
- /**
82
- * IE标准
83
- * @type {ActiveXObject}
84
- */
85
- // xhr = new ActiveXObject("Microsoft.XMLHTTP");
86
- }
87
- xhr.timeout = 10000;
88
- xhr.onreadystatechange = function () {
89
- if (xhr.readyState == 4) {
90
- var status = xhr.status;
91
- if (status >= 200 && status < 300) {
92
- options.success && options.success(xhr.responseText, xhr.responseXML);
93
- } else {
94
- options.fail && options.fail(status);
95
- }
96
- }
97
- };
98
-
99
- if (options.type === "GET") {
100
- xhr.open("GET", options.url + "?" + params, options.async);
101
- xhr.setRequestHeader("projectId", Config.token);
102
- xhr.send(null);
103
- } else if (options.type === "POST") {
104
- /**
105
- *打开请求
106
- * */
107
- xhr.open("POST", options.url, options.async);
108
- /**
109
- * POST请求设置请求头
110
- * */
111
- xhr.setRequestHeader("Content-Type", "application/json");
112
- xhr.setRequestHeader("projectId", Config.token);
113
- xhr.setRequestHeader("appKey", Config.token);
114
- // if (Config.appkey) {
115
- // xhr.setRequestHeader("appKey", Config.appkey);
116
- // }
117
- /**
118
- * 发送请求参数
119
- */
120
- xhr.send(JSON.stringify(options.data));
121
- }
122
- }
package/src/typing.d.ts DELETED
@@ -1,24 +0,0 @@
1
- interface Navigator {
2
- connection: any;
3
- }
4
-
5
- interface Window {
6
- attachEvent: any;
7
- detachEvent: any;
8
- CustomEvent: any;
9
- __oXMLHttpRequest_: any;
10
- XMLHttpRequest: any;
11
- __bb: any;
12
- __bb_onpopstate_: any;
13
- dsy_app_call_js: any;
14
- }
15
-
16
- interface HTMLStyleElement {
17
- styleSheet: {
18
- cssText: string
19
- }
20
- }
21
-
22
- declare module 'pako';
23
- declare module 'js-base64';
24
- declare module 'md5';