UNPKG

reporting-lib

Version:

A comprehensive monitoring and reporting library for web applications

205 lines 6.32 kB
export const detectWhiteScreen = ({ samplePoints = 6, whiteTags = ['HTML', 'BODY', "IFRAME"], onDetected = (isWhiteScreen, detail) => { }, threshold = 0.9, } = {}) => { const viewportWidth = window.innerWidth; const viewportHeight = window.innerHeight; const points = []; for (let i = 1; i <= samplePoints; i++) { points.push([ viewportWidth / 2, (viewportHeight / (samplePoints + 1)) * i, ]); } let whiteCount = 0; const detail = []; points.forEach(([x, y]) => { const el = document.elementFromPoint(x, y); const tag = el?.tagName || 'NULL'; detail.push({ x, y, tag }); if (!el || whiteTags.includes(tag)) { whiteCount++; } }); const isWhite = whiteCount / samplePoints >= threshold; onDetected(isWhite, detail); }; export const debounce = (fn, delay = 300, immediate = false) => { let timer = null; return function (...args) { const context = this; if (timer) clearTimeout(timer); if (immediate && !timer) { fn.apply(context, args); } timer = setTimeout(() => { if (!immediate) { fn.apply(context, args); } timer = null; }, delay); }; }; const debouncedDetectWhiteScreen = debounce((resolve) => { detectWhiteScreen({ onDetected: (isWhite, detail) => { resolve(isWhite); pendingPromise = null; }, }); }, 1000); let pendingPromise = null; export const detectionWhiteScreen = () => { if (!pendingPromise) { pendingPromise = new Promise((resolve) => { setTimeout(() => { debouncedDetectWhiteScreen(resolve); }, 3000); }); } return pendingPromise; }; export const getFullBrowserInfo = () => { const ua = navigator.userAgent; let name = 'Unknown'; let version = 'Unknown'; if (/edg/i.test(ua)) { name = 'Edge'; version = ua.match(/edg\/([\d\.]+)/i)?.[1]; } else if (/opr/i.test(ua)) { name = 'Opera'; version = ua.match(/opr\/([\d\.]+)/i)?.[1]; } else if (/chrome/i.test(ua)) { name = 'Chrome'; version = ua.match(/chrome\/([\d\.]+)/i)?.[1]; } else if (/firefox/i.test(ua)) { name = 'Firefox'; version = ua.match(/firefox\/([\d\.]+)/i)?.[1]; } else if (/safari/i.test(ua) && !/chrome/i.test(ua)) { name = 'Safari'; version = ua.match(/version\/([\d\.]+)/i)?.[1]; } else if (/msie/i.test(ua) || /trident/i.test(ua)) { name = 'Internet Explorer'; version = ua.match(/(msie\s|rv:)([\d\.]+)/i)?.[2]; } return { name, version }; }; export const getOSInfo = () => { const ua = navigator.userAgent || navigator.vendor || window.opera; if (/windows phone/i.test(ua)) { return "Windows Phone"; } if (/win/i.test(ua)) { return "Windows"; } if (/android/i.test(ua)) { return "Android"; } if (/iPad|iPhone|iPod/.test(ua) && !window.MSStream) { return "iOS"; } if (/Macintosh|MacIntel|MacPPC|Mac68K/.test(ua)) { return "macOS"; } if (/Linux/.test(ua)) { return "Linux"; } return "Unknown OS"; }; // 识别是上传视频的链接 export const isValidOssVideoUrl = (url) => { try { const u = new URL(url.indexOf('http') === -1 ? `https:${url}` : url); // 域名和协议必须匹配 // const hostOk = ( // u.protocol === 'https:' && // u.hostname === 'sd-cms-bmp.oss-accelerate.aliyuncs.com' && // u.port === '443' // ); // 路径必须匹配 /mbp/数字/video/文件名.扩展名 const pathOk = /^\/mbp\/\d+\/video\/[^/?#]+\.[^/?#]+$/i.test(u.pathname); // return hostOk && pathOk; return pathOk; } catch { return false; // 非法 URL } }; export const toReadableString = (data) => { try { return JSON.stringify(data, null, 2); } catch (e) { return String(data); } }; export const getRequestBodySize = (body) => { if (!body) return 0; try { if (typeof body === "string") return new TextEncoder().encode(body).length; if (body instanceof Blob) return body.size; if (body instanceof ArrayBuffer) return body.byteLength; if (ArrayBuffer.isView(body)) return body.byteLength; if (body instanceof URLSearchParams) return new TextEncoder().encode(body.toString()).length; if (body instanceof FormData) { let total = 0; for (const [key, value] of body.entries()) { total += new TextEncoder().encode(key).length; if (typeof value === "string") { total += new TextEncoder().encode(value).length; } else if (value instanceof Blob) { total += value.size; } } return total; } } catch (error) { return 0; } return 0; }; export const getRequestBodySizeFetch = (body) => { if (!body) return 0; try { if (typeof body === "string") return new TextEncoder().encode(body).length; if (body instanceof Blob) return body.size; if (body instanceof ArrayBuffer) return body.byteLength; if (ArrayBuffer.isView(body)) return body.byteLength; if (body instanceof URLSearchParams) return new TextEncoder().encode(body.toString()).length; if (body instanceof FormData) { let total = 0; for (const [key, value] of body.entries()) { total += new TextEncoder().encode(key).length; if (typeof value === "string") { total += new TextEncoder().encode(value).length; } else if (value instanceof Blob) { total += value.size; } } return total; } } catch (error) { return 0; } return 0; }; //# sourceMappingURL=tool.js.map