### Get First Paint and FCP Metrics Source: https://weexapp.com/zh/guide/contribute/how-to-contribute Retrieves 'first-paint' and 'first-contentful-paint' (fcp) times using the Performance API. It includes fallbacks for older browsers or environments where `getEntriesByType` might not be available, checking for `window.chrome.loadTimes` or `window.performance.timing.msFirstPaint`. ```javascript function o() { var e = {}; if (r.isFunction(performance.getEntriesByType)) { var t = performance.getEntriesByType("paint"); if (t) for (var n = 0, o = t.length; n < o; n++) { var i = t[n]; "first-paint" === i.name ? e.firstPaint = i.startTime : "first-contentful-paint" === i.name && (e.fcp = parseInt(i.startTime)) } } else if (window.chrome && window.chrome.loadTimes) { var a = 1e3 * window.chrome.loadTimes().firstPaintTime; e.firstPaint = a - 1e3 * window.chrome.loadTimes().startLoadTime } else if ("number" == typeof window.performance.timing.msFirstPaint) { var s = window.performance.timing.msFirstPaint; e.firstPaint = s - window.performance.timing.navigationStart } return e.firstPaint && (e.firstPaint = parseInt(e.firstPaint)), e } ``` -------------------------------- ### White Screen Detection Configuration (JavaScript) Source: https://weexapp.com/zh/docs/modules/websockets This snippet defines the configuration and setup for a white screen detection feature. It allows enabling/disabling the feature, setting a start time, specifying a root element ID to monitor, a sampling rate, and whether to check when the page unloads. The core logic involves checking the document's ready state and analyzing the content of a specified root element or the body to determine if a white screen condition is met. ```javascript var r = n(0); e.exports = { "install": function (e) { var t = e.getConfig("whiteScreen") || {} , n = t.startTime || 1e4 , o = t.rootElementId , i = t.enable === undefined || !!t.enable , a = t.sampling || 1 , s = !1 !== t.checkWhenUnload , c = t.uploadInnerHtml; if (i) { var u = r.now() , l = !1 , f = function (t) { if (document && document.getElementById && !(t && "complete" !== document.readyState || l)) { l = !0; var n = o ? document.getElementById(o) : document.body || {} if (n) { var i = function (e) { try { return !!e.innerText } catch (t) { } }(n) , s = function () { if (window.performance && r.isFunction(performance.getEntriesByType)) { var e = performance.getEntriesByName("first-contentful-paint"); return 0 !== e.length } }() , u = function (e) { try { ``` -------------------------------- ### Get First Paint and FCP Metrics Source: https://weexapp.com/zh/docs/modules/console-log Retrieves 'first-paint' and 'first-contentful-paint' (fcp) metrics using `performance.getEntriesByType('paint')`. It includes fallbacks for browsers that do not support this API, utilizing `window.chrome.loadTimes` or `window.performance.timing.msFirstPaint`. ```javascript var e = {} if (r.isFunction(performance.getEntriesByType)) { var t = performance.getEntriesByType("paint"); if (t) for (var n = 0, o = t.length; n < o; n++) { var i = t[n]; "first-paint" === i.name ? e.firstPaint = i.startTime : "first-contentful-paint" === i.name && (e.fcp = parseInt(i.startTime)) } } else if (window.chrome && window.chrome.loadTimes) { var a = 1e3 * window.chrome.loadTimes().firstPaintTime; e.firstPaint = a - 1e3 * window.chrome.loadTimes().startLoadTime } else if ("number" == typeof window.performance.timing.msFirstPaint) { var s = window.performance.timing.msFirstPaint; e.firstPaint = s - window.performance.timing.navigationStart } return e.firstPaint && (e.firstPaint = parseInt(e.firstPaint)), e ``` -------------------------------- ### Initialize Performance Metrics Object Source: https://weexapp.com/zh/download This function initializes an object with default performance metrics. It includes counts and averages for speed, resources, image loading, slow resources, transfer sizes, and idle time. It returns this default object if `performance.getEntries` is not available. Dependencies include `a.isFunction` and `performance.getEntries`. ```javascript var t = null , n = { "speedCount": [], "speedMax": 0, "speedAvg": 0, "resourceCount": 0, "imgCount": 0, "slowResourceCount": 0, "totalTransferSize": 0, "baseResourceTransferSize": 0, "baseResourceCount": 0, "idleTime": 0, "rateParallelUse": 0 }; if (!a.isFunction(performance.getEntries)) return n; ``` -------------------------------- ### Get Device Unique ID (UTDID) Source: https://weexapp.com/zh/docs/modules/globalEvent Retrieves the unique device identifier (UTDID) using the Windvane API. It attempts to get the UTDID when the module loads and again on window load if not yet obtained. This ensures the UTDID is available for subsequent operations. ```javascript var r = n(0) , o = !1 , i = ""; function a() { r.isMatchApp(["", "TB"]) && r.callWindvane("TBDeviceInfo", "getUtdid", {}, function (e) { o = !0, i = e && e.utdid ? e.utdid : "" }) } a(), r.onload(function () { !o && a() }), e.exports = function () { return i } ``` -------------------------------- ### JavaScript: Module Initialization for Event Handling Source: https://weexapp.com/zh/guide/introduction This snippet shows the initialization of a module that likely handles various event reporting functionalities. It imports necessary modules like `r`, `o`, `i`, `a`, `s`, `c`, `u`, `l`, `f`, `d`. It also sets up a 'beforeunload' event listener to track page unload status. ```javascript "use strict"; var r = n(0) , o = n(15) , i = n(16) , a = n(1) , s = n(2) , c = n(3) , u = n(18) , l = n(4) , f = n(19) , d = n(5) , p = (n(9), l.getPageOverview) , m = ["api_perf", "manual"]; var g = !1; window.addEventListener("beforeunload", function () { g = !0 }), e.exports = function (e, t) { try { if (!e || "PRELOAD" === r.getMode()) return; for (var n in e && e.constructor === Object || (e = i(e)), ``` -------------------------------- ### Consolidate Performance Metrics and Timings Source: https://weexapp.com/zh/guide/develop/create-a-new-app This snippet consolidates various performance metrics by merging data from first paint calculations, predefined timing calculations using the PerformanceTiming API, and potentially memory information. It defines a mapping for common timing intervals like 'load', 'domReady', 'ttfb', etc. ```javascript function () { var e = window.performance || window.webkitPerformance || window.msPerformance || window.mozPerformance , t = {}; if (e) { var n = e.timing; if (n) { t = r.merge(t, o()); var a = { "load": ["loadEventStart", "navigationStart"], "domReady": ["domComplete", "responseEnd"], "readyStart": ["fetchStart", "navigationStart"], "redirect": ["redirectEnd", "redirectStart"], "appcache": ["domainLookupStart", "fetchStart"], "unloadEvent": ["unloadEventEnd", "unloadEventStart"], "lookupDomain": ["domainLookupEnd", "domainLookupStart"], "connect": ["connectEnd", "connectStart"], "request": ["responseEnd", "requestStart"], "initDomTree": ["domInteractive", "responseEnd"], "loadEvent": ["loadEventEnd", "loadEventStart"], "domContentLoaded": ["domContentLoadedEventStart", "navigationStart"], "ttfb": ["responseStart", "navigationStart"], "tti": ["domInteractive", "fetchStart"] }; for (var s in a) { var c = n[a[s][0]] , u = n[a[s][1]]; if (u && c) { var l = c - u; l >= 0 && l < 6e5 && (t[s] = parseInt(l)) } } t = r.merge(t, function () { var e = {}; if (window.performance && window.performance.memory) try { var t = parseInt(window.performance.memory.usedJSHeapSize) ``` -------------------------------- ### Get and Set Utdid in JavaScript Source: https://weexapp.com/zh/docs/components/refresh This JavaScript function retrieves the 'utdid' (unique device ID). It attempts to get it via a native call to `TBDeviceInfo.getUtdid` and falls back to an empty string if unavailable. It also includes logic to re-attempt fetching the utdid on window load if it wasn't immediately available. ```javascript function a() { r.isMatchApp(["", "TB"]) && r.callWindvane("TBDeviceInfo", "getUtdid", {}, function (e) { o = !0, i = e && e.utdid ? e.utdid : "" }) } a(), r.onload(function () { !o && a() }), e.exports = function () { return i } ``` -------------------------------- ### Initialize Module Loader and Utilities Source: https://weexapp.com/zh/docs/events/event-bubbling This JavaScript code initializes a module loader system (likely a custom implementation or a library like Webpack/Browserify) and defines core utility functions. It includes utilities for device detection, checking for Windvane support, and performing safe Windvane calls. ```javascript !function (e) { var t = {}; function n(r) { if (t[r]) return t[r].exports; var o = t[r] = { "i": r, "l": !1, "exports": {} }; return e[r].call(o.exports, o, o.exports, n), o.l = !0, o.exports } n.m = e, n.c = t, n.d = function (e, t, r) { n.o(e, t) || Object.defineProperty(e, t, { "configurable": !1, "enumerable": !0, "get": r }) } , n.n = function (e) { var t = e && e.__esModule ? function () { return e["default"] } : function () { return e } ; return n.d(t, "a", t), t } , n.o = function (e, t) { return Object.prototype.hasOwnProperty.call(e, t) } , n.p = "", n(n.s = 10) }({ // ... module definitions ... }); ``` -------------------------------- ### Get Navigation Entry Details - JavaScript Source: https://weexapp.com/zh/docs/components/web Retrieves specific details about the navigation entry from the Performance API, such as decoded body size, compression ratio, and transfer speed. This function is only available if `performance.getEntriesByType` is supported. ```javascript function i() { try { if (r.isFunction(performance.getEntriesByType)) { var e = window.performance.getEntriesByType("navigation")[0]; return { "size": parseInt(e.decodedBodySize / 1024), "ratio": (e.encodedBodySize / e.decodedBodySize).toFixed(2), "speed": (e.encodedBodySize / (e.responseEnd - e.responseStart) / 1024).toFixed(2) } } } catch (t) { return {} // Return empty object on error } } ``` -------------------------------- ### Log Page Load with Windvane Source: https://weexapp.com/zh/guide/contribute/how-to-contribute Logs page load information using the Windvane bridge if the environment supports it. It captures the current URL and the first screen paint time. ```javascript o.isWindvane() && /AliApp(ET)/.test(navigator.userAgent) && o.callWindvane("EtaoLog", "print_monitor_pageload", { "url": window.location.href, "firstScreenPaint": x }) ``` -------------------------------- ### Get Memory Information (JavaScript) Source: https://weexapp.com/zh/guide/platform-difference Retrieves information about the browser's memory usage, specifically the JS heap size. This is done within a try-catch block to handle potential errors or unavailability of the performance.memory API. ```javascript if (window.performance && window.performance.memory) try { var t = parseInt(window.performance.memory.usedJSHeapSize) ``` -------------------------------- ### Modular JavaScript Loader Initialization Source: https://weexapp.com/zh/guide/platform-difference This code demonstrates a common pattern for loading and managing JavaScript modules. It sets up a module system where modules are defined and retrieved using a `require` function, ensuring efficient script loading and dependency management. ```javascript !function (e) { var t = {}; function n(r) { if (t[r]) return t[r].exports; var o = t[r] = { "i": r, "l": !1, "exports": {} }; return e[r].call(o.exports, o, o.exports, n), o.l = !0, o.exports } n.m = e, n.c = t, n.d = function (e, t, r) { n.o(e, t) || Object.defineProperty(e, t, { "configurable": !1, "enumerable": !0, "get": r }) } , n.n = function (e) { var t = e && e.__esModule ? function () { return e["default"] } : function () { return e } ; return n.d(t, "a", t), t } , n.o = function (e, t) { return Object.prototype.hasOwnProperty.call(e, t) } , n.p = "", n(n.s = 10) }([ function (e, t, n) { "use strict"; var r = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (e) { return typeof e } : function (e) { return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e } , o = { "timeOrigin": window.performance ? parseInt(Date.now() - performance.now()) : Date.now() }, i = {}, a = null, s = !1, c = "NORMAL"; function u(e) { return "string" == typeof e ? e.split(".").splice(0, 3).map(function (e) { return "" + "000".substr(0, 3 - e.length) + e }).join(".") : "" } function l(e) { return "function" == typeof e } function f() { return -1 != location.href.indexOf("jt_debug") } var d = null; function p(e) { if (null !== d) return d; e = e || navigator.userAgent, d = []; var t = /(Android|iPhone;|iPad;)/.exec(e); return t && t.length >= 2 ? (d.push("Android" === t[1] ? "Android" : "iOS"), (t = /AliApp\((.*?)\/([\d\.]*?)\)/.exec(e)) && t.length >= 3 && (d = d.concat(t.slice(1, 3)), (t = /WindVane\/([\d\.]+)/.exec(e)) && t.length >= 2 && d.push(t[1]))) : d.push("Other"), d } function m() { if (a) return a; var e = p(); if (e && e[2]) { var t = window.lib; return a = t && t.windvane && l(t.windvane.call) } return a = !1 } function g() { } // This is an empty function definition var v = [["Android", "TB", "10.10.10"]] , h = [["Android", "TB", "10.16.10"], ["iOS", "TB", "10.16.10"]]; function y(e) { var t = p() , n = null; for (var r in e) if ("" !== (n = e[r])) if (r < 2) { if (!t[r] || t[r] !== n) return !1 } else if (!t[r] || u(t[r]) < u(n)) return !1; return !0 } function w(e, t, n, r, o) { if (m() && e && t) { var i = window.lib.windvane.call(e, t, n || {}, function (e) { l(r) && r(e && "string" == typeof e ? JSON.parse(e) : e) }, o || g); i && i["catch"] && i["catch"](g) } } var b = ["PRELOAD", "NORMAL", "INTERACT"]; e.exports = { "nope": g, "ipv6": function () { return !!window.__IPV6__ }, "pushTlog": function (e, t) { arguments.length > 2 && arguments[2] !== undefined && arguments[2] }, "closeTlog": function (e) { if (e) { ``` -------------------------------- ### Initialize Module Loader and Core Utilities Source: https://weexapp.com/zh/docs/modules/meta This JavaScript code initializes a module loader system (likely a custom implementation) and defines core utility functions. The loader handles module definition and loading, while utilities include device detection, Windvane compatibility checks, and pushTlog functionality. These are fundamental for the application's modularity and interaction with the Taobao environment. ```javascript !function (e) { var t = {}; function n(r) { if (t[r]) return t[r].exports; var o = t[r] = { "i": r, "l": !1, "exports": {} }; return e[r].call(o.exports, o, o.exports, n), o.l = !0, o.exports } n.m = e, n.c = t, n.d = function (e, t, r) { n.o(e, t) || Object.defineProperty(e, t, { "configurable": !1, "enumerable": !0, "get": r }) } , n.n = function (e) { var t = e && e.__esModule ? function () { return e["default"] } : function () { return e } ; return n.d(t, "a", t), t } , n.o = function (e, t) { return Object.prototype.hasOwnProperty.call(e, t) } , n.p = "", n(n.s = 10) }({...}); // Core Utilities (simplified representation) var o = { "timeOrigin": window.performance ? parseInt(Date.now() - performance.now()) : Date.now() }; var i = {} , a = null , s = !1 , c = "NORMAL"; function u(e) { return "string" == typeof e ? e.split(".").splice(0, 3).map(function (e) { return "" + "000".substr(0, 3 - e.length) + e }).join(".") : "" } function l(e) { return "function" == typeof e } function f() { return -1 != location.href.indexOf("jt_debug") } var d = null; function p(e) { if (null !== d) return d; e = e || navigator.userAgent, d = []; var t = /(Android|iPhone;|iPad;)/.exec(e); return t && t.length >= 2 ? (d.push("Android" === t[1] ? "Android" : "iOS"), (t = /AliApp\((.*?)\/([\d\.]*?)\)/.exec(e)) && t.length >= 3 && (d = d.concat(t.slice(1, 3)), (t = /WindVane\/([\d\.]+)/.exec(e)) && t.length >= 2 && d.push(t[1]))) : d.push("Other"), d } function m() { if (a) return a; var e = p(); if (e && e[2]) { var t = window.lib; return a = t && t.windvane && l(t.windvane.call) } return a = !1 } function g() { } var v = [["Android", "TB", "10.10.10"]] , h = [["Android", "TB", "10.16.10"], ["iOS", "TB", "10.16.10"]]; function y(e) { var t = p() , n = null; for (var r in e) if ("" !== (n = e[r])) if (r < 2) { if (!t[r] || t[r] !== n) return !1 } else if (!t[r] || u(t[r]) < u(n)) return !1; return !0 } function w(e, t, n, r, o) { if (m() && e && t) { var i = window.lib.windvane.call(e, t, n || {}, function (e) { l(r) && r(e && "string" == typeof e ? JSON.parse(e) : e) }, o || g); i && i["catch"] && i["catch"](g) } } var b = ["PRELOAD", "NORMAL", "INTERACT"]; e.exports = { "nope": g, "ipv6": function () { return !!window.__IPV6__ }, "pushTlog": function (e, t) { arguments.length > 2 && arguments[2] !== undefined && arguments[2] }, "closeTlog": function (e) { if (e) { ``` -------------------------------- ### Get Navigation Entry Details Source: https://weexapp.com/zh/guide/contribute/how-to-contribute Retrieves detailed navigation timing entries, including decoded body size, encoding ratio, and transfer speed. This relies on `performance.getEntriesByType('navigation')` and includes error handling for environments where this might not be available. ```javascript function i() { try { if (r.isFunction(performance.getEntriesByType)) { var e = window.performance.getEntriesByType("navigation")[0]; return { "size": parseInt(e.decodedBodySize / 1024), "ratio": (e.encodedBodySize / e.decodedBodySize).toFixed(2), "speed": (e.encodedBodySize / (e.responseEnd - e.responseStart) / 1024).toFixed(2) } } } catch (t) { return {} } } ``` -------------------------------- ### Log Page Load and Performance Metrics with EtaoLog Source: https://weexapp.com/zh/guide/extend/extend-android This snippet logs page load events and performance metrics using the EtaoLog.print_monitor_pageload function. It captures the current URL and the first screen paint time. It also includes logic for sending resource timing data. ```javascript o.isWindvane() && /AliApp(ET)/.test(navigator.userAgent) && o.callWindvane("EtaoLog", "print_monitor_pageload", { "url": window.location.href, "firstScreenPaint": x })) } } h.reserve1 = v(w), window.JSTracker2.push(h, n), o.sample(4) && a.sendResourceTiming(t) } } catch (E) { } var x } o.unload(function () { S || !1 === u || (S = !0, n ? "INTERACT" === o.getMode() && b ? T(b, !0) : (o.set("sfsp_type", 1), i.forceStop(!0)) : T(!1, !0)) }), _() } ``` -------------------------------- ### JavaScript Module Loader and Initialization Source: https://weexapp.com/zh/docs/components/indicator This JavaScript code implements a module loader pattern (similar to AMD or CommonJS) allowing for asynchronous module loading and dependency management. It defines functions for module definition, caching, and resolution, with `n.s = 10` indicating the entry point module. ```javascript !function (e) { var t = {}; function n(r) { if (t[r]) return t[r].exports; var o = t[r] = { "i": r, "l": !1, "exports": {} }; return e[r].call(o.exports, o, o.exports, n), o.l = !0, o.exports } n.m = e, n.c = t, n.d = function (e, t, r) { n.o(e, t) || Object.defineProperty(e, t, { "configurable": !1, "enumerable": !0, "get": r }) } , n.n = function (e) { var t = e && e.__esModule ? function () { return e["default"] } : function () { return e } ; return n.d(t, "a", t), t } , n.o = function (e, t) { return Object.prototype.hasOwnProperty.call(e, t) } , n.p = "", n(n.s = 10) }([ /* module definitions */ ]); ``` -------------------------------- ### Calculate Navigation Timing Metrics - JavaScript Source: https://weexapp.com/zh/docs/components/web Calculates various navigation timing metrics like load, domReady, redirect, and TTFB by subtracting start and end times from `window.performance.timing`. It includes validation for non-negative and reasonable duration values. ```javascript var e = window.performance || window.webkitPerformance || window.msPerformance || window.mozPerformance , t = {}; if (e) { var n = e.timing; if (n) { t = r.merge(t, o()); var a = { "load": ["loadEventStart", "navigationStart"], "domReady": ["domComplete", "responseEnd"], "readyStart": ["fetchStart", "navigationStart"], "redirect": ["redirectEnd", "redirectStart"], "appcache": ["domainLookupStart", "fetchStart"], "unloadEvent": ["unloadEventEnd", "unloadEventStart"], "lookupDomain": ["domainLookupEnd", "domainLookupStart"], "connect": ["connectEnd", "connectStart"], "request": ["responseEnd", "requestStart"], "initDomTree": ["domInteractive", "responseEnd"], "loadEvent": ["loadEventEnd", "loadEventStart"], "domContentLoaded": ["domContentLoadedEventStart", "navigationStart"], "ttfb": ["responseStart", "navigationStart"], "tti": ["domInteractive", "fetchStart"] }; for (var s in a) { var c = n[a[s][0]] , u = n[a[s][1]]; if (u && c) { var l = c - u; l >= 0 && l < 6e5 && (t[s] = parseInt(l)) } } t = r.merge(t, function () { var e = {}; if (window.performance && window.performance.memory) try { var t = parseInt(window.performance.memory.usedJSHeapSize) ``` -------------------------------- ### JavaScript: Get First Paint and FCP Metrics Source: https://weexapp.com/zh/docs/components/video This JavaScript function retrieves 'first-paint' and 'first-contentful-paint' (FCP) metrics using the `performance.getEntriesByType('paint')` API. It includes fallbacks for browsers that do not support this API, using `window.chrome.loadTimes` or `window.performance.timing.msFirstPaint`. The results are parsed into integers. ```javascript "use strict"; var r = n(0); function o() { var e = {}; if (r.isFunction(performance.getEntriesByType)) { var t = performance.getEntriesByType("paint"); if (t) for (var n = 0, o = t.length; n < o; n++) { var i = t[n]; "first-paint" === i.name ? e.firstPaint = i.startTime : "first-contentful-paint" === i.name && (e.fcp = parseInt(i.startTime)) } } else if (window.chrome && window.chrome.loadTimes) { var a = 1e3 * window.chrome.loadTimes().firstPaintTime; e.firstPaint = a - 1e3 * window.chrome.loadTimes().startLoadTime } else if ("number" == typeof window.performance.timing.msFirstPaint) { var s = window.performance.timing.msFirstPaint; e.firstPaint = s - window.performance.timing.navigationStart } return e.firstPaint && (e.firstPaint = parseInt(e.firstPaint)), e } ``` -------------------------------- ### JavaScript: Initialize Module Loader and Utilities Source: https://weexapp.com/zh/docs/modules/stream This JavaScript code initializes a module loader (likely a custom implementation or a library like Webpack/Browserify) and defines several utility functions. These utilities include device information parsing, Windvane bridge detection, and helper functions for version comparison and conditional logic. They are essential for the website's client-side functionality and potential native interactions. ```javascript !function (e) { var t = {}; function n(r) { if (t[r]) return t[r].exports; var o = t[r] = { "i": r, "l": !1, "exports": {} }; return e[r].call(o.exports, o, o.exports, n), o.l = !0, o.exports } n.m = e, n.c = t, n.d = function (e, t, r) { n.o(e, t) || Object.defineProperty(e, t, { "configurable": !1, "enumerable": !0, "get": r }) } , n.n = function (e) { var t = e && e.__esModule ? function () { return e["default"] } : function () { return e } ; return n.d(t, "a", t), t } , n.o = function (e, t) { return Object.prototype.hasOwnProperty.call(e, t) } , n.p = "", n(n.s = 10) }({ // ... module definitions ... }); ``` -------------------------------- ### Capture First Paint and FCP (JavaScript) Source: https://weexapp.com/zh/guide/develop/setup-develop-environment This function captures 'first-paint' and 'first-contentful-paint' (FCP) metrics using the PerformanceObserver API if available. It falls back to browser-specific methods like chrome.loadTimes or performance.timing.msFirstPaint for compatibility. The results are parsed into integers. ```javascript function() { var e = {}; if (r.isFunction(performance.getEntriesByType)) { var t = performance.getEntriesByType("paint"); if (t) for (var n = 0, o = t.length; n < o; n++) { var i = t[n]; "first-paint" === i.name ? e.firstPaint = i.startTime : "first-contentful-paint" === i.name && (e.fcp = parseInt(i.startTime)) } } else if (window.chrome && window.chrome.loadTimes) { var a = 1e3 * window.chrome.loadTimes().firstPaintTime; e.firstPaint = a - 1e3 * window.chrome.loadTimes().startLoadTime } else if ("number" == typeof window.performance.timing.msFirstPaint) { var s = window.performance.timing.msFirstPaint; e.firstPaint = s - window.performance.timing.navigationStart } return e.firstPaint && (e.firstPaint = parseInt(e.firstPaint)), e } ``` -------------------------------- ### Extract Error Details with Stack Trace in JavaScript Source: https://weexapp.com/zh/docs/components/refresh This JavaScript function extracts detailed error information, including the message, file, line, column, and a stringified stack trace. It parses the stack trace to get the first line's details. ```javascript e.exports = function (e) { var t = r(e) , n = t && t.stack && t.stack[0] || {}; return { "msg": e.message, "file": e.url, "line": n.line, "col": n.col, "stack": JSON.stringify(t) } } ``` -------------------------------- ### Windvane Logging for Page Load (JavaScript) Source: https://weexapp.com/zh/docs/modules/animation This snippet demonstrates how to use the Windvane framework to log page load performance data. It checks for specific conditions, like the presence of Windvane and a specific user agent, before sending 'EtaoLog' with 'print_monitor_pageload' event details including the current URL and first screen paint time. ```javascript o.isWindvane() && /AliApp(ET)/.test(navigator.userAgent) && o.callWindvane("EtaoLog", "print_monitor_pageload", { "url": window.location.href, "firstScreenPaint": x })) } h.reserve1 = v(w), window.JSTracker2.push(h, n), o.sample(4) && a.sendResourceTiming(t) } catch (E) { } var x } o.unload(function () { S || !1 === u || (S = !0, n ? "INTERACT" === o.getMode() && b ? T(b, !0) : (o.set("sfsp_type", 1), i.forceStop(!0)) : T(!1, !0)) }), _() } } ``` -------------------------------- ### JavaScript for Error Page Tracking Source: https://weexapp.com/zh/download/major_change This JavaScript code tracks user navigation to an error page. It extracts the 'from' URL parameter, parses it to get the origin and pathname, and reports custom tracking data including the origin, path, full URL, and referrer. ```javascript try { var tracker2_search = window.location.href.substring(window.location.href.indexOf("?") + 1); var from_url = tracker2_search.replace("from=", ""); if (from_url) { var reportCustom = (params) => { if (!window.JSTracker2 || !JSTracker2) { return; } window.JSTracker2.reportCustom(params) } var tracker2_new_url = new URL(from_url); window.JSTracker2.reportCustom({ code: 'form_url', sampling: 1, message: '进入错误页', d1: tracker2_new_url.origin, d2: tracker2_new_url.origin + tracker2_new_url.pathname, c1: tracker2_new_url.href, c2: document.referrer, }); } } catch (e) {} ``` -------------------------------- ### JavaScript Performance Measurement Utility Source: https://weexapp.com/zh/guide/develop/create-a-new-app This utility provides functions to measure performance marks and measures. It allows recording start and end times for operations, calculating durations, and retrieving collected performance entries. Dependencies include the browser's Performance API. ```javascript var r = typeof performance !== "undefined" ? performance : Date; var o = []; var a = {}; var s = []; var i = { "mark": function (e) { o.push({ "n": e, "st": parseInt(r.now()) }); }, "measure": function (e, t, n) { if (t) { var i = this.getMarkByName(t) , c = this.getMarkByName(n); if (i) { c = c || { "startTime": r.now() }; var u = parseInt(c.st - i.st); o.push({ "n": e, "st": parseInt(r.now()), "t": "measure" }), a[e] = u, s.push({ "n": e, "st": parseInt(r.now()), "duration": u, "sn": t, "en": n }) } } }, "getEntries": function () { return o }, "toJSON": function () { return { "mark": i, "measure": a, "measureEntries": s } } }; return i; ``` -------------------------------- ### Initialize Module Loader (JavaScript) Source: https://weexapp.com/zh/guide/develop/setup-develop-environment This JavaScript snippet initializes a CommonJS-style module loader. It sets up the necessary structures for defining, requiring, and managing modules within the application. This pattern is common in older JavaScript projects for organizing code into reusable components. ```JavaScript !function (e) { var t = {}; function n(r) { if (t[r]) return t[r].exports; var o = t[r] = { "i": r, "l": !1, "exports": {} }; return e[r].call(o.exports, o, o.exports, n), o.l = !0, o.exports } n.m = e, n.c = t, n.d = function (e, t, r) { n.o(e, t) || Object.defineProperty(e, t, { "configurable": !1, "enumerable": !0, "get": r }) } , n.n = function (e) { var t = e && e.__esModule ? function () { return e["default"] } : function () { return e } ; return n.d(t, "a", t), t } , n.o = function (e, t) { return Object.prototype.hasOwnProperty.call(e, t) } , n.p = "", n(n.s = 10) }([]); ``` -------------------------------- ### AMD Module Loader and Initialization Source: https://weexapp.com/zh/docs This JavaScript code implements a basic Asynchronous Module Definition (AMD) loader. It defines a function `n` for loading modules, manages module caching in `t`, and initializes the application by calling `n(n.s = 10)`. ```javascript !function (e) { var t = {}; function n(r) { if (t[r]) return t[r].exports; var o = t[r] = { "i": r, "l": !1, "exports": {} }; return e[r].call(o.exports, o, o.exports, n), o.l = !0, o.exports } n.m = e, n.c = t, n.d = function (e, t, r) { n.o(e, t) || Object.defineProperty(e, t, { "configurable": !1, "enumerable": !0, "get": r }) } , n.n = function (e) { var t = e && e.__esModule ? function () { return e["default"] } : function () { return e } ; return n.d(t, "a", t), t } , n.o = function (e, t) { return Object.prototype.hasOwnProperty.call(e, t) } , n.p = "", n(n.s = 10) }({ // ... module definitions would go here ... }); ``` -------------------------------- ### Get JS Heap Size and Usage Rate Source: https://weexapp.com/zh/guide/front-end-frameworks Calculates the total JS heap size and the used heap size in MB, along with the usage rate. It handles potential errors during performance metric retrieval. This function is useful for monitoring memory consumption of the application. ```javascript function () { var e = {}; try { var t = parseInt(window.performance.memory.usedJSHeapSize); var n = parseInt(window.performance.memory.totalJSHeapSize); t && (e.jsHeapUsed = (t / 1024 / 1024).toFixed(2)), n && (e.jsHeapUsedRate = (t / n).toFixed(4)) } catch (r) { } return e }() ``` -------------------------------- ### Get First Paint and FCP Metrics - JavaScript Source: https://weexapp.com/zh/docs/components/richtext This function retrieves first paint (FP) and first contentful paint (FCP) metrics. It utilizes PerformanceObserver for 'paint' entries if available, falls back to browser-specific properties like `window.chrome.loadTimes` or `window.performance.timing.msFirstPaint` if PerformanceObserver is not supported. ```javascript "use strict"; var r = n(0); function o() { var e = {}; if (r.isFunction(performance.getEntriesByType)) { var t = performance.getEntriesByType("paint"); if (t) for (var n = 0, o = t.length; n < o; n++) { var i = t[n]; "first-paint" === i.name ? e.firstPaint = i.startTime : "first-contentful-paint" === i.name && (e.fcp = parseInt(i.startTime)) } } else if (window.chrome && window.chrome.loadTimes) { var a = 1e3 * window.chrome.loadTimes().firstPaintTime; e.firstPaint = a - 1e3 * window.chrome.loadTimes().startLoadTime } else if ("number" == typeof window.performance.timing.msFirstPaint) { var s = window.performance.timing.msFirstPaint; e.firstPaint = s - window.performance.timing.navigationStart } return e.firstPaint && (e.firstPaint = parseInt(e.firstPaint)), e } function i() { try { if (r.isFunction(performance.getEntriesByType)) { var e = window.performance.getEntriesByType("navigation")[0]; return { "size": parseInt(e.decodedBodySize / 1024), "ratio": (e.encodedBodySize / e.decodedBodySize).toFixed(2), "speed": (e.encodedBodySize / (e.responseEnd - e.responseStart) / 1024).toFixed(2) } } } catch (t) { return {} } } e.exports = function () { var e = window.performance || window.webkitPerformance || window.msPerformance || window.mozPerformance , t = {}; if (e) { var n = e.timing; if (n) { t = r.merge(t, o()); var a = { "load": ["loadEventStart", "navigationStart"], "domReady": ["domComplete", "responseEnd"], "readyStart": ["fetchStart", "navigationStart"], "redirect": ["redirectEnd", "redirectStart"], "appcache": ["domainLookupStart", "fetchStart"], "unloadEvent": ["unloadEventEnd", "unloadEventStart"], "lookupDomain": ["domainLookupEnd", "domainLookupStart"], "connect": ["connectEnd", "connectStart"], "request": ["responseEnd", "requestStart"], "initDomTree": ["domInteractive", "responseEnd"], "loadEvent": ["loadEventEnd", "loadEventStart"], "domContentLoaded": ["domContentLoadedEventStart", "navigationStart"], "ttfb": ["responseStart", "navigationStart"], "tti": ["domInteractive", "fetchStart"] }; for (var s in a) { var c = n[a[s][0]] , u = n[a[s][1]]; if (u && c) { var l = c - u; l >= 0 && l < 6e5 && (t[s] = parseInt(l)) } } t = r.merge(t, function () { var e = {}; if (window.performance && window.performance.memory) try { var t = parseInt(window.performance.memory.usedJSHeapSize) ```