@@ -60,63 +60,32 @@ class Stylesheets extends BaseGatherer {
60
60
61
61
/**
62
62
* @param {LH.Gatherer.Context } context
63
+ * @return {Promise<LH.Artifacts['Stylesheets']> }
63
64
*/
64
- async startInstrumentation ( context ) {
65
+ async getArtifact ( context ) {
66
+ const executionContext = context . driver . executionContext ;
65
67
const session = context . driver . defaultSession ;
66
68
this . _session = session ;
67
69
68
- // Calling `CSS.enable` will emit events for stylesheets currently on the page.
69
- // We want to ignore these events in navigation mode because they are not relevant to the
70
- // navigation that is about to happen. Adding the event listener *after* calling `CSS.enable`
71
- // ensures that the events for pre-existing stylesheets are ignored.
72
- const isNavigation = context . gatherMode === 'navigation' ;
73
- if ( ! isNavigation ) {
74
- session . on ( 'CSS.styleSheetAdded' , this . _onStylesheetAdded ) ;
75
- }
70
+ session . on ( 'CSS.styleSheetAdded' , this . _onStylesheetAdded ) ;
76
71
77
72
await session . sendCommand ( 'DOM.enable' ) ;
78
73
await session . sendCommand ( 'CSS.enable' ) ;
79
74
80
- if ( isNavigation ) {
81
- session . on ( 'CSS.styleSheetAdded' , this . _onStylesheetAdded ) ;
82
- }
83
- }
84
-
75
+ // Force style to recompute.
76
+ // Doesn't appear to be necessary in newer versions of Chrome.
77
+ await executionContext . evaluateAsync ( 'getComputedStyle(document.body)' ) ;
85
78
86
- /**
87
- * @param {LH.Gatherer.Context } context
88
- */
89
- async stopInstrumentation ( context ) {
90
- const session = context . driver . defaultSession ;
91
79
session . off ( 'CSS.styleSheetAdded' , this . _onStylesheetAdded ) ;
92
80
93
81
// Ensure we finish fetching all stylesheet contents before disabling the CSS domain
94
- await Promise . all ( this . _sheetPromises . values ( ) ) ;
82
+ const sheets = await Promise . all ( this . _sheetPromises . values ( ) ) ;
95
83
96
84
await session . sendCommand ( 'CSS.disable' ) ;
97
85
await session . sendCommand ( 'DOM.disable' ) ;
98
- }
99
-
100
- /**
101
- * @param {LH.Gatherer.Context } context
102
- * @return {Promise<LH.Artifacts['Stylesheets']> }
103
- */
104
- async getArtifact ( context ) {
105
- const executionContext = context . driver . executionContext ;
106
-
107
- if ( context . gatherMode === 'snapshot' ) {
108
- await this . startInstrumentation ( context ) ;
109
-
110
- // Force style to recompute.
111
- // Doesn't appear to be necessary in newer versions of Chrome.
112
- await executionContext . evaluateAsync ( 'getComputedStyle(document.body)' ) ;
113
-
114
- await this . stopInstrumentation ( context ) ;
115
- }
116
86
117
87
/** @type {Map<string, LH.Artifacts.CSSStyleSheetInfo> } */
118
88
const dedupedStylesheets = new Map ( ) ;
119
- const sheets = await Promise . all ( this . _sheetPromises . values ( ) ) ;
120
89
121
90
for ( const sheet of sheets ) {
122
91
// Erroneous sheets will be reported via sentry and the log.
0 commit comments