Create a gist now

Instantly share code, notes, and snippets.

Embed
ResourceTiming leak to parent frames
//
// Placed in any cross-origin IFRAMEs
//
if (window !== window.top) {
if (typeof window.PerformanceObserver !== "function") {
return;
}
// Listen for all ResourceTimings, repeating them to the parent window
var observer = new PerformanceObserver(function(entries) {
window.top.postMessage({
type: "resourcetiming",
entries: JSON.stringify(entries.getEntries()),
origin: window.performance.timeOrigin || window.performance.timing.navigationStart
}, "*");
});
observer.observe({entryTypes: ['resource']});
// repeat any sub-frame's ResourceTiming data
window.addEventListener("message", function(event) {
if (event.data && event.data.type === "resourcetiming") {
window.top.postMessage(event.data, "*");
}
}, false);
}
//
// Placed in the top-level window
//
// list of all ResourceTimings from cross-origin IFRAMEs
var iframeEntries = [];
window.addEventListener("message", function(event) {
if (event.data && event.data.type === "resourcetiming") {
// RT entries are JSON.stringify'd
var entries = JSON.parse(event.data.entries);
var myOrigin = window.performance.timeOrigin || window.performance.timing.navigationStart;
// update each entry's startTime by the offset of the two frame's time origins
for (var i = 0; i < entries.length; i++) {
entries[i].startTime += (event.data.origin - myOrigin);
}
Array.prototype.push.apply(iframeEntries, entries);
}
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment