-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
document-urls.js
54 lines (45 loc) · 1.99 KB
/
document-urls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as Lantern from '../lib/lantern/lantern.js';
import {makeComputedArtifact} from './computed-artifact.js';
import {NetworkRecords} from './network-records.js';
import {ProcessedTrace} from './processed-trace.js';
/**
* @fileoverview Compute the navigation specific URLs `requestedUrl` and `mainDocumentUrl` in situations where
* the `URL` artifact is not present. This is not a drop-in replacement for `URL` but can be helpful in situations
* where getting the `URL` artifact is difficult.
*/
class DocumentUrls {
/**
* @param {{trace: LH.Trace, devtoolsLog: LH.DevtoolsLog}} data
* @param {LH.Artifacts.ComputedContext} context
* @return {Promise<{requestedUrl: string, mainDocumentUrl: string}>}
*/
static async compute_(data, context) {
const processedTrace = await ProcessedTrace.request(data.trace, context);
const networkRecords = await NetworkRecords.request(data.devtoolsLog, context);
const mainFrameId = processedTrace.mainFrameInfo.frameId;
/** @type {string|undefined} */
let requestedUrl;
/** @type {string|undefined} */
let mainDocumentUrl;
for (const event of data.devtoolsLog) {
if (event.method === 'Page.frameNavigated' && event.params.frame.id === mainFrameId) {
const {url} = event.params.frame;
// Only set requestedUrl on the first main frame navigation.
if (!requestedUrl) requestedUrl = url;
mainDocumentUrl = url;
}
}
if (!requestedUrl || !mainDocumentUrl) throw new Error('No main frame navigations found');
const initialRequest =
Lantern.Core.NetworkAnalyzer.findResourceForUrl(networkRecords, requestedUrl);
if (initialRequest?.redirects?.length) requestedUrl = initialRequest.redirects[0].url;
return {requestedUrl, mainDocumentUrl};
}
}
const DocumentUrlsComputed = makeComputedArtifact(DocumentUrls, ['devtoolsLog', 'trace']);
export {DocumentUrlsComputed as DocumentUrls};