Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viewer: enable pasting json report on app #1128

Merged
merged 2 commits into from
Dec 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lighthouse-viewer/app/src/lighthouse-report-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class LighthouseViewerReport {
this.onCopy = this.onCopy.bind(this);
this.onCopyButtonClick = this.onCopyButtonClick.bind(this);
this.onFileUpload = this.onFileUpload.bind(this);
this.onPaste = this.onPaste.bind(this);

this._copyAttempt = false;

Expand Down Expand Up @@ -78,6 +79,8 @@ class LighthouseViewerReport {
copyButton.addEventListener('click', this.onCopyButtonClick);
document.addEventListener('copy', this.onCopy);
}

document.addEventListener('paste', this.onPaste);
}

enableShareButton() {
Expand Down Expand Up @@ -239,6 +242,22 @@ class LighthouseViewerReport {
logger.log(err.message);
}
}

/**
* Enables pasting a JSON report on the page.
*/
onPaste(e) {
e.preventDefault();

try {
const json = JSON.parse(e.clipboardData.getData('text'));
this.replaceReportHTML(json);

ga('send', 'event', 'report', 'paste');
} catch (err) {
// noop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe log the failure here?

Copy link
Contributor Author

@ebidel ebidel Dec 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it will be that useful to users. I'd prefer just ignoring random key pressing or users that accidentally paste something they didn't mean to in our app.

Note: replaceReportHTML will show a butter bar if the json file is invalid, so there's some feedback already built in if this go wrong.

}
}
}

module.exports = LighthouseViewerReport;