Skip to content

Commit 6ed6799

Browse files
committed
add filter 3p logic to report
1 parent 77c85eb commit 6ed6799

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

lighthouse-core/report/html/renderer/report-ui-features.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class ReportUIFeatures {
8686
this.json = report;
8787
this._setupMediaQueryListeners();
8888
this._setupExportButton();
89+
this._setupThirdPartyFilter();
8990
this._setUpCollapseDetailsAfterPrinting();
9091
this._setupHeaderAnimation();
9192
this._resetUIState();
@@ -128,6 +129,54 @@ class ReportUIFeatures {
128129
dropdown.addEventListener('click', this.onExport);
129130
}
130131

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+
131180
_setupHeaderAnimation() {
132181
const scoresWrapper = this._dom.find('.lh-scores-wrapper', this._document);
133182
const computedMarginTop = window.getComputedStyle(scoresWrapper).marginTop;

lighthouse-core/report/html/templates.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,24 @@
650650
</div>
651651
</div>
652652
</template>
653+
654+
<template id="tmpl-lh-3p-filter">
655+
<style>
656+
.lh-3p-filter {
657+
background: var(--lh-table-higlight-bg);
658+
color: var(--medium-75-gray);
659+
float: right;
660+
padding: 6px;
661+
}
662+
.lh-3p-filter-label, .lh-3p-filter-input {
663+
cursor: pointer;
664+
vertical-align: middle;
665+
}
666+
</style>
667+
<div class="lh-3p-filter">
668+
<label class="lh-3p-filter-label">
669+
Filter out 3rd-parties:
670+
<input type="checkbox" class="lh-3p-filter-input">
671+
</label>
672+
</div>
673+
</template>

0 commit comments

Comments
 (0)