-
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
CRX: disable extensions before running lighthouse #1492
Changes from all commits
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
"permissions": [ | ||
"activeTab", | ||
"debugger", | ||
"management", | ||
"storage" | ||
], | ||
"browser_action": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,11 +26,36 @@ const log = require('../../../lighthouse-core/lib/log'); | |
const ReportGenerator = require('../../../lighthouse-core/report/report-generator'); | ||
|
||
const STORAGE_KEY = 'lighthouse_audits'; | ||
const _flatten = arr => [].concat(...arr); | ||
|
||
let installedExtensions = []; | ||
let lighthouseIsRunning = false; | ||
let latestStatusLog = []; | ||
|
||
const _flatten = arr => [].concat(...arr); | ||
|
||
/** | ||
* Enables or disables all other installed chrome extensions. The initial list | ||
* of the user's extension is created when the background page is started. | ||
* @param {!boolean} enable If true, enables all other installed extensions. | ||
* False disables them. | ||
* @param {!Promise} | ||
*/ | ||
function enableOtherChromeExtensions(enable) { | ||
const str = enable ? 'enabling' : 'disabling'; | ||
log.log('Chrome', `${str} ${installedExtensions.length} extensions.`); | ||
|
||
return Promise.all(installedExtensions.map(info => { | ||
return new Promise((resolve, reject) => { | ||
chrome.management.setEnabled(info.id, enable, _ => { | ||
if (chrome.runtime.lastError) { | ||
reject(chrome.runtime.lastError); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
})); | ||
} | ||
|
||
/** | ||
* Filter out any unrequested aggregations from the config. If any audits are | ||
* no longer needed by any remaining aggregations, filter out those as well. | ||
|
@@ -124,8 +149,7 @@ window.runLighthouseForConnection = function(connection, url, options, requested | |
lighthouseIsRunning = true; | ||
updateBadgeUI(url); | ||
|
||
// Run Lighthouse. | ||
return Runner.run(connection, runOptions) | ||
return Runner.run(connection, runOptions) // Run Lighthouse. | ||
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. can revert this block of changes? 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. It's just a proper 4-space indent. No code changes. https://github.com/GoogleChrome/lighthouse/pull/1492/files?w=1 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. only two spaces for chaining though, just to keep everyone on their toes :) 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. (I think this is one place we diverge from Google style, but it's so common in open source JS that we've pretty much unquestioningly done it throughout LH) 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. We're not consistent, but I'll revert to be consistent with the file. If we want to enforce something other than Goog style, there really should be an eslint rule. |
||
.then(result => { | ||
lighthouseIsRunning = false; | ||
updateBadgeUI(); | ||
|
@@ -148,11 +172,18 @@ window.runLighthouseInExtension = function(options, requestedAggregations) { | |
// Default to 'info' logging level. | ||
log.setLevel('info'); | ||
const connection = new ExtensionProtocol(); | ||
return connection.getCurrentTabURL() | ||
return enableOtherChromeExtensions(false) | ||
.then(_ => connection.getCurrentTabURL()) | ||
.then(url => window.runLighthouseForConnection(connection, url, options, requestedAggregations)) | ||
.then(results => { | ||
const blobURL = window.createReportPageAsBlob(results, 'extension'); | ||
chrome.tabs.create({url: blobURL}); | ||
return enableOtherChromeExtensions(true).then(_ => { | ||
const blobURL = window.createReportPageAsBlob(results, 'extension'); | ||
chrome.tabs.create({url: blobURL}); | ||
}); | ||
}).catch(err => { | ||
return enableOtherChromeExtensions(true).then(_ => { | ||
throw err; | ||
}); | ||
}); | ||
}; | ||
|
||
|
@@ -277,7 +308,19 @@ window.isRunning = function() { | |
return lighthouseIsRunning; | ||
}; | ||
|
||
// Run when in extension context, but not in devtools. | ||
if (window.chrome && chrome.runtime) { | ||
// Get list of installed extensions that are enabled and can be disabled. | ||
// Extensions are not allowed to be disabled if they are under an admin policy. | ||
chrome.management.getAll(installs => { | ||
chrome.management.getSelf(lighthouseCrxInfo => { | ||
installedExtensions = installs.filter(info => { | ||
return info.id !== lighthouseCrxInfo.id && info.type === 'extension' && | ||
info.enabled && info.mayDisable; | ||
}); | ||
}); | ||
}); | ||
|
||
chrome.runtime.onInstalled.addListener(details => { | ||
if (details.previousVersion) { | ||
// eslint-disable-next-line no-console | ||
|
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.
since the extension is running this after a button click, I assume this gets around the docs warning about "In most cases this function must be called in the context of a user gesture"?
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.
Yea. Still falls within that click.