@@ -86,6 +86,7 @@ class ReportUIFeatures {
86
86
this . json = report ;
87
87
this . _setupMediaQueryListeners ( ) ;
88
88
this . _setupExportButton ( ) ;
89
+ this . _setupThirdPartyFilter ( ) ;
89
90
this . _setUpCollapseDetailsAfterPrinting ( ) ;
90
91
this . _setupHeaderAnimation ( ) ;
91
92
this . _resetUIState ( ) ;
@@ -128,6 +129,54 @@ class ReportUIFeatures {
128
129
dropdown . addEventListener ( 'click' , this . onExport ) ;
129
130
}
130
131
132
+ _setupThirdPartyFilter ( ) {
133
+ const pageTLDPlusOne = new URL ( this . json . finalUrl ) . origin . split ( '.' ) . slice ( - 2 ) . join ( '.' ) ;
134
+
135
+ // get all tables with a text url
136
+ /** @type {Array<Element> } */
137
+ const tables = Array . from ( document . querySelectorAll ( '.lh-table' ) ) ;
138
+ tables . filter ( el => el . querySelector ( 'td.lh-table-column--url' ) )
139
+ . forEach ( ( el , index ) => {
140
+ const replacements = new Map ( ) ;
141
+ const urlItems = el . querySelectorAll ( '.lh-text__url' ) ;
142
+
143
+ // create input box
144
+ const filterTemplate = this . _dom . cloneTemplate ( '#tmpl-lh-3p-filter' , this . _document ) ;
145
+ const filterInput = filterTemplate . querySelector ( 'input' ) ;
146
+ const id = `lh-3p-filter-label--${ index } ` ;
147
+ filterTemplate . querySelector ( 'label' ) . setAttribute ( 'for' , id ) ;
148
+ filterInput . setAttribute ( 'id' , id ) ;
149
+
150
+ filterInput . addEventListener ( 'change' , e => {
151
+ // remove elements from the dom and keep track of them to readd on uncheck
152
+ // why removing instead of hiding? nth-child(even) background-colors keep working
153
+ if ( e . target . checked ) {
154
+ let i = 0 ;
155
+ for ( const urlItem of urlItems ) {
156
+ const isThirdParty = ! urlItem . title . includes ( `${ pageTLDPlusOne } /` ) ;
157
+ if ( ! isThirdParty ) continue ;
158
+
159
+ const urlRow = urlItem . closest ( 'tr' ) ;
160
+ replacements . set ( urlRow , Array . from ( el . tBodies [ 0 ] . children ) . indexOf ( urlRow ) + i ++ ) ;
161
+
162
+ urlRow . parentNode . removeChild ( urlRow ) ;
163
+ }
164
+ } else {
165
+ for ( let [ urlRow , position ] of replacements . entries ( ) ) {
166
+ // if we have children we add them on the right position, else we just append it to the list
167
+ if ( el . tBodies [ 0 ] . children . length ) {
168
+ Array . from ( el . tBodies [ 0 ] . children ) [ position - 1 ] . insertAdjacentElement ( 'afterend' , urlRow ) ;
169
+ } else {
170
+ el . tBodies [ 0 ] . appendChild ( urlRow ) ;
171
+ }
172
+ }
173
+ }
174
+ } ) ;
175
+
176
+ el . parentNode . insertBefore ( filterTemplate , el ) ;
177
+ } ) ;
178
+ }
179
+
131
180
_setupHeaderAnimation ( ) {
132
181
const scoresWrapper = this . _dom . find ( '.lh-scores-wrapper' , this . _document ) ;
133
182
const computedMarginTop = window . getComputedStyle ( scoresWrapper ) . marginTop ;
0 commit comments