-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Fix some cross-browser compatibility issues #1593
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,8 @@ class ConfigPanel { | |
this._messageField = this._configPanel.querySelector('.js-message'); | ||
this._urlBlockingList = this._configPanel.querySelector('.js-url-blocking-patterns'); | ||
this._urlBlockingStatus = {}; | ||
this._reportId = new URL(window.location).searchParams.get('id'); | ||
const match = location.search.match(/[?&]id=([^&]*)/); | ||
this._reportId = match ? match[1]: ''; | ||
|
||
const bodyToggle = this._configPanel.querySelector('.js-panel-toggle'); | ||
bodyToggle.addEventListener('click', () => this._toggleBody()); | ||
|
@@ -83,15 +84,6 @@ class ConfigPanel { | |
} | ||
}); | ||
}); | ||
|
||
// get and recover blocked URL patterns of current run | ||
fetch(`/flags?id=${this._reportId}`).then(response => { | ||
return response.json(); | ||
}).then(flags => { | ||
const blockedUrlPatterns = flags.blockedUrlPatterns || []; | ||
blockedUrlPatterns.forEach(urlPattern => this.addBlockedUrlPattern(urlPattern)); | ||
this.log(''); | ||
}); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could also use a polyfill for this: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But Safari 10.1 also has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets not polyfill fetch() |
||
/** | ||
|
@@ -100,15 +92,24 @@ class ConfigPanel { | |
*/ | ||
_rerunLighthouse() { | ||
this.log('Start Rerunning Lighthouse'); | ||
|
||
const options = { | ||
blockedUrlPatterns: this.getBlockedUrlPatterns() | ||
}; | ||
|
||
return fetch(`/rerun?id=${this._reportId}`, {method: 'POST', body: JSON.stringify(options)}) | ||
.then(response => response.text()) | ||
.then(newReportId => location.assign(`?id=${newReportId}`)) | ||
.catch(err => this.log(`Lighthouse Runtime Error: ${err}`)); | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open('POST', `/rerun?id=${this._reportId}`, true); | ||
xhr.onreadystatechange = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just use |
||
if (xhr.readyState !== 4) { | ||
return; | ||
} | ||
|
||
if (xhr.status === 200) { | ||
location.assign(`?id=${xhr.responseText}`); | ||
} else if (xhr.status === 500) { | ||
this.log('Error: Lighthouse runtime error'); | ||
} | ||
}; | ||
xhr.send(JSON.stringify(options)); | ||
} | ||
|
||
/** | ||
|
@@ -118,8 +119,12 @@ class ConfigPanel { | |
* @param {string} urlPattern | ||
*/ | ||
addBlockedUrlPattern(urlPattern) { | ||
if (this._urlBlockingStatus[urlPattern]) { | ||
this.log(`${urlPattern} is already in the list`); | ||
urlPattern = urlPattern.trim(); | ||
if (urlPattern.length === 0) { | ||
this.log('Error: URL blocking pattern is an empty string'); | ||
return; | ||
} else if (this._urlBlockingStatus[urlPattern]) { | ||
this.log(`Error: ${urlPattern} is already in the list`); | ||
return; | ||
} | ||
|
||
|
@@ -150,8 +155,9 @@ class ConfigPanel { | |
* @param {string} urlPattern | ||
*/ | ||
removeBlockedUrlPattern(urlPattern) { | ||
urlPattern = urlPattern.trim(); | ||
if (!this._urlBlockingStatus[urlPattern]) { | ||
this.log(`${urlPattern} is not in the list`); | ||
this.log(`Error: ${urlPattern} is not in the list`); | ||
return; | ||
} | ||
|
||
|
@@ -193,5 +199,11 @@ class ConfigPanel { | |
} | ||
|
||
window.addEventListener('DOMContentLoaded', () => { | ||
new ConfigPanel(); | ||
const configPanel = new ConfigPanel(); | ||
|
||
// init recover blocked URL patterns | ||
[...document.querySelectorAll('.js-currently-blocked-url')].forEach(ele => { | ||
configPanel.addBlockedUrlPattern(ele.getAttribute('data-url-pattern')); | ||
}); | ||
configPanel.log(''); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use a polyfill in Viewer for URLSearchParams: https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-viewer/package.json#L33
Could you use that here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your advice. Shall I move the dependencies from viewer to core? Or just add some dependencies to cli?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to keep the deps for the viewer since it's a separate app.
Since you've already done the work of moving over to the fallbacks, there's not a lot of benefit to adding polyfills for two small APIs. I'm fine with what you've got.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a comment here?
// Compat: URL.searchParams isn't yet supported by _______
so later when that browser updates we'll know when we can remove it