Skip to content

Commit 8dbd6fa

Browse files
committed
terminology fixes and docs added to extension
1 parent ca444bf commit 8dbd6fa

File tree

2 files changed

+123
-70
lines changed

2 files changed

+123
-70
lines changed

lighthouse-extension/app/src/lighthouse-background.js

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
1817
'use strict';
1918

2019
const ExtensionProtocol = require('../../../lighthouse-core/gather/drivers/extension');
2120
const Runner = require('../../../lighthouse-core/runner');
2221
const Config = require('../../../lighthouse-core/config/config');
23-
const configJSON = require('../../../lighthouse-core/config/default.json');
22+
const defaultConfig = require('../../../lighthouse-core/config/default.json');
2423
const log = require('../../../lighthouse-core/lib/log');
24+
2525
const STORAGE_KEY = 'lighthouse_audits';
2626
const _flatten = arr => [].concat.apply([], arr);
2727

@@ -39,27 +39,45 @@ window.createPageAndPopulate = function(results) {
3939
});
4040
};
4141

42-
window.runAudits = function(options, audits) {
42+
/**
43+
* @param {!Object} options Lighthouse options.
44+
* @param {!Array<string>} requestedAudits Names of audits to run.
45+
* @return {!Promise}
46+
*/
47+
window.runLighthouse = function(options, requestedAudits) {
4348
// Default to 'info' logging level.
4449
log.setLevel('info');
4550
const driver = new ExtensionProtocol();
4651

4752
return driver.getCurrentTabURL()
48-
.then(url => {
49-
// Setup the run config, audits are calculated by selected options
50-
const config = new Config(configJSON, new Set(audits));
51-
52-
// Add in the URL to the options.
53-
return Runner.run(driver, Object.assign({}, options, {url, config}));
54-
}).catch(e => {
55-
console.error(e);
56-
throw e;
57-
});
53+
.then(url => {
54+
// Always start with a freshly parsed default config.
55+
const runConfig = JSON.parse(JSON.stringify(defaultConfig));
56+
57+
// Filter out audits not requested.
58+
requestedAudits = new Set(requestedAudits);
59+
runConfig.audits = runConfig.audits.filter(audit => requestedAudits.has(audit));
60+
const config = new Config(runConfig);
61+
62+
// Add url and config to fresh options object.
63+
const runOptions = Object.assign({}, options, {url, config});
64+
65+
// Run Lighthouse.
66+
return Runner.run(driver, runOptions);
67+
}).catch(e => {
68+
console.error(e);
69+
throw e;
70+
});
5871
};
5972

60-
window.getListOfAudits = function() {
73+
/**
74+
* Returns list of aggregation categories (each with a list of its constituent
75+
* audits) from the default config.
76+
* @return {!Array<{name: string, audits: !Array<string>}>}
77+
*/
78+
window.getDefaultAggregations = function() {
6179
return _flatten(
62-
configJSON.aggregations.map(aggregation => {
80+
defaultConfig.aggregations.map(aggregation => {
6381
if (aggregation.items.length === 1) {
6482
return {
6583
name: aggregation.name,
@@ -69,34 +87,51 @@ window.getListOfAudits = function() {
6987

7088
return aggregation.items;
7189
})
72-
);
90+
).map(aggregation => {
91+
return {
92+
name: aggregation.name,
93+
audits: Object.keys(aggregation.criteria)
94+
};
95+
});
7396
};
7497

75-
window.saveAudits = function(audits) {
76-
let storage = {};
77-
storage[STORAGE_KEY] = {};
78-
79-
window.getListOfAudits().forEach(audit => {
80-
storage[STORAGE_KEY][audit.name] = audits.indexOf(audit.name) > -1;
98+
/**
99+
* Save currently selected set of aggregation categories to local storage.
100+
* @param {!Array<{name: string, audits: !Array<string>}>} selectedAggregations
101+
*/
102+
window.saveSelectedAggregations = function(selectedAggregations) {
103+
const storage = {
104+
[STORAGE_KEY]: {}
105+
};
106+
107+
window.getDefaultAggregations().forEach(audit => {
108+
const selected = selectedAggregations.indexOf(audit.name) > -1;
109+
storage[STORAGE_KEY][audit.name] = selected;
81110
});
82111

83112
chrome.storage.local.set(storage);
84113
};
85114

86-
window.fetchAudits = function() {
115+
/**
116+
* Load selected aggregation categories from local storage.
117+
* @return {!Promise<!Object<boolean>>}
118+
*/
119+
window.loadSelectedAggregations = function() {
87120
return new Promise(resolve => {
88121
chrome.storage.local.get(STORAGE_KEY, result => {
89-
const audits = result[STORAGE_KEY];
90-
91-
// create list of default audits
92-
let defaultAudits = {};
93-
window.getListOfAudits().forEach(audit => {
94-
defaultAudits[audit.name] = true;
122+
// Start with list of all default aggregations set to true so list is
123+
// always up to date.
124+
const defaultAggregations = {};
125+
window.getDefaultAggregations().forEach(aggregation => {
126+
defaultAggregations[aggregation.name] = true;
95127
});
96128

97-
// merge default and saved audits together so we always have the latest list of audits
129+
// Load saved aggregation selections.
130+
const savedSelections = result[STORAGE_KEY];
131+
132+
// Overwrite defaults with any saved aggregation selections.
98133
resolve(
99-
Object.assign({}, defaultAudits, audits)
134+
Object.assign(defaultAggregations, savedSelections)
100135
);
101136
});
102137
});

lighthouse-extension/app/src/popup.js

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
'use strict';
1718

1819
const _flatten = arr => [].concat.apply([], arr);
1920

2021
document.addEventListener('DOMContentLoaded', _ => {
2122
const background = chrome.extension.getBackgroundPage();
22-
const aggregations = background.getListOfAudits();
23+
const defaultAggregations = background.getDefaultAggregations();
2324

2425
const siteNameEl = window.document.querySelector('header h2');
2526
const generateReportEl = document.getElementById('generate-report');
@@ -38,7 +39,7 @@ document.addEventListener('DOMContentLoaded', _ => {
3839

3940
let spinnerAnimation;
4041

41-
const startSpinner = _ => {
42+
function startSpinner() {
4243
statusEl.classList.add(subpageVisibleClass);
4344
spinnerAnimation = spinnerEl.animate([
4445
{transform: 'rotate(0deg)'},
@@ -47,72 +48,88 @@ document.addEventListener('DOMContentLoaded', _ => {
4748
duration: 1000,
4849
iterations: Infinity
4950
});
50-
};
51+
}
5152

52-
const stopSpinner = _ => {
53+
function stopSpinner() {
5354
spinnerAnimation.cancel();
5455
statusEl.classList.remove(subpageVisibleClass);
55-
};
56+
}
5657

57-
const logstatus = ([, message, details]) => {
58+
function logstatus([, message, details]) {
5859
statusMessageEl.textContent = message;
5960
statusDetailsMessageEl.textContent = details;
60-
};
61+
}
6162

62-
const createOptionItem = (text, isChecked) => {
63+
function createOptionItem(text, isChecked) {
6364
const input = document.createElement('input');
64-
const attributes = [['type', 'checkbox'], ['value', text]];
65+
input.setAttribute('type', 'checkbox');
66+
input.setAttribute('value', text);
6567
if (isChecked) {
66-
attributes.push(['checked', 'checked']);
68+
input.setAttribute('checked', 'checked');
6769
}
6870

69-
attributes.forEach(attr => input.setAttribute.apply(input, attr));
70-
7171
const label = document.createElement('label');
7272
label.appendChild(input);
7373
label.appendChild(document.createTextNode(text));
7474
const listItem = document.createElement('li');
7575
listItem.appendChild(label);
7676

7777
return listItem;
78-
};
79-
80-
const generateOptionsList = (list, selectedAudits) => {
78+
}
79+
80+
/**
81+
* Generates a document fragment containing a list of checkboxes and labels
82+
* for the aggregation categories.
83+
* @param {!Object<boolean>} selectedAggregations
84+
* @return {!DocumentFragment}
85+
*/
86+
function generateOptionsList(list, selectedAggregations) {
8187
const frag = document.createDocumentFragment();
8288

83-
aggregations.forEach(aggregation => {
84-
frag.appendChild(createOptionItem(aggregation.name, selectedAudits[aggregation.name]));
89+
defaultAggregations.forEach(aggregation => {
90+
const isChecked = selectedAggregations[aggregation.name];
91+
frag.appendChild(createOptionItem(aggregation.name, isChecked));
8592
});
8693

87-
list.appendChild(frag);
88-
};
94+
return frag;
95+
}
96+
97+
/**
98+
* Returns an array of names of audits from the selected aggregation
99+
* categories.
100+
* @param {!Object<boolean>} selectedAggregations
101+
* @return {!Array<string>}
102+
*/
103+
function getAuditsFromSelected(selectedAggregations) {
104+
const auditLists = defaultAggregations.filter(aggregation => {
105+
return selectedAggregations[aggregation.name];
106+
}).map(selectedAggregation => {
107+
return selectedAggregation.audits;
108+
});
89109

90-
const getAuditsFromCategory = audits => _flatten(
91-
Object.keys(audits).filter(audit => audits[audit]).map(audit => {
92-
const auditsInCategory = aggregations
93-
.find(aggregation => aggregation.name === audit).criteria;
94-
95-
return Object.keys(auditsInCategory);
96-
})
97-
);
110+
return _flatten(auditLists);
111+
}
98112

99113
background.listenForStatus(logstatus);
100-
background.fetchAudits().then(audits => {
101-
generateOptionsList(optionsList, audits);
114+
background.loadSelectedAggregations().then(aggregations => {
115+
const frag = generateOptionsList(optionsList, aggregations);
116+
optionsList.appendChild(frag);
102117
});
103118

104119
generateReportEl.addEventListener('click', () => {
105120
startSpinner();
106121
feedbackEl.textContent = '';
107122

108-
background.fetchAudits()
109-
.then(getAuditsFromCategory)
110-
.then(audits => background.runAudits({
111-
flags: {
112-
mobile: true,
113-
loadPage: true
114-
}
115-
}, audits))
123+
background.loadSelectedAggregations()
124+
.then(getAuditsFromSelected)
125+
.then(selectedAudits => {
126+
return background.runLighthouse({
127+
flags: {
128+
mobile: true,
129+
loadPage: true
130+
}
131+
}, selectedAudits);
132+
})
116133
.then(results => {
117134
background.createPageAndPopulate(results);
118135
})
@@ -133,9 +150,10 @@ document.addEventListener('DOMContentLoaded', _ => {
133150
});
134151

135152
okButton.addEventListener('click', () => {
136-
const checkedAudits = Array.from(optionsEl.querySelectorAll(':checked'))
153+
// Save selected aggregation categories on options page close.
154+
const checkedAggregations = Array.from(optionsEl.querySelectorAll(':checked'))
137155
.map(input => input.value);
138-
background.saveAudits(checkedAudits);
156+
background.saveSelectedAggregations(checkedAggregations);
139157

140158
optionsEl.classList.remove(subpageVisibleClass);
141159
});

0 commit comments

Comments
 (0)