Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core(artifacts): rename Scripts to ScriptElements artifact (#7920)
- Loading branch information
1 parent
a76e7f2
commit 7a43125
Showing
10 changed files
with
197 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* @license Copyright 2017 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const Gatherer = require('./gatherer.js'); | ||
const NetworkAnalyzer = require('../../lib/dependency-graph/simulator/network-analyzer.js'); | ||
const NetworkRequest = require('../../lib/network-request.js'); | ||
const getElementsInDocumentString = require('../../lib/page-functions.js').getElementsInDocumentString; // eslint-disable-line max-len | ||
|
||
/** | ||
* @return {LH.Artifacts['ScriptElements']} | ||
*/ | ||
/* istanbul ignore next */ | ||
function collectAllScriptElements() { | ||
/** @type {HTMLScriptElement[]} */ | ||
// @ts-ignore - getElementsInDocument put into scope via stringification | ||
const scripts = getElementsInDocument('script'); // eslint-disable-line no-undef | ||
|
||
return scripts.map(script => { | ||
return { | ||
type: script.type || null, | ||
src: script.src || null, | ||
async: script.async, | ||
defer: script.defer, | ||
source: /** @type {'head'|'body'} */ (script.closest('head') ? 'head' : 'body'), | ||
content: script.src ? null : script.text, | ||
requestId: null, | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* @fileoverview Gets JavaScript file contents. | ||
*/ | ||
class ScriptElements extends Gatherer { | ||
/** | ||
* @param {LH.Gatherer.PassContext} passContext | ||
* @param {LH.Gatherer.LoadData} loadData | ||
* @return {Promise<LH.Artifacts['ScriptElements']>} | ||
*/ | ||
async afterPass(passContext, loadData) { | ||
const driver = passContext.driver; | ||
const mainResource = NetworkAnalyzer.findMainDocument(loadData.networkRecords, passContext.url); | ||
|
||
/** @type {LH.Artifacts['ScriptElements']} */ | ||
const scripts = await driver.evaluateAsync(`(() => { | ||
${getElementsInDocumentString} | ||
return (${collectAllScriptElements.toString()})(); | ||
})()`, {useIsolation: true}); | ||
|
||
for (const script of scripts) { | ||
if (script.content) script.requestId = mainResource.requestId; | ||
} | ||
|
||
const scriptRecords = loadData.networkRecords | ||
// Ignore records from OOPIFs | ||
.filter(record => !record.sessionId) | ||
// Only get the content of script requests | ||
.filter(record => record.resourceType === NetworkRequest.TYPES.Script); | ||
|
||
for (const record of scriptRecords) { | ||
try { | ||
const content = await driver.getRequestContent(record.requestId); | ||
if (!content) continue; | ||
|
||
const matchedScriptElement = scripts.find(script => script.src === record.url); | ||
if (matchedScriptElement) { | ||
matchedScriptElement.requestId = record.requestId; | ||
matchedScriptElement.content = content; | ||
} else { | ||
scripts.push({ | ||
type: null, | ||
src: record.url, | ||
async: false, | ||
defer: false, | ||
source: 'network', | ||
requestId: record.requestId, | ||
content, | ||
}); | ||
} | ||
} catch (e) {} | ||
} | ||
|
||
return scripts; | ||
} | ||
} | ||
|
||
module.exports = ScriptElements; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.