Skip to content

Commit 1b5bfd0

Browse files
authored
Merge pull request #560 from wardpeet/extension-configuration
Added audit configurations to extension
2 parents ed027d8 + f9de055 commit 1b5bfd0

File tree

4 files changed

+132
-17
lines changed

4 files changed

+132
-17
lines changed

lighthouse-extension/app/popup.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,25 @@ <h2 class="header-titles__url">...</h2>
3838

3939
<main class="main" role="main">
4040
<div class="feedback"></div>
41-
<button class="generate-report">Generate report</button>
41+
42+
<button class="button button--configure" id="configure-options">Options</button>
43+
<button class="button button--generate" id="generate-report">Generate report</button>
4244
</main>
4345

44-
<aside class="status">
46+
<aside class="status subpage">
4547
<div class="status__spinner"></div>
4648
<div class="status__msg">Starting...</div>
4749
<div><small class="status__detailsmsg"></small></div>
4850
</aside>
4951

52+
<aside class="options subpage">
53+
<h2 class="options__title">Options</h2>
54+
<ul class="options__list">
55+
</ul>
56+
57+
<button class="button button--ok" id="ok">OK</button>
58+
</aside>
59+
5060
<script src="scripts/popup.js"></script>
5161
</body>
5262
</html>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ window.createPageAndPopulate = function(results) {
3737
});
3838
};
3939

