Skip to content

Commit 5c72d72

Browse files
wardpeetpaulirish
authored andcommitted
Save audit list into storage so it's kept for the next run (#595)
1 parent 5d20fa8 commit 5c72d72

File tree

3 files changed

+78
-33
lines changed

3 files changed

+78
-33
lines changed

lighthouse-extension/app/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"permissions": [
1919
"activeTab",
2020
"debugger",
21+
"storage",
2122
"tabs"
2223
],
2324
"browser_action": {

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const Runner = require('../../../lighthouse-core/runner');
2222
const Config = require('../../../lighthouse-core/config/config');
2323
const configJSON = require('../../../lighthouse-core/config/default.json');
2424
const log = require('../../../lighthouse-core/lib/log');
25+
const STORAGE_KEY = 'lighthouse_audits';
26+
const _flatten = arr => [].concat.apply([], arr);
2527

2628
window.createPageAndPopulate = function(results) {
2729
const tabURL = chrome.extension.getURL('/pages/report.html');
@@ -40,7 +42,6 @@ window.createPageAndPopulate = function(results) {
4042
window.runAudits = function(options, audits) {
4143
// Default to 'info' logging level.
4244
log.setLevel('info');
43-
4445
const driver = new ExtensionProtocol();
4546

4647
return driver.getCurrentTabURL()
@@ -56,6 +57,52 @@ window.runAudits = function(options, audits) {
5657
});
5758
};
5859

60+
window.getListOfAudits = function() {
61+
return _flatten(
62+
configJSON.aggregations.map(aggregation => {
63+
if (aggregation.items.length === 1) {
64+
return {
65+
name: aggregation.name,
66+
criteria: aggregation.items[0].criteria,
67+
};
68+
}
69+
70+
return aggregation.items;
71+
})
72+
);
73+
};
74+
75+
window.saveAudits = function(audits) {
76+
const listOfAudits = window.getListOfAudits().map(aggregation => aggregation.name);
77+
let storage = {};
78+
storage[STORAGE_KEY] = {};
79+
80+
window.getListOfAudits().forEach(audit => {
81+
storage[STORAGE_KEY][audit.name] = audits.indexOf(audit.name) > -1;
82+
});
83+
84+
chrome.storage.local.set(storage);
85+
};
86+
87+
window.fetchAudits = function() {
88+
return new Promise(resolve => {
89+
chrome.storage.local.get(STORAGE_KEY, result => {
90+
const audits = result[STORAGE_KEY];
91+
92+
// create list of default audits
93+
let defaultAudits = {};
94+
window.getListOfAudits().forEach((audit) => {
95+
defaultAudits[audit.name] = true;
96+
});
97+
98+
// merge default and saved audits together so we always have the latest list of audits
99+
resolve(
100+
Object.assign({}, defaultAudits, audits)
101+
);
102+
});
103+
});
104+
};
105+
59106
window.listenForStatus = function(callback) {
60107
log.events.addListener('status', callback);
61108
};

lighthouse-extension/app/src/popup.js

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,12 @@
1515
* limitations under the License.
1616
*/
1717

18-
const configJSON = require('../../../lighthouse-core/config/default.json');
1918
const _flatten = arr => [].concat.apply([], arr);
2019

21-
const aggregations = _flatten(
22-
configJSON.aggregations.map(aggregation => {
23-
if (aggregation.items.length > 1) {
24-
return aggregation.items;
25-
}
26-
27-
return {
28-
name: aggregation.name,
29-
criteria: aggregation.items[0].criteria,
30-
};
31-
})
32-
);
33-
3420
document.addEventListener('DOMContentLoaded', _ => {
3521
const background = chrome.extension.getBackgroundPage();
22+
const aggregations = background.getListOfAudits();
23+
3624
const siteNameEl = window.document.querySelector('header h2');
3725
const generateReportEl = document.getElementById('generate-report');
3826
const subpageVisibleClass = 'subpage--visible';
@@ -71,15 +59,13 @@ document.addEventListener('DOMContentLoaded', _ => {
7159
statusDetailsMessageEl.textContent = details;
7260
};
7361

74-
const getAuditsOfName = name => {
75-
let aggregation = aggregations.filter(aggregation => aggregation.name === name);
76-
77-
return Object.keys(aggregation[0].criteria);
78-
};
79-
80-
const createOptionItem = text => {
62+
const createOptionItem = (text, isChecked) => {
8163
const input = document.createElement('input');
82-
const attributes = [['type', 'checkbox'], ['checked', 'checked'], ['value', text]];
64+
const attributes = [['type', 'checkbox'], ['value', text]];
65+
if (isChecked) {
66+
attributes.push(['checked', 'checked']);
67+
}
68+
8369
attributes.forEach(attr => input.setAttribute.apply(input, attr));
8470

8571
const label = document.createElement('label');
@@ -91,34 +77,41 @@ document.addEventListener('DOMContentLoaded', _ => {
9177
return listItem;
9278
};
9379

94-
const generateOptionsList = list => {
80+
const generateOptionsList = (list, selectedAudits) => {
9581
const frag = document.createDocumentFragment();
9682

9783
aggregations.forEach(aggregation => {
98-
frag.appendChild(createOptionItem(aggregation.name));
84+
frag.appendChild(createOptionItem(aggregation.name, selectedAudits[aggregation.name]));
9985
});
10086

10187
list.appendChild(frag);
10288
};
10389

90+
const getAuditsFromCategory = audits => _flatten(
91+
Object.keys(audits).filter(audit => audits[audit]).map(audit => {
92+
const auditsInCategory = aggregations.find(aggregation => aggregation.name === audit).criteria;
93+
94+
return Object.keys(auditsInCategory);
95+
})
96+
);
97+
10498
background.listenForStatus(logstatus);
105-
generateOptionsList(optionsList);
99+
background.fetchAudits().then(audits => {
100+
generateOptionsList(optionsList, audits);
101+
});
106102

107103
generateReportEl.addEventListener('click', () => {
108104
startSpinner();
109105
feedbackEl.textContent = '';
110106

111-
const audits = _flatten(
112-
Array.from(optionsEl.querySelectorAll(':checked'))
113-
.map(input => getAuditsOfName(input.value))
114-
);
115-
116-
background.runAudits({
107+
background.fetchAudits()
108+
.then(getAuditsFromCategory)
109+
.then(audits => background.runAudits({
117110
flags: {
118111
mobile: true,
119112
loadPage: true
120113
}
121-
}, audits)
114+
}, audits))
122115
.then(results => {
123116
background.createPageAndPopulate(results);
124117
})
@@ -139,6 +132,10 @@ document.addEventListener('DOMContentLoaded', _ => {
139132
});
140133

141134
okButton.addEventListener('click', () => {
135+
const checkedAudits = Array.from(optionsEl.querySelectorAll(':checked'))
136+
.map(input => input.value);
137+
background.saveAudits(checkedAudits);
138+
142139
optionsEl.classList.remove(subpageVisibleClass);
143140
});
144141

0 commit comments

Comments
 (0)