Source code
Revision control
Copy as Markdown
Other Tools
<!DOCTYPE html>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/speculation-rules/prerender/resources/utils.js"></script>
<body>
<script>
async function main() {
// Ensure this page is running in a prerendering state.
if (!document.prerendering) {
const testChannel = new PrerenderChannel('test-channel');
testChannel.postMessage({error: 'Page is not prerendering'});
testChannel.close();
return;
}
const iframe_uid = token();
const iframeChannel = new PrerenderChannel('iframe-channel', iframe_uid);
const params = new URLSearchParams(location.search);
const iframeOrigin = params.get('iframe_origin') ||
get_host_info()['HTTPS_REMOTE_ORIGIN'];
const crossOriginUrl = new URL(
`scroll-into-view-iframe-src.html?uid=${iframe_uid}`,
iframeOrigin + window.location.pathname);
const testChannel = new PrerenderChannel('test-channel');
// Make the page tall so the iframe is positioned below the fold.
document.body.style.height = '10000px';
const spacer = document.createElement('div');
spacer.style.height = '5000px';
document.body.appendChild(spacer);
// Create an iframe below the fold.
const iframe = document.createElement('iframe');
iframe.style.height = '200px';
iframe.style.width = '100%';
iframe.src = crossOriginUrl.href;
document.body.appendChild(iframe);
// Wait for the iframe to report that focus is done.
const iframeResult = await new Promise(resolve => {
iframeChannel.addEventListener('message', e => {
resolve(e.data);
}, {once: true});
});
// Report results after activation.
document.addEventListener('prerenderingchange', () => {
const nav = performance.getEntriesByType('navigation')[0];
testChannel.postMessage({
iframeLoadedDuringPrerendering: iframeResult.prerendering,
activeElementUpdated: iframeResult.activeElementUpdated,
hasFocus: iframeResult.hasFocus,
iframeScrollTop: iframeResult.iframeScrollTop,
prerenderActivated: nav && nav.activationStart > 0
});
testChannel.close();
});
// Signal ready for activation.
const prerenderChannel = new PrerenderChannel('prerender-channel');
prerenderChannel.postMessage('readyToActivate');
prerenderChannel.close();
new PrerenderChannel('close').addEventListener('message', () => {
iframeChannel.close();
window.close();
});
}
const params = new URLSearchParams(location.search);
if (!params.has('prerendering')) {
const rule_extras = {'target_hint': getTargetHint()};
loadInitiatorPage(rule_extras);
} else {
main();
}
</script>
</body>