Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test LCP viewport stat #103

Merged
merged 7 commits into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions dist/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -39,6 +39,7 @@ function getLcpElement() {
naturalWidth: element?.naturalWidth,
naturalHeight: element?.naturalHeight,
styles,
percentOfViewport: getPercentOfViewport(element),
cover90viewport
};
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -188,7 +168,7 @@ function getGamingMetrics(rawDoc, lcp_elem_stats) {
}
}

returnObj['fidIframeOverlaySoft'] = doesElementCoverPercentageOfViewport(iframeElement, 90);
returnObj['fidIframeOverlaySoft'] = doesElementCoverPercentageOfViewport(iframeElement, 0.9);

});

Expand All @@ -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
Expand Down
Loading