40-
window.runAudits = function(options) {
40+
window.runAudits = function(options, audits) {
4141
// Default to 'info' logging level.
4242
log.setLevel('info');
4343

4444
const driver = new ExtensionProtocol();
4545

4646
return driver.getCurrentTabURL()
4747
.then(url => {
48-
// Setup the run config, false == no whitelist, run all audits
49-
const config = new Config(configJSON, false);
48+
// Setup the run config, audits are calculated by selected options
49+
const config = new Config(configJSON, new Set(audits));
5050

5151
// Add in the URL to the options.
5252
return Runner.run(driver, Object.assign({}, options, {url, config}));

lighthouse-extension/app/src/popup.js

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,43 @@
1515
* limitations under the License.
1616
*/
1717

18+
const configJSON = require('../../../lighthouse-core/config/default.json');
19+
const _flatten = arr => [].concat.apply([], arr);
20+
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+
1834
document.addEventListener('DOMContentLoaded', _ => {
1935
const background = chrome.extension.getBackgroundPage();
2036
const siteNameEl = window.document.querySelector('header h2');
21-
const generateReportEl = document.body.querySelector('.generate-report');
37+
const generateReportEl = document.getElementById('generate-report');
38+
const subpageVisibleClass = 'subpage--visible';
2239

2340
const statusEl = document.body.querySelector('.status');
2441
const statusMessageEl = document.body.querySelector('.status__msg');
2542
const statusDetailsMessageEl = document.body.querySelector('.status__detailsmsg');
2643
const spinnerEl = document.body.querySelector('.status__spinner');
2744
const feedbackEl = document.body.querySelector('.feedback');
45+
46+
const generateOptionsEl = document.getElementById('configure-options');
47+
const optionsEl = document.body.querySelector('.options');
48+
const optionsList = document.body.querySelector('.options__list');
49+
const okButton = document.getElementById('ok');
50+
2851
let spinnerAnimation;
2952

3053
const startSpinner = _ => {
31-
statusEl.classList.add('status--visible');
54+
statusEl.classList.add(subpageVisibleClass);
3255
spinnerAnimation = spinnerEl.animate([
3356
{transform: 'rotate(0deg)'},
3457
{transform: 'rotate(359deg)'}
@@ -40,25 +63,62 @@ document.addEventListener('DOMContentLoaded', _ => {
4063

4164
const stopSpinner = _ => {
4265
spinnerAnimation.cancel();
43-
statusEl.classList.remove('status--visible');
66+
statusEl.classList.remove(subpageVisibleClass);
4467
};
4568

4669
const logstatus = ([, message, details]) => {
4770
statusMessageEl.textContent = message;
4871
statusDetailsMessageEl.textContent = details;
4972
};
5073

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 => {
81+
const input = document.createElement('input');
82+
const attributes = [['type', 'checkbox'], ['checked', 'checked'], ['value', text]];
83+
attributes.forEach(attr => input.setAttribute.apply(input, attr));
84+
85+
const label = document.createElement('label');
86+
label.appendChild(input);
87+
label.appendChild(document.createTextNode(text));
88+
const listItem = document.createElement('li');
89+
listItem.appendChild(label);
90+
91+
return listItem;
92+
};
93+
94+
const generateOptionsList = list => {
95+
const frag = document.createDocumentFragment();
96+
97+
aggregations.forEach(aggregation => {
98+
frag.appendChild(createOptionItem(aggregation.name));
99+
});
100+
101+
list.appendChild(frag);
102+
};
103+
51104
background.listenForStatus(logstatus);
105+
generateOptionsList(optionsList);
52106

53107
generateReportEl.addEventListener('click', () => {
54108
startSpinner();
55109
feedbackEl.textContent = '';
110+
111+
const audits = _flatten(
112+
Array.from(optionsEl.querySelectorAll(':checked'))
113+
.map(input => getAuditsOfName(input.value))
114+
);
115+
56116
background.runAudits({
57117
flags: {
58118
mobile: true,
59119
loadPage: true
60120
}
61-
})
121+
}, audits)
62122
.then(results => {
63123
background.createPageAndPopulate(results);
64124
})
@@ -74,6 +134,14 @@ document.addEventListener('DOMContentLoaded', _ => {
74134
});
75135
});
76136

137+
generateOptionsEl.addEventListener('click', () => {
138+
optionsEl.classList.add(subpageVisibleClass);
139+
});
140+
141+
okButton.addEventListener('click', () => {
142+
optionsEl.classList.remove(subpageVisibleClass);
143+
});
144+
77145
chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) {
78146
if (tabs.length === 0) {
79147
return;

lighthouse-extension/app/styles/lighthouse.css

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,42 @@ body {
108108
border-top: 1px solid rgba(255,255,255,0.1);
109109
background: #49525F;
110110
justify-content: space-around;
111-
flex-direction: column;
111+
flex-direction: row;
112112
align-items: flex-end;
113113
}
114114

115-
.generate-report {
115+
.button {
116+
cursor: pointer;
116117
font-family: 'Roboto', Arial, sans-serif;
117118
-webkit-font-smoothing: antialiased;
118119
font-weight: 500;
119-
font-size: 16px;
120+
font-size: 12px;
120121
color: #FFFFFF;
121122
border: none;
122123
color: #FFF;
123124
background: #719EA8;
124125
box-shadow: 0px 2px 4px 0px rgba(0,0,0,0.50);
125126
border-radius: 2px;
127+
padding: 7px 12px 5px 12px
128+
}
129+
130+
.button--generate {
131+
font-weight: 500;
132+
font-size: 16px;
126133
padding: 8px 16px 10px 16px;
134+
align-self: stretched;
135+
margin-left: auto;
127136
}
128137

129-
.status {
138+
.button--configure {
139+
margin-right: auto;
140+
}
141+
142+
.button--ok {
143+
margin: 0 auto;
144+
}
145+
146+
.subpage {
130147
position: fixed;
131148
top: 0;
132149
left: 0;
@@ -136,20 +153,23 @@ body {
136153
pointer-events: none;
137154
display: flex;
138155
flex-direction: column;
139-
align-items: center;
140-
justify-content: center;
141156
opacity: 0;
142157
will-change: opacity;
143158
transition: opacity 0.150s cubic-bezier(0,0,0.41,1);
144159
text-align: center;
145160
padding: 20px;
146161
}
147162

148-
.status--visible {
163+
.subpage--visible {
149164
opacity: 1;
150165
pointer-events: auto;
151166
}
152167

168+
.status {
169+
align-items: center;
170+
justify-content: center;
171+
}
172+
153173
.status__msg {
154174
font-size: 16px;
155175
margin: 8px;
@@ -164,9 +184,26 @@ body {
164184
}
165185

166186
.feedback {
187+
background: #49525F;
167188
position: absolute;
168-
bottom: 16px;
189+
bottom: 10px;
169190
left: 16px;
170191
width: 180px;
171192
line-height: 1.45;
172193
}
194+
195+
/* 1. scrollbar when extension is too small */
196+
.options {
197+
overflow: auto; /* [1] */
198+
display: block;
199+
}
200+
201+
.options__title {
202+
margin: 0 auto;
203+
}
204+
205+
.options__list {
206+
padding: 0;
207+
list-style: none;
208+
text-align: left;
209+
}

0 commit comments

Comments
 (0)