@fz-firefly/experience 0.0.0-stage → 0.6.1
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.cjs +120 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +119 -0
- package/dist/objectSpread2-BYC7ck9c.cjs +107 -0
- package/dist/objectSpread2-lWxa9t4g.js +78 -0
- package/dist/options-C2iLgUnA.d.cts +115 -0
- package/dist/options-C2iLgUnA.d.ts +115 -0
- package/dist/remote.cjs +236 -0
- package/dist/remote.d.cts +10 -0
- package/dist/remote.d.ts +10 -0
- package/dist/remote.js +235 -0
- package/dist/replay.cjs +301 -0
- package/dist/replay.d.cts +19 -0
- package/dist/replay.d.ts +19 -0
- package/dist/replay.js +299 -0
- package/dist/white-screen.cjs +275 -0
- package/dist/white-screen.d.cts +35 -0
- package/dist/white-screen.d.ts +35 -0
- package/dist/white-screen.js +273 -0
- package/package.json +69 -3
- package/README.md +0 -3
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_objectSpread2 = require("./objectSpread2-BYC7ck9c.cjs");
|
|
3
|
+
// src/attachment.ts
|
|
4
|
+
/**
|
|
5
|
+
* 上传白屏截图等附件。
|
|
6
|
+
*
|
|
7
|
+
* 附件不进入事件信封,避免把大块数据塞进 256KB 的上报包;
|
|
8
|
+
* 服务端按 DSN 鉴权,返回附件 id,事件里用 contexts.white_screen.attachment_id 关联。
|
|
9
|
+
*/
|
|
10
|
+
async function uploadAttachment(options) {
|
|
11
|
+
const url = `${require_objectSpread2.trimEndpoint(options.endpoint)}/api/ingest/v1/attachments?key=${encodeURIComponent(options.dsn)}`;
|
|
12
|
+
const headers = {
|
|
13
|
+
"content-type": options.mime,
|
|
14
|
+
"x-firefly-kind": options.kind
|
|
15
|
+
};
|
|
16
|
+
if (options.eventId) headers["x-firefly-event-id"] = options.eventId;
|
|
17
|
+
if (options.sessionId) headers["x-firefly-session-id"] = options.sessionId;
|
|
18
|
+
if (options.filename) headers["x-firefly-filename"] = options.filename;
|
|
19
|
+
if (options.meta) headers["x-firefly-meta"] = JSON.stringify(options.meta);
|
|
20
|
+
try {
|
|
21
|
+
const response = await fetch(url, {
|
|
22
|
+
method: "POST",
|
|
23
|
+
headers,
|
|
24
|
+
body: options.body,
|
|
25
|
+
keepalive: options.keepalive === true
|
|
26
|
+
});
|
|
27
|
+
if (!response.ok) return {
|
|
28
|
+
ok: false,
|
|
29
|
+
error: `HTTP ${response.status}`
|
|
30
|
+
};
|
|
31
|
+
const data = await response.json().catch(() => ({}));
|
|
32
|
+
return require_objectSpread2._objectSpread2({ ok: true }, typeof data.id === "string" ? { id: data.id } : {});
|
|
33
|
+
} catch (error) {
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
error: error instanceof Error ? error.message : String(error)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// src/white-screen.ts
|
|
41
|
+
const DEFAULT_IGNORE_SELECTORS = [
|
|
42
|
+
"[class*=\"loading\"]",
|
|
43
|
+
"[class*=\"skeleton\"]",
|
|
44
|
+
"[role=\"progressbar\"]"
|
|
45
|
+
];
|
|
46
|
+
function findRoot(doc, selectors) {
|
|
47
|
+
for (const selector of selectors) {
|
|
48
|
+
const element = doc.querySelector(selector);
|
|
49
|
+
if (element) return element;
|
|
50
|
+
}
|
|
51
|
+
return doc.body;
|
|
52
|
+
}
|
|
53
|
+
function isVisible(element) {
|
|
54
|
+
var _element$ownerDocumen;
|
|
55
|
+
const style = (_element$ownerDocumen = element.ownerDocument.defaultView) === null || _element$ownerDocumen === void 0 ? void 0 : _element$ownerDocumen.getComputedStyle(element);
|
|
56
|
+
if (style && (style.display === "none" || style.visibility === "hidden")) return false;
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 对当前 DOM 做一次轻量白屏打分。
|
|
61
|
+
*
|
|
62
|
+
* 不追求像素级判断,只回答「页面有没有可见内容」:
|
|
63
|
+
* 文本、可交互元素、媒体元素、根节点子元素数量。
|
|
64
|
+
*/
|
|
65
|
+
function detectWhiteScreen(doc, options = {}) {
|
|
66
|
+
var _doc$documentElement$, _doc$documentElement, _doc$documentElement$2, _doc$documentElement2, _options$ignoreSelect, _options$rootSelector, _clone$textContent, _options$scoreThresho;
|
|
67
|
+
const viewport = {
|
|
68
|
+
width: (_doc$documentElement$ = (_doc$documentElement = doc.documentElement) === null || _doc$documentElement === void 0 ? void 0 : _doc$documentElement.clientWidth) !== null && _doc$documentElement$ !== void 0 ? _doc$documentElement$ : 0,
|
|
69
|
+
height: (_doc$documentElement$2 = (_doc$documentElement2 = doc.documentElement) === null || _doc$documentElement2 === void 0 ? void 0 : _doc$documentElement2.clientHeight) !== null && _doc$documentElement$2 !== void 0 ? _doc$documentElement$2 : 0
|
|
70
|
+
};
|
|
71
|
+
if (!doc.body) return {
|
|
72
|
+
whiteScreen: true,
|
|
73
|
+
score: 0,
|
|
74
|
+
textLength: 0,
|
|
75
|
+
visibleElements: 0,
|
|
76
|
+
rootChildren: 0,
|
|
77
|
+
viewport,
|
|
78
|
+
reason: "empty-body"
|
|
79
|
+
};
|
|
80
|
+
if (doc.visibilityState === "hidden") return {
|
|
81
|
+
whiteScreen: false,
|
|
82
|
+
score: 1,
|
|
83
|
+
textLength: 0,
|
|
84
|
+
visibleElements: 0,
|
|
85
|
+
rootChildren: 0,
|
|
86
|
+
viewport,
|
|
87
|
+
reason: "hidden"
|
|
88
|
+
};
|
|
89
|
+
const ignoreSelectors = [...DEFAULT_IGNORE_SELECTORS, ...(_options$ignoreSelect = options.ignoreSelectors) !== null && _options$ignoreSelect !== void 0 ? _options$ignoreSelect : []];
|
|
90
|
+
for (const selector of ignoreSelectors) try {
|
|
91
|
+
const element = doc.querySelector(selector);
|
|
92
|
+
if (element && isVisible(element)) return {
|
|
93
|
+
whiteScreen: false,
|
|
94
|
+
score: 1,
|
|
95
|
+
textLength: 0,
|
|
96
|
+
visibleElements: 0,
|
|
97
|
+
rootChildren: 0,
|
|
98
|
+
viewport,
|
|
99
|
+
reason: "ignored-selector"
|
|
100
|
+
};
|
|
101
|
+
} catch (_unused) {}
|
|
102
|
+
const root = findRoot(doc, (_options$rootSelector = options.rootSelectors) !== null && _options$rootSelector !== void 0 ? _options$rootSelector : [
|
|
103
|
+
"#app",
|
|
104
|
+
"#root",
|
|
105
|
+
"body"
|
|
106
|
+
]);
|
|
107
|
+
const clone = root.cloneNode(true);
|
|
108
|
+
clone.querySelectorAll("script,style,noscript,template").forEach((element) => element.remove());
|
|
109
|
+
const textLength = ((_clone$textContent = clone.textContent) !== null && _clone$textContent !== void 0 ? _clone$textContent : "").replace(/\s+/g, " ").trim().length;
|
|
110
|
+
const rootChildren = root.children.length;
|
|
111
|
+
const candidates = doc.querySelectorAll("img,canvas,video,svg,input,button,a,select,textarea,[role=\"button\"]");
|
|
112
|
+
let visibleElements = 0;
|
|
113
|
+
let hasMedia = false;
|
|
114
|
+
for (const element of candidates) {
|
|
115
|
+
if (!isVisible(element)) continue;
|
|
116
|
+
visibleElements += 1;
|
|
117
|
+
if (element.matches("img,canvas,video,svg")) hasMedia = true;
|
|
118
|
+
}
|
|
119
|
+
const textScore = Math.min(1, textLength / 50);
|
|
120
|
+
const elementScore = Math.min(1, visibleElements / 4);
|
|
121
|
+
const score = Math.max(textScore, elementScore, hasMedia ? 1 : 0, rootChildren >= 2 ? .3 : 0);
|
|
122
|
+
const threshold = (_options$scoreThresho = options.scoreThreshold) !== null && _options$scoreThresho !== void 0 ? _options$scoreThresho : .15;
|
|
123
|
+
return {
|
|
124
|
+
whiteScreen: score < threshold,
|
|
125
|
+
score: Number(score.toFixed(3)),
|
|
126
|
+
textLength,
|
|
127
|
+
visibleElements,
|
|
128
|
+
rootChildren,
|
|
129
|
+
viewport,
|
|
130
|
+
reason: score < threshold ? "low-content" : "ok"
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/** 白屏错误类型,便于在代码错误列表里识别。 */
|
|
134
|
+
var WhiteScreenError = class extends Error {
|
|
135
|
+
constructor(message) {
|
|
136
|
+
super(message);
|
|
137
|
+
this.name = "WhiteScreenError";
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
function isIgnoredRoute(pathname, routes) {
|
|
141
|
+
return routes.some((route) => route.length > 0 && pathname.startsWith(route));
|
|
142
|
+
}
|
|
143
|
+
async function captureScreenshot(client, eventId, sessionId, options, score) {
|
|
144
|
+
try {
|
|
145
|
+
const { domToCanvas } = await import("modern-screenshot");
|
|
146
|
+
const width = Math.min(window.innerWidth || document.documentElement.clientWidth || 1280, options.maxScreenshotWidth);
|
|
147
|
+
const height = window.innerHeight || document.documentElement.clientHeight;
|
|
148
|
+
const scale = width / Math.max(1, window.innerWidth || width);
|
|
149
|
+
const canvas = await domToCanvas(document.body, {
|
|
150
|
+
width,
|
|
151
|
+
height,
|
|
152
|
+
scale,
|
|
153
|
+
backgroundColor: "#ffffff",
|
|
154
|
+
filter: (node) => {
|
|
155
|
+
if (!(node instanceof Element)) return true;
|
|
156
|
+
return !node.matches("input,textarea,[data-firefly-mask],[data-firefly-block]");
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
const blob = await new Promise((resolve) => {
|
|
160
|
+
canvas.toBlob((value) => resolve(value), "image/webp", options.screenshotQuality);
|
|
161
|
+
});
|
|
162
|
+
if (!blob) return;
|
|
163
|
+
const runtime = client.getRuntimeInfo();
|
|
164
|
+
await uploadAttachment({
|
|
165
|
+
endpoint: runtime.endpoint,
|
|
166
|
+
dsn: runtime.dsn,
|
|
167
|
+
kind: "screenshot",
|
|
168
|
+
eventId,
|
|
169
|
+
sessionId,
|
|
170
|
+
mime: blob.type || "image/webp",
|
|
171
|
+
body: blob,
|
|
172
|
+
filename: "white-screen.webp",
|
|
173
|
+
meta: { score }
|
|
174
|
+
});
|
|
175
|
+
} catch (_unused2) {}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* 安装白屏检测。
|
|
179
|
+
*
|
|
180
|
+
* 每个页面会话只检测一次;检测到白屏后上报一条 WhiteScreenError,
|
|
181
|
+
* 截图作为附件按 event_id 关联,不塞进事件信封。
|
|
182
|
+
*/
|
|
183
|
+
function installWhiteScreen(client, options) {
|
|
184
|
+
if (!require_objectSpread2.isBrowser()) return () => {};
|
|
185
|
+
let timer = null;
|
|
186
|
+
let loadHandler = null;
|
|
187
|
+
let reportedSessionId = "";
|
|
188
|
+
let hadError = false;
|
|
189
|
+
let disposed = false;
|
|
190
|
+
const unsubscribeEvent = client.onEvent((item) => {
|
|
191
|
+
if (item.type === "error" || item.type === "message" && item.level === "error") hadError = true;
|
|
192
|
+
});
|
|
193
|
+
function clearTimer() {
|
|
194
|
+
if (timer !== null) {
|
|
195
|
+
window.clearTimeout(timer);
|
|
196
|
+
timer = null;
|
|
197
|
+
}
|
|
198
|
+
if (loadHandler) {
|
|
199
|
+
window.removeEventListener("load", loadHandler);
|
|
200
|
+
loadHandler = null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function schedule(sessionId) {
|
|
204
|
+
if (disposed || !sessionId || sessionId === reportedSessionId) return;
|
|
205
|
+
if (isIgnoredRoute(window.location.pathname, options.ignoreRoutes)) return;
|
|
206
|
+
clearTimer();
|
|
207
|
+
hadError = false;
|
|
208
|
+
const run = () => {
|
|
209
|
+
if (disposed) return;
|
|
210
|
+
if (timer !== null) {
|
|
211
|
+
window.clearTimeout(timer);
|
|
212
|
+
timer = null;
|
|
213
|
+
}
|
|
214
|
+
timer = window.setTimeout(() => {
|
|
215
|
+
check(sessionId);
|
|
216
|
+
}, options.delayMs);
|
|
217
|
+
};
|
|
218
|
+
if (document.readyState === "complete") {
|
|
219
|
+
run();
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
loadHandler = () => {
|
|
223
|
+
loadHandler = null;
|
|
224
|
+
run();
|
|
225
|
+
};
|
|
226
|
+
window.addEventListener("load", loadHandler, { once: true });
|
|
227
|
+
timer = window.setTimeout(() => {
|
|
228
|
+
if (loadHandler) {
|
|
229
|
+
window.removeEventListener("load", loadHandler);
|
|
230
|
+
loadHandler = null;
|
|
231
|
+
run();
|
|
232
|
+
}
|
|
233
|
+
}, 1e4);
|
|
234
|
+
}
|
|
235
|
+
async function check(sessionId) {
|
|
236
|
+
timer = null;
|
|
237
|
+
if (disposed || reportedSessionId === sessionId) return;
|
|
238
|
+
if (document.visibilityState === "hidden") return;
|
|
239
|
+
const snapshot = detectWhiteScreen(document, {
|
|
240
|
+
scoreThreshold: options.scoreThreshold,
|
|
241
|
+
ignoreSelectors: options.ignoreSelectors,
|
|
242
|
+
rootSelectors: options.rootSelectors
|
|
243
|
+
});
|
|
244
|
+
if (!snapshot.whiteScreen) return;
|
|
245
|
+
if (hadError) return;
|
|
246
|
+
reportedSessionId = sessionId;
|
|
247
|
+
const eventId = client.captureException(new WhiteScreenError("页面无可见内容"), {
|
|
248
|
+
source: "white_screen",
|
|
249
|
+
level: "error",
|
|
250
|
+
tags: {
|
|
251
|
+
white_screen: "true",
|
|
252
|
+
white_screen_score: snapshot.score
|
|
253
|
+
},
|
|
254
|
+
contexts: { white_screen: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, snapshot), {}, {
|
|
255
|
+
url: window.location.href,
|
|
256
|
+
route: window.location.pathname
|
|
257
|
+
}) }
|
|
258
|
+
});
|
|
259
|
+
if (eventId && options.screenshot) captureScreenshot(client, eventId, sessionId, options, snapshot.score);
|
|
260
|
+
}
|
|
261
|
+
schedule(client.getRuntimeInfo().pageSession.id);
|
|
262
|
+
const unsubscribeSession = client.onPageSessionStart((info) => {
|
|
263
|
+
schedule(info.id);
|
|
264
|
+
});
|
|
265
|
+
return () => {
|
|
266
|
+
disposed = true;
|
|
267
|
+
clearTimer();
|
|
268
|
+
unsubscribeEvent();
|
|
269
|
+
unsubscribeSession();
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
exports.detectWhiteScreen = detectWhiteScreen;
|
|
273
|
+
exports.installWhiteScreen = installWhiteScreen;
|
|
274
|
+
|
|
275
|
+
//# sourceMappingURL=white-screen.cjs.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { o as ResolvedWhiteScreenOptions } from "./options-C2iLgUnA.cjs";
|
|
2
|
+
import { FireflyClient } from "@fz-firefly/core";
|
|
3
|
+
/** 一次白屏检测的结果。 */
|
|
4
|
+
export interface WhiteScreenSnapshot {
|
|
5
|
+
whiteScreen: boolean;
|
|
6
|
+
score: number;
|
|
7
|
+
textLength: number;
|
|
8
|
+
visibleElements: number;
|
|
9
|
+
rootChildren: number;
|
|
10
|
+
viewport: {
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
};
|
|
14
|
+
reason: string;
|
|
15
|
+
}
|
|
16
|
+
export interface DetectWhiteScreenOptions {
|
|
17
|
+
scoreThreshold?: number;
|
|
18
|
+
ignoreSelectors?: string[];
|
|
19
|
+
rootSelectors?: string[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 对当前 DOM 做一次轻量白屏打分。
|
|
23
|
+
*
|
|
24
|
+
* 不追求像素级判断,只回答「页面有没有可见内容」:
|
|
25
|
+
* 文本、可交互元素、媒体元素、根节点子元素数量。
|
|
26
|
+
*/
|
|
27
|
+
export declare function detectWhiteScreen(doc: Document, options?: DetectWhiteScreenOptions): WhiteScreenSnapshot;
|
|
28
|
+
/**
|
|
29
|
+
* 安装白屏检测。
|
|
30
|
+
*
|
|
31
|
+
* 每个页面会话只检测一次;检测到白屏后上报一条 WhiteScreenError,
|
|
32
|
+
* 截图作为附件按 event_id 关联,不塞进事件信封。
|
|
33
|
+
*/
|
|
34
|
+
export declare function installWhiteScreen(client: FireflyClient, options: ResolvedWhiteScreenOptions): () => void;
|
|
35
|
+
//# sourceMappingURL=white-screen.d.cts.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { o as ResolvedWhiteScreenOptions } from "./options-C2iLgUnA.js";
|
|
2
|
+
import { FireflyClient } from "@fz-firefly/core";
|
|
3
|
+
/** 一次白屏检测的结果。 */
|
|
4
|
+
export interface WhiteScreenSnapshot {
|
|
5
|
+
whiteScreen: boolean;
|
|
6
|
+
score: number;
|
|
7
|
+
textLength: number;
|
|
8
|
+
visibleElements: number;
|
|
9
|
+
rootChildren: number;
|
|
10
|
+
viewport: {
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
};
|
|
14
|
+
reason: string;
|
|
15
|
+
}
|
|
16
|
+
export interface DetectWhiteScreenOptions {
|
|
17
|
+
scoreThreshold?: number;
|
|
18
|
+
ignoreSelectors?: string[];
|
|
19
|
+
rootSelectors?: string[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 对当前 DOM 做一次轻量白屏打分。
|
|
23
|
+
*
|
|
24
|
+
* 不追求像素级判断,只回答「页面有没有可见内容」:
|
|
25
|
+
* 文本、可交互元素、媒体元素、根节点子元素数量。
|
|
26
|
+
*/
|
|
27
|
+
export declare function detectWhiteScreen(doc: Document, options?: DetectWhiteScreenOptions): WhiteScreenSnapshot;
|
|
28
|
+
/**
|
|
29
|
+
* 安装白屏检测。
|
|
30
|
+
*
|
|
31
|
+
* 每个页面会话只检测一次;检测到白屏后上报一条 WhiteScreenError,
|
|
32
|
+
* 截图作为附件按 event_id 关联,不塞进事件信封。
|
|
33
|
+
*/
|
|
34
|
+
export declare function installWhiteScreen(client: FireflyClient, options: ResolvedWhiteScreenOptions): () => void;
|
|
35
|
+
//# sourceMappingURL=white-screen.d.ts.map
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { a as trimEndpoint, i as isBrowser, t as _objectSpread2 } from "./objectSpread2-lWxa9t4g.js";
|
|
2
|
+
// src/attachment.ts
|
|
3
|
+
/**
|
|
4
|
+
* 上传白屏截图等附件。
|
|
5
|
+
*
|
|
6
|
+
* 附件不进入事件信封,避免把大块数据塞进 256KB 的上报包;
|
|
7
|
+
* 服务端按 DSN 鉴权,返回附件 id,事件里用 contexts.white_screen.attachment_id 关联。
|
|
8
|
+
*/
|
|
9
|
+
async function uploadAttachment(options) {
|
|
10
|
+
const url = `${trimEndpoint(options.endpoint)}/api/ingest/v1/attachments?key=${encodeURIComponent(options.dsn)}`;
|
|
11
|
+
const headers = {
|
|
12
|
+
"content-type": options.mime,
|
|
13
|
+
"x-firefly-kind": options.kind
|
|
14
|
+
};
|
|
15
|
+
if (options.eventId) headers["x-firefly-event-id"] = options.eventId;
|
|
16
|
+
if (options.sessionId) headers["x-firefly-session-id"] = options.sessionId;
|
|
17
|
+
if (options.filename) headers["x-firefly-filename"] = options.filename;
|
|
18
|
+
if (options.meta) headers["x-firefly-meta"] = JSON.stringify(options.meta);
|
|
19
|
+
try {
|
|
20
|
+
const response = await fetch(url, {
|
|
21
|
+
method: "POST",
|
|
22
|
+
headers,
|
|
23
|
+
body: options.body,
|
|
24
|
+
keepalive: options.keepalive === true
|
|
25
|
+
});
|
|
26
|
+
if (!response.ok) return {
|
|
27
|
+
ok: false,
|
|
28
|
+
error: `HTTP ${response.status}`
|
|
29
|
+
};
|
|
30
|
+
const data = await response.json().catch(() => ({}));
|
|
31
|
+
return _objectSpread2({ ok: true }, typeof data.id === "string" ? { id: data.id } : {});
|
|
32
|
+
} catch (error) {
|
|
33
|
+
return {
|
|
34
|
+
ok: false,
|
|
35
|
+
error: error instanceof Error ? error.message : String(error)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// src/white-screen.ts
|
|
40
|
+
const DEFAULT_IGNORE_SELECTORS = [
|
|
41
|
+
"[class*=\"loading\"]",
|
|
42
|
+
"[class*=\"skeleton\"]",
|
|
43
|
+
"[role=\"progressbar\"]"
|
|
44
|
+
];
|
|
45
|
+
function findRoot(doc, selectors) {
|
|
46
|
+
for (const selector of selectors) {
|
|
47
|
+
const element = doc.querySelector(selector);
|
|
48
|
+
if (element) return element;
|
|
49
|
+
}
|
|
50
|
+
return doc.body;
|
|
51
|
+
}
|
|
52
|
+
function isVisible(element) {
|
|
53
|
+
var _element$ownerDocumen;
|
|
54
|
+
const style = (_element$ownerDocumen = element.ownerDocument.defaultView) === null || _element$ownerDocumen === void 0 ? void 0 : _element$ownerDocumen.getComputedStyle(element);
|
|
55
|
+
if (style && (style.display === "none" || style.visibility === "hidden")) return false;
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 对当前 DOM 做一次轻量白屏打分。
|
|
60
|
+
*
|
|
61
|
+
* 不追求像素级判断,只回答「页面有没有可见内容」:
|
|
62
|
+
* 文本、可交互元素、媒体元素、根节点子元素数量。
|
|
63
|
+
*/
|
|
64
|
+
function detectWhiteScreen(doc, options = {}) {
|
|
65
|
+
var _doc$documentElement$, _doc$documentElement, _doc$documentElement$2, _doc$documentElement2, _options$ignoreSelect, _options$rootSelector, _clone$textContent, _options$scoreThresho;
|
|
66
|
+
const viewport = {
|
|
67
|
+
width: (_doc$documentElement$ = (_doc$documentElement = doc.documentElement) === null || _doc$documentElement === void 0 ? void 0 : _doc$documentElement.clientWidth) !== null && _doc$documentElement$ !== void 0 ? _doc$documentElement$ : 0,
|
|
68
|
+
height: (_doc$documentElement$2 = (_doc$documentElement2 = doc.documentElement) === null || _doc$documentElement2 === void 0 ? void 0 : _doc$documentElement2.clientHeight) !== null && _doc$documentElement$2 !== void 0 ? _doc$documentElement$2 : 0
|
|
69
|
+
};
|
|
70
|
+
if (!doc.body) return {
|
|
71
|
+
whiteScreen: true,
|
|
72
|
+
score: 0,
|
|
73
|
+
textLength: 0,
|
|
74
|
+
visibleElements: 0,
|
|
75
|
+
rootChildren: 0,
|
|
76
|
+
viewport,
|
|
77
|
+
reason: "empty-body"
|
|
78
|
+
};
|
|
79
|
+
if (doc.visibilityState === "hidden") return {
|
|
80
|
+
whiteScreen: false,
|
|
81
|
+
score: 1,
|
|
82
|
+
textLength: 0,
|
|
83
|
+
visibleElements: 0,
|
|
84
|
+
rootChildren: 0,
|
|
85
|
+
viewport,
|
|
86
|
+
reason: "hidden"
|
|
87
|
+
};
|
|
88
|
+
const ignoreSelectors = [...DEFAULT_IGNORE_SELECTORS, ...(_options$ignoreSelect = options.ignoreSelectors) !== null && _options$ignoreSelect !== void 0 ? _options$ignoreSelect : []];
|
|
89
|
+
for (const selector of ignoreSelectors) try {
|
|
90
|
+
const element = doc.querySelector(selector);
|
|
91
|
+
if (element && isVisible(element)) return {
|
|
92
|
+
whiteScreen: false,
|
|
93
|
+
score: 1,
|
|
94
|
+
textLength: 0,
|
|
95
|
+
visibleElements: 0,
|
|
96
|
+
rootChildren: 0,
|
|
97
|
+
viewport,
|
|
98
|
+
reason: "ignored-selector"
|
|
99
|
+
};
|
|
100
|
+
} catch (_unused) {}
|
|
101
|
+
const root = findRoot(doc, (_options$rootSelector = options.rootSelectors) !== null && _options$rootSelector !== void 0 ? _options$rootSelector : [
|
|
102
|
+
"#app",
|
|
103
|
+
"#root",
|
|
104
|
+
"body"
|
|
105
|
+
]);
|
|
106
|
+
const clone = root.cloneNode(true);
|
|
107
|
+
clone.querySelectorAll("script,style,noscript,template").forEach((element) => element.remove());
|
|
108
|
+
const textLength = ((_clone$textContent = clone.textContent) !== null && _clone$textContent !== void 0 ? _clone$textContent : "").replace(/\s+/g, " ").trim().length;
|
|
109
|
+
const rootChildren = root.children.length;
|
|
110
|
+
const candidates = doc.querySelectorAll("img,canvas,video,svg,input,button,a,select,textarea,[role=\"button\"]");
|
|
111
|
+
let visibleElements = 0;
|
|
112
|
+
let hasMedia = false;
|
|
113
|
+
for (const element of candidates) {
|
|
114
|
+
if (!isVisible(element)) continue;
|
|
115
|
+
visibleElements += 1;
|
|
116
|
+
if (element.matches("img,canvas,video,svg")) hasMedia = true;
|
|
117
|
+
}
|
|
118
|
+
const textScore = Math.min(1, textLength / 50);
|
|
119
|
+
const elementScore = Math.min(1, visibleElements / 4);
|
|
120
|
+
const score = Math.max(textScore, elementScore, hasMedia ? 1 : 0, rootChildren >= 2 ? .3 : 0);
|
|
121
|
+
const threshold = (_options$scoreThresho = options.scoreThreshold) !== null && _options$scoreThresho !== void 0 ? _options$scoreThresho : .15;
|
|
122
|
+
return {
|
|
123
|
+
whiteScreen: score < threshold,
|
|
124
|
+
score: Number(score.toFixed(3)),
|
|
125
|
+
textLength,
|
|
126
|
+
visibleElements,
|
|
127
|
+
rootChildren,
|
|
128
|
+
viewport,
|
|
129
|
+
reason: score < threshold ? "low-content" : "ok"
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** 白屏错误类型,便于在代码错误列表里识别。 */
|
|
133
|
+
var WhiteScreenError = class extends Error {
|
|
134
|
+
constructor(message) {
|
|
135
|
+
super(message);
|
|
136
|
+
this.name = "WhiteScreenError";
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
function isIgnoredRoute(pathname, routes) {
|
|
140
|
+
return routes.some((route) => route.length > 0 && pathname.startsWith(route));
|
|
141
|
+
}
|
|
142
|
+
async function captureScreenshot(client, eventId, sessionId, options, score) {
|
|
143
|
+
try {
|
|
144
|
+
const { domToCanvas } = await import("modern-screenshot");
|
|
145
|
+
const width = Math.min(window.innerWidth || document.documentElement.clientWidth || 1280, options.maxScreenshotWidth);
|
|
146
|
+
const height = window.innerHeight || document.documentElement.clientHeight;
|
|
147
|
+
const scale = width / Math.max(1, window.innerWidth || width);
|
|
148
|
+
const canvas = await domToCanvas(document.body, {
|
|
149
|
+
width,
|
|
150
|
+
height,
|
|
151
|
+
scale,
|
|
152
|
+
backgroundColor: "#ffffff",
|
|
153
|
+
filter: (node) => {
|
|
154
|
+
if (!(node instanceof Element)) return true;
|
|
155
|
+
return !node.matches("input,textarea,[data-firefly-mask],[data-firefly-block]");
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
const blob = await new Promise((resolve) => {
|
|
159
|
+
canvas.toBlob((value) => resolve(value), "image/webp", options.screenshotQuality);
|
|
160
|
+
});
|
|
161
|
+
if (!blob) return;
|
|
162
|
+
const runtime = client.getRuntimeInfo();
|
|
163
|
+
await uploadAttachment({
|
|
164
|
+
endpoint: runtime.endpoint,
|
|
165
|
+
dsn: runtime.dsn,
|
|
166
|
+
kind: "screenshot",
|
|
167
|
+
eventId,
|
|
168
|
+
sessionId,
|
|
169
|
+
mime: blob.type || "image/webp",
|
|
170
|
+
body: blob,
|
|
171
|
+
filename: "white-screen.webp",
|
|
172
|
+
meta: { score }
|
|
173
|
+
});
|
|
174
|
+
} catch (_unused2) {}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 安装白屏检测。
|
|
178
|
+
*
|
|
179
|
+
* 每个页面会话只检测一次;检测到白屏后上报一条 WhiteScreenError,
|
|
180
|
+
* 截图作为附件按 event_id 关联,不塞进事件信封。
|
|
181
|
+
*/
|
|
182
|
+
function installWhiteScreen(client, options) {
|
|
183
|
+
if (!isBrowser()) return () => {};
|
|
184
|
+
let timer = null;
|
|
185
|
+
let loadHandler = null;
|
|
186
|
+
let reportedSessionId = "";
|
|
187
|
+
let hadError = false;
|
|
188
|
+
let disposed = false;
|
|
189
|
+
const unsubscribeEvent = client.onEvent((item) => {
|
|
190
|
+
if (item.type === "error" || item.type === "message" && item.level === "error") hadError = true;
|
|
191
|
+
});
|
|
192
|
+
function clearTimer() {
|
|
193
|
+
if (timer !== null) {
|
|
194
|
+
window.clearTimeout(timer);
|
|
195
|
+
timer = null;
|
|
196
|
+
}
|
|
197
|
+
if (loadHandler) {
|
|
198
|
+
window.removeEventListener("load", loadHandler);
|
|
199
|
+
loadHandler = null;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function schedule(sessionId) {
|
|
203
|
+
if (disposed || !sessionId || sessionId === reportedSessionId) return;
|
|
204
|
+
if (isIgnoredRoute(window.location.pathname, options.ignoreRoutes)) return;
|
|
205
|
+
clearTimer();
|
|
206
|
+
hadError = false;
|
|
207
|
+
const run = () => {
|
|
208
|
+
if (disposed) return;
|
|
209
|
+
if (timer !== null) {
|
|
210
|
+
window.clearTimeout(timer);
|
|
211
|
+
timer = null;
|
|
212
|
+
}
|
|
213
|
+
timer = window.setTimeout(() => {
|
|
214
|
+
check(sessionId);
|
|
215
|
+
}, options.delayMs);
|
|
216
|
+
};
|
|
217
|
+
if (document.readyState === "complete") {
|
|
218
|
+
run();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
loadHandler = () => {
|
|
222
|
+
loadHandler = null;
|
|
223
|
+
run();
|
|
224
|
+
};
|
|
225
|
+
window.addEventListener("load", loadHandler, { once: true });
|
|
226
|
+
timer = window.setTimeout(() => {
|
|
227
|
+
if (loadHandler) {
|
|
228
|
+
window.removeEventListener("load", loadHandler);
|
|
229
|
+
loadHandler = null;
|
|
230
|
+
run();
|
|
231
|
+
}
|
|
232
|
+
}, 1e4);
|
|
233
|
+
}
|
|
234
|
+
async function check(sessionId) {
|
|
235
|
+
timer = null;
|
|
236
|
+
if (disposed || reportedSessionId === sessionId) return;
|
|
237
|
+
if (document.visibilityState === "hidden") return;
|
|
238
|
+
const snapshot = detectWhiteScreen(document, {
|
|
239
|
+
scoreThreshold: options.scoreThreshold,
|
|
240
|
+
ignoreSelectors: options.ignoreSelectors,
|
|
241
|
+
rootSelectors: options.rootSelectors
|
|
242
|
+
});
|
|
243
|
+
if (!snapshot.whiteScreen) return;
|
|
244
|
+
if (hadError) return;
|
|
245
|
+
reportedSessionId = sessionId;
|
|
246
|
+
const eventId = client.captureException(new WhiteScreenError("页面无可见内容"), {
|
|
247
|
+
source: "white_screen",
|
|
248
|
+
level: "error",
|
|
249
|
+
tags: {
|
|
250
|
+
white_screen: "true",
|
|
251
|
+
white_screen_score: snapshot.score
|
|
252
|
+
},
|
|
253
|
+
contexts: { white_screen: _objectSpread2(_objectSpread2({}, snapshot), {}, {
|
|
254
|
+
url: window.location.href,
|
|
255
|
+
route: window.location.pathname
|
|
256
|
+
}) }
|
|
257
|
+
});
|
|
258
|
+
if (eventId && options.screenshot) captureScreenshot(client, eventId, sessionId, options, snapshot.score);
|
|
259
|
+
}
|
|
260
|
+
schedule(client.getRuntimeInfo().pageSession.id);
|
|
261
|
+
const unsubscribeSession = client.onPageSessionStart((info) => {
|
|
262
|
+
schedule(info.id);
|
|
263
|
+
});
|
|
264
|
+
return () => {
|
|
265
|
+
disposed = true;
|
|
266
|
+
clearTimer();
|
|
267
|
+
unsubscribeEvent();
|
|
268
|
+
unsubscribeSession();
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
export { detectWhiteScreen, installWhiteScreen };
|
|
272
|
+
|
|
273
|
+
//# sourceMappingURL=white-screen.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fz-firefly/experience",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.6.1",
|
|
4
|
+
"description": "萤火虫 SDK 的可选体验增强:白屏检测、会话回放、用户连线。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./white-screen": {
|
|
16
|
+
"types": "./dist/white-screen.d.ts",
|
|
17
|
+
"import": "./dist/white-screen.js",
|
|
18
|
+
"require": "./dist/white-screen.cjs"
|
|
19
|
+
},
|
|
20
|
+
"./replay": {
|
|
21
|
+
"types": "./dist/replay.d.ts",
|
|
22
|
+
"import": "./dist/replay.js",
|
|
23
|
+
"require": "./dist/replay.cjs"
|
|
24
|
+
},
|
|
25
|
+
"./remote": {
|
|
26
|
+
"types": "./dist/remote.d.ts",
|
|
27
|
+
"import": "./dist/remote.js",
|
|
28
|
+
"require": "./dist/remote.cjs"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"!dist/**/*.map"
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": [
|
|
37
|
+
"./dist/index.js",
|
|
38
|
+
"./dist/index.cjs"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@fz-firefly/core": "0.6.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"jsdom": "^25.0.0",
|
|
48
|
+
"modern-screenshot": "^4.7.0",
|
|
49
|
+
"rrweb": "^2.2.2",
|
|
50
|
+
"tsdown": "0.23.2",
|
|
51
|
+
"typescript": "^5.6.0",
|
|
52
|
+
"vitest": "^5.0.5"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"modern-screenshot": ">=4.0.0",
|
|
56
|
+
"rrweb": ">=2.0.0"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"modern-screenshot": {
|
|
60
|
+
"optional": true
|
|
61
|
+
},
|
|
62
|
+
"rrweb": {
|
|
63
|
+
"optional": true
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"dev": "tsdown --watch --no-clean",
|
|
68
|
+
"build": "tsdown",
|
|
69
|
+
"test": "vitest run",
|
|
70
|
+
"typecheck": "tsc --noEmit"
|
|
71
|
+
}
|
|
6
72
|
}
|
package/README.md
DELETED