Skip to content

Commit 5d61156

Browse files
committed
core: append sourceURL comment to eval code
1 parent af07cc3 commit 5d61156

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

lighthouse-core/gather/driver/execution-context.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ class ExecutionContext {
9797
.catch(${pageFunctions.wrapRuntimeEvalErrorInBrowserString})
9898
.then(resolve);
9999
});
100-
}())`,
100+
}())
101+
//# sourceURL=lighthouse-eval.js`,
101102
includeCommandLineAPI: true,
102103
awaitPromise: true,
103104
returnByValue: true,

lighthouse-core/gather/gatherers/js-usage.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ class JsUsage extends FRGatherer {
8181

8282
for (const scriptUsage of this._scriptUsages) {
8383
// If `url` is blank, that means the script was anonymous (eval, new Function, onload, ...).
84-
// Or, it's because it was code Lighthouse over the protocol via `Runtime.evaluate`.
85-
// We currently don't consider coverage of anonymous scripts, and we definitely don't want
86-
// coverage of code Lighthouse ran to inspect the page, so we ignore this ScriptCoverage if
87-
// url is blank.
88-
if (scriptUsage.url === '') {
84+
if (scriptUsage.url === '' || scriptUsage.url === 'lighthouse-eval.js') {
85+
// We currently don't consider coverage of anonymous scripts, and we definitely don't want
86+
// coverage of code Lighthouse ran to inspect the page, so we ignore this ScriptCoverage.
8987
continue;
9088
}
9189

lighthouse-core/gather/gatherers/scripts.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ async function runInSeriesOrParallel(values, promiseMapper, runInSeries) {
2828
}
2929
}
3030

31+
/**
32+
* Returns true if the script was created via our own calls
33+
* to Runtime.evaluate.
34+
* @param {LH.Crdp.Debugger.ScriptParsedEvent} script
35+
*/
36+
function isLighthouseRuntimeEvaluateScript(script) {
37+
// Scripts created by Runtime.evaluate that run on the main session/frame
38+
// result in an empty string for the embedderName.
39+
if (!script.embedderName) return true;
40+
41+
// Otherwise, when running our own code inside other frames, the embedderName
42+
// is set to the frame's url. In that case, we rely on the special sourceURL that
43+
// we set.
44+
return script.hasSourceURL && script.url === 'lighthouse-eval.js';
45+
}
46+
3147
/**
3248
* @fileoverview Gets JavaScript file contents.
3349
*/
@@ -68,8 +84,9 @@ class Scripts extends FRGatherer {
6884
// it also blocks scripts from the same origin but that happen to run in a different process,
6985
// like a worker.
7086
if (event.method === 'Debugger.scriptParsed' && !sessionId) {
71-
// Events without an embedderName (read: a url) are for JS that we ran over the protocol.
72-
if (event.params.embedderName) this._scriptParsedEvents.push(event.params);
87+
if (!isLighthouseRuntimeEvaluateScript(event.params)) {
88+
this._scriptParsedEvents.push(event.params);
89+
}
7390
}
7491
}
7592

0 commit comments

Comments
 (0)