Skip to content

Commit

Permalink
Expand report <details> on print. Fixes #1240
Browse files Browse the repository at this point in the history
  • Loading branch information
ebidel committed Jan 13, 2017
1 parent ec97c35 commit 4d3bff0
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lighthouse-core/report/scripts/lighthouse-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,32 @@ function sendJSONReport() {
const popup = window.open(VIEWER_URL, '_blank');
}

/**
* Sets up listeners to expand audit `<details>` when the user prints the page.
* Upon closing the print dialog, the details are collapsed.
*/
function expandDetailsWhenPrinting() {
const details = Array.from(document.querySelectorAll('details'));

// FF and IE implement these old events.
if ('onbeforeprint' in window) {
window.addEventListener('beforeprint', _ => {
details.map(detail => detail.open = true);
});
window.addEventListener('afterprint', _ => {
details.map(detail => detail.open = false);
});
} else {
// Note: while FF has media listeners, it doesn't fire when matching 'print'.
window.matchMedia('print').addListener(mql => {
details.map(detail => detail.open = mql.matches);
});
}
}

window.addEventListener('DOMContentLoaded', _ => {
expandDetailsWhenPrinting();

const printButton = document.querySelector('.js-print');
printButton.addEventListener('click', _ => {
window.print();
Expand Down

0 comments on commit 4d3bff0

Please sign in to comment.