use frameswap timestamp for finding screenshot

This commit is contained in:
Simon Knott 2024-09-09 10:53:36 +02:00
parent 718bd9b35f
commit 909a47cdbf
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
8 changed files with 19 additions and 7 deletions

View file

@ -137,6 +137,7 @@ export class Snapshotter {
html: data.html, html: data.html,
viewport: data.viewport, viewport: data.viewport,
timestamp: monotonicTime(), timestamp: monotonicTime(),
absoluteTimestamp: data.absoluteTimestamp,
collectionTime: data.collectionTime, collectionTime: data.collectionTime,
resourceOverrides: [], resourceOverrides: [],
isMainFrame: page.mainFrame() === frame isMainFrame: page.mainFrame() === frame

View file

@ -28,6 +28,7 @@ export type SnapshotData = {
viewport: { width: number, height: number }, viewport: { width: number, height: number },
url: string, url: string,
timestamp: number, timestamp: number,
absoluteTimestamp: number, // milliseconds since epoch
collectionTime: number, collectionTime: number,
}; };
@ -573,6 +574,7 @@ export function frameSnapshotStreamer(snapshotStreamer: string, removeNoScript:
}, },
url: location.href, url: location.href,
timestamp, timestamp,
absoluteTimestamp: Date.now(),
collectionTime: 0, collectionTime: 0,
}; };

View file

@ -472,7 +472,8 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
sha1, sha1,
width: params.width, width: params.width,
height: params.height, height: params.height,
timestamp: monotonicTime() timestamp: monotonicTime(),
frameSwapTimestamp: params.timestamp ? params.timestamp * 1000 : undefined,
}; };
// Make sure to write the screencast frame before adding a reference to it. // Make sure to write the screencast frame before adding a reference to it.
this._appendResource(sha1, params.buffer); this._appendResource(sha1, params.buffer);

View file

@ -45,6 +45,7 @@ export type PageEntry = {
screencastFrames: { screencastFrames: {
sha1: string, sha1: string,
timestamp: number, timestamp: number,
frameSwapTimestamp?: number,
width: number, width: number,
height: number, height: number,
}[]; }[];

View file

@ -46,6 +46,7 @@ export class SnapshotServer {
viewport: snapshot.viewport(), viewport: snapshot.viewport(),
url: snapshot.snapshot().frameUrl, url: snapshot.snapshot().frameUrl,
timestamp: snapshot.snapshot().timestamp, timestamp: snapshot.snapshot().timestamp,
absoluteTimestamp: snapshot.snapshot().absoluteTimestamp,
} : { } : {
error: 'No snapshot found' error: 'No snapshot found'
}); });

View file

@ -30,12 +30,12 @@ import { locatorOrSelectorAsSelector } from '@isomorphic/locatorParser';
import { TabbedPaneTab } from '@web/components/tabbedPane'; import { TabbedPaneTab } from '@web/components/tabbedPane';
import { BrowserFrame } from './browserFrame'; import { BrowserFrame } from './browserFrame';
function findClosest<T extends { timestamp: number }>(items: T[], target: number) { function findClosest<T>(items: T[], metric: (v: T) => number, target: number) {
return items.find((item, index) => { return items.find((item, index) => {
if (index === items.length - 1) if (index === items.length - 1)
return true; return true;
const next = items[index + 1]; const next = items[index + 1];
return Math.abs(item.timestamp - target) < Math.abs(next.timestamp - target); return Math.abs(metric(item) - target) < Math.abs(metric(next) - target);
}); });
} }
@ -101,7 +101,7 @@ export const SnapshotTab: React.FunctionComponent<{
const iframeRef0 = React.useRef<HTMLIFrameElement>(null); const iframeRef0 = React.useRef<HTMLIFrameElement>(null);
const iframeRef1 = React.useRef<HTMLIFrameElement>(null); const iframeRef1 = React.useRef<HTMLIFrameElement>(null);
const [snapshotInfo, setSnapshotInfo] = React.useState<{ viewport: typeof kDefaultViewport, url: string, timestamp?: number }>({ viewport: kDefaultViewport, url: '', timestamp: undefined }); const [snapshotInfo, setSnapshotInfo] = React.useState<{ viewport: typeof kDefaultViewport, url: string, timestamp?: number, absoluteTimestamp?: undefined }>({ viewport: kDefaultViewport, url: '' });
const loadingRef = React.useRef({ iteration: 0, visibleIframe: 0 }); const loadingRef = React.useRef({ iteration: 0, visibleIframe: 0 });
React.useEffect(() => { React.useEffect(() => {
@ -110,7 +110,7 @@ export const SnapshotTab: React.FunctionComponent<{
const newVisibleIframe = 1 - loadingRef.current.visibleIframe; const newVisibleIframe = 1 - loadingRef.current.visibleIframe;
loadingRef.current.iteration = thisIteration; loadingRef.current.iteration = thisIteration;
const newSnapshotInfo = { url: '', viewport: kDefaultViewport, timestamp: undefined }; const newSnapshotInfo = { url: '', viewport: kDefaultViewport, timestamp: undefined, absoluteTimestamp: undefined };
if (snapshotInfoUrl) { if (snapshotInfoUrl) {
const response = await fetch(snapshotInfoUrl); const response = await fetch(snapshotInfoUrl);
const info = await response.json(); const info = await response.json();
@ -118,6 +118,7 @@ export const SnapshotTab: React.FunctionComponent<{
newSnapshotInfo.url = info.url; newSnapshotInfo.url = info.url;
newSnapshotInfo.viewport = info.viewport; newSnapshotInfo.viewport = info.viewport;
newSnapshotInfo.timestamp = info.timestamp; newSnapshotInfo.timestamp = info.timestamp;
newSnapshotInfo.absoluteTimestamp = info.absoluteTimestamp;
} }
} }
@ -169,10 +170,13 @@ export const SnapshotTab: React.FunctionComponent<{
const page = action ? pageForAction(action) : undefined; const page = action ? pageForAction(action) : undefined;
const screencastFrame = React.useMemo( const screencastFrame = React.useMemo(
() => { () => {
if (snapshotInfo.absoluteTimestamp && page?.screencastFrames[0]?.frameSwapTimestamp)
return findClosest(page.screencastFrames, frame => frame.frameSwapTimestamp!, snapshotInfo.absoluteTimestamp);
if (snapshotInfo.timestamp && page?.screencastFrames) if (snapshotInfo.timestamp && page?.screencastFrames)
return findClosest(page.screencastFrames, snapshotInfo.timestamp); return findClosest(page.screencastFrames, frame => frame.timestamp, snapshotInfo.timestamp);
}, },
[page?.screencastFrames, snapshotInfo.timestamp] [page?.screencastFrames, snapshotInfo.timestamp, snapshotInfo.absoluteTimestamp]
); );
return <div return <div

View file

@ -44,6 +44,7 @@ export type FrameSnapshot = {
frameId: string, frameId: string,
frameUrl: string, frameUrl: string,
timestamp: number, timestamp: number,
absoluteTimestamp: number,
collectionTime: number, collectionTime: number,
doctype?: string, doctype?: string,
html: NodeSnapshot, html: NodeSnapshot,

View file

@ -53,6 +53,7 @@ export type ScreencastFrameTraceEvent = {
width: number, width: number,
height: number, height: number,
timestamp: number, timestamp: number,
frameSwapTimestamp?: number, // milliseconds since epoch
}; };
export type BeforeActionTraceEvent = { export type BeforeActionTraceEvent = {