diff --git a/dist/performance.js b/dist/performance.js index eaab120..2696a08 100644 --- a/dist/performance.js +++ b/dist/performance.js @@ -24,7 +24,7 @@ function getLcpElement() { resolve(naiveLcpEntry); }).observe({ type: "largest-contentful-paint", buffered: true }); }).then(({ startTime, element, url, size, loadTime, renderTime }) => { - const cover90viewport = doesElementCoverPercentageOfViewport(element, 90); + const cover90viewport = doesElementCoverPercentageOfViewport(element, 0.9); const attributes = getAttributes(element); const styles = getAllStyles(element, ['background-image', 'pointer-events', 'position', 'width', 'height']); return { @@ -39,6 +39,7 @@ function getLcpElement() { naturalWidth: element?.naturalWidth, naturalHeight: element?.naturalHeight, styles, + percentOfViewport: getPercentOfViewport(element), cover90viewport }; }); @@ -66,29 +67,8 @@ function getComputedStyles(element, properties) { return Object.fromEntries(properties.map(prop => ([prop, styles.getPropertyValue(prop)]))); } -function getInlineStyles (element, properties) { - if (!element) { - return null; - } - - const styles = element.style; - return Object.fromEntries(properties.map(prop => ([prop, styles.getPropertyValue(prop)]))); -} - -// Merge Inline styles with Computed styles. -// Inline has higher specificty, unless '!important' exists in computed styles. function getAllStyles(element, properties) { - const inlineStyles = getInlineStyles(element, properties); - const computedStyles = getComputedStyles(element, properties); - const allStyles = {}; - for (const styleName in inlineStyles) { - if (!inlineStyles[styleName].includes('!important') && computedStyles.hasOwnProperty(styleName) && computedStyles[styleName].includes('!important')) { - allStyles[styleName] = computedStyles[styleName]; - } else { - allStyles[styleName] = inlineStyles[styleName]; - } - } - return allStyles; + return getComputedStyles(element, properties); } function summarizeElement(element) { @@ -188,7 +168,7 @@ function getGamingMetrics(rawDoc, lcp_elem_stats) { } } - returnObj['fidIframeOverlaySoft'] = doesElementCoverPercentageOfViewport(iframeElement, 90); + returnObj['fidIframeOverlaySoft'] = doesElementCoverPercentageOfViewport(iframeElement, 0.9); }); @@ -207,16 +187,14 @@ function getGamingMetrics(rawDoc, lcp_elem_stats) { return returnObj; } -// Source: https://stackoverflow.com/questions/57786082/determine-how-much-of-the-viewport-is-covered-by-element-intersectionobserver -// percentage is a whole number (ex: 90, not .9) function doesElementCoverPercentageOfViewport(element, percentage) { - const elementBCR = element.getBoundingClientRect(); - const percentOfViewport = ((elementBCR.width * elementBCR.height) * calcOcclusion(elementBCR)) / ((window.innerWidth * window.innerHeight) / 100); + return getPercentOfViewport(element) > percentage; +} - if (percentOfViewport > percentage) { - return true; - } - return false; +// Source: https://stackoverflow.com/questions/57786082/determine-how-much-of-the-viewport-is-covered-by-element-intersectionobserver +function getPercentOfViewport(element) { + const elementBCR = element.getBoundingClientRect(); + return ((elementBCR.width * elementBCR.height * calcOcclusion(elementBCR)) / (window.innerWidth * window.innerHeight)).toPrecision(3); } // Calculate Element : Viewport Intersection ratio without Intersection Observer