forked from ampproject/amphtml
/
experiments.js
403 lines (367 loc) · 12 KB
/
experiments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import '../../third_party/babel/custom-babel-helpers';
import '../../src/polyfills';
import {dev, initLogConstructor} from '../../src/log';
import {getCookie, setCookie} from '../../src/cookies';
import {getMode} from '../../src/mode';
import {isExperimentOn, toggleExperiment} from '../../src/experiments';
import {listenOnce} from '../../src/event-helper';
import {onDocumentReady} from '../../src/document-ready';
//TODO(@cramforce): For type. Replace with forward declaration.
import '../../src/service/timer-impl';
initLogConstructor();
const COOKIE_MAX_AGE_DAYS = 180; // 6 month
/**
* @typedef {{
* id: string,
* name: string,
* spec: string
* }}
*/
let ExperimentDef;
/**
* This experiment is special because it uses a different mechanism that is
* interpreted by the server to deliver a different version of the AMP
* JS libraries.
*/
const CANARY_EXPERIMENT_ID = 'dev-channel';
/** @const {!Array<!ExperimentDef>} */
const EXPERIMENTS = [
// Canary (Dev Channel)
{
id: CANARY_EXPERIMENT_ID,
name: 'AMP Dev Channel (more info)',
spec: 'https://github.com/ampproject/amphtml/blob/master/' +
'README.md#amp-dev-channel',
},
{
id: 'alp',
name: 'Activates support for measuring incoming clicks.',
spec: 'https://github.com/ampproject/amphtml/issues/2934',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4005',
},
{
id: 'amp-experiment',
name: 'AMP Experiment',
spec: 'https://github.com/ampproject/amphtml/blob/master/' +
'extensions/amp-experiment/amp-experiment.md',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4004',
},
{
id: 'amp-fx-flying-carpet',
name: 'AMP Flying Carpet',
spec: 'https://github.com/ampproject/amphtml/blob/master/' +
'extensions/amp-fx-flying-carpet/amp-fx-flying-carpet.md',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4003',
},
{
id: 'amp-access-server',
name: 'AMP Access server side prototype',
spec: '',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4000',
},
{
id: 'amp-access-jwt',
name: 'AMP Access JWT prototype',
spec: '',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4000',
},
{
id: 'amp-access-signin',
name: 'AMP Access sign-in',
spec: 'https://github.com/ampproject/amphtml/issues/4227',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4226',
},
{
id: 'amp-scrollable-carousel',
name: 'AMP carousel using horizontal scroll',
spec: '',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4232',
},
{
id: 'form-submit',
name: 'Global document form submit handler',
spec: 'https://github.com/ampproject/amphtml/issues/3343',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/3999',
},
{
id: 'amp-form',
name: 'AMP Form Extension',
spec: 'https://github.com/ampproject/amphtml/issues/3343',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/3998',
},
{
id: 'amp-form-custom-validations',
name: 'AMP Form Custom Validations',
spec: 'https://github.com/ampproject/amphtml/issues/3343',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/5423',
},
{
id: 'amp-google-vrview-image',
name: 'AMP VR Viewer for images via Google VRView',
spec: 'https://github.com/ampproject/amphtml/blob/master/extensions/' +
'amp-google-vrview-image/amp-google-vrview-image.md',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/3996',
},
{
id: 'no-auth-in-prerender',
name: 'Delay amp-access auth request until doc becomes visible.',
spec: '',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/3824',
},
{
id: 'amp-share-tracking',
name: 'AMP Share Tracking',
spec: 'https://github.com/ampproject/amphtml/issues/3135',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/5167',
},
{
id: 'amp-viz-vega',
name: 'AMP Visualization using Vega grammar',
spec: 'https://github.com/ampproject/amphtml/issues/3991',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4171',
},
{
id: 'amp-apester-media',
name: 'AMP extension for Apester media',
spec: 'https://github.com/ampproject/amphtml/issues/3233',
cleanupIssue: 'https://github.com/ampproject/amphtml/pull/4291',
},
{
id: 'amp-app-banner',
name: 'Shows a native app install/open banner.',
spec: 'https://github.com/ampproject/amphtml/issues/800',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/5166',
},
{
id: 'cache-service-worker',
name: 'AMP Cache Service Worker',
spec: 'https://github.com/ampproject/amphtml/issues/1199',
},
{
id: 'amp-lightbox-viewer',
name: 'Enables a new lightbox experience via the `lightbox` attribute',
spec: 'https://github.com/ampproject/amphtml/issues/4152',
},
{
id: 'amp-lightbox-viewer-auto',
name: 'Allows the new lightbox experience to automatically include some ' +
'elements without the need to manually add the `lightbox` attribute',
spec: 'https://github.com/ampproject/amphtml/issues/4152',
},
{
id: 'amp-fresh',
name: 'Guaranteed minimum freshness on sections of a page',
spec: '',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4715',
},
{
id: 'make-body-block',
name: 'Sets the body to display:block.',
spec: 'https://github.com/ampproject/amphtml/issues/5310',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/5319',
},
{
id: 'make-body-relative',
name: 'Sets the body to position:relative.',
spec: 'https://github.com/ampproject/amphtml/issues/5667',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/5660',
},
{
id: 'link-url-replace',
name: 'Enables replacing variables in URLs of outgoing links.',
spec: 'https://github.com/ampproject/amphtml/issues/4078',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/5627',
},
{
id: 'pan-y',
name: 'Sets "touch-action: pan-y" on doc root to enable passive' +
' touch handlers',
spec: 'https://github.com/ampproject/amphtml/issues/4820',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4894',
},
{
id: 'alp-for-a4a',
name: 'Enable redirect to landing page directly for A4A',
spec: 'https://github.com/ampproject/amphtml/issues/5212',
},
{
id: 'ios-embed-wrapper',
name: 'A new iOS embedded viewport model that wraps the body into' +
' a synthetic root',
spec: '',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/5639',
},
{
id: 'chunked-amp',
name: 'Split AMP\'s loading phase into chunks',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/5535',
},
{
id: 'amp-sticky-ad-better-ux',
name: 'New breaking UX changes make to amp-sticky-ad',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/5822',
},
];
if (getMode().localDev) {
EXPERIMENTS.forEach(experiment => {
dev().assert(experiment.cleanupIssue, `experiment ${experiment.name} must` +
' have a `cleanupIssue` field.');
});
}
/**
* Builds the expriments tbale.
*/
function build() {
const table = document.getElementById('experiments-table');
EXPERIMENTS.forEach(function(experiment) {
table.appendChild(buildExperimentRow(experiment));
});
}
/**
* Builds one row of the experiments table.
* @param {!ExperimentDef} experiment
*/
function buildExperimentRow(experiment) {
const tr = document.createElement('tr');
tr.id = 'exp-tr-' + experiment.id;
const tdId = document.createElement('td');
tdId.appendChild(buildLinkMaybe(experiment.id, experiment.spec));
tr.appendChild(tdId);
const tdName = document.createElement('td');
tdName.appendChild(buildLinkMaybe(experiment.name, experiment.spec));
tr.appendChild(tdName);
const tdOn = document.createElement('td');
tdOn.classList.add('button-cell');
tr.appendChild(tdOn);
const button = document.createElement('button');
tdOn.appendChild(button);
const buttonOn = document.createElement('div');
buttonOn.classList.add('on');
buttonOn.textContent = 'On';
button.appendChild(buttonOn);
const buttonOff = document.createElement('div');
buttonOff.classList.add('off');
buttonOff.textContent = 'Off';
button.appendChild(buttonOff);
button.addEventListener('click', toggleExperiment_.bind(null, experiment.id,
experiment.name, undefined));
return tr;
}
/**
* If link is available, builds the anchor. Otherwise, it'd return a basic span.
* @param {string} text
* @param {?string} link
* @return {!Element}
*/
function buildLinkMaybe(text, link) {
let element;
if (link) {
element = document.createElement('a');
element.setAttribute('href', link);
element.setAttribute('target', '_blank');
} else {
element = document.createElement('span');
}
element.textContent = text;
return element;
}
/**
* Updates states of all experiments in the table.
*/
function update() {
EXPERIMENTS.forEach(function(experiment) {
updateExperimentRow(experiment);
});
}
/**
* Updates the state of a single experiment.
* @param {!ExperimentDef} experiment
*/
function updateExperimentRow(experiment) {
const tr = document.getElementById('exp-tr-' + experiment.id);
if (!tr) {
return;
}
tr.setAttribute('data-on', isExperimentOn_(experiment.id) ? 1 : 0);
}
/**
* Returns whether the experiment is on or off.
* @param {string} id
* @return {boolean}
*/
function isExperimentOn_(id) {
if (id == CANARY_EXPERIMENT_ID) {
return getCookie(window, 'AMP_CANARY') == '1';
}
return isExperimentOn(window, id);
}
/**
* Toggles the experiment.
* @param {string} id
* @param {boolean=} opt_on
*/
function toggleExperiment_(id, name, opt_on) {
const currentlyOn = isExperimentOn_(id);
const on = opt_on === undefined ? !currentlyOn : opt_on;
// Protect against click jacking.
const confirmMessage = on ?
'Do you really want to activate the AMP experiment' :
'Do you really want to deactivate the AMP experiment';
showConfirmation_(`${confirmMessage}: "${name}"`, () => {
if (id == CANARY_EXPERIMENT_ID) {
const validUntil = Date.now() +
COOKIE_MAX_AGE_DAYS * 24 * 60 * 60 * 1000;
setCookie(window, 'AMP_CANARY', (on ? '1' : '0'), (on ? validUntil : 0));
} else {
toggleExperiment(window, id, on);
}
update();
});
}
/**
* Shows confirmation and calls callback if it's approved.
* @param {string} message
* @param {function()} callback
*/
function showConfirmation_(message, callback) {
const container = dev().assert(document.getElementById('popup-container'));
const messageElement = dev().assert(document.getElementById('popup-message'));
const confirmButton = dev().assert(
document.getElementById('popup-button-ok'));
const cancelButton = dev().assert(
document.getElementById('popup-button-cancel'));
const unlistenSet = [];
const closePopup = affirmative => {
container.classList.remove('show');
unlistenSet.forEach(unlisten => unlisten());
if (affirmative) {
callback();
}
};
messageElement.textContent = message;
unlistenSet.push(listenOnce(confirmButton, 'click',
() => closePopup(true)));
unlistenSet.push(listenOnce(cancelButton, 'click',
() => closePopup(false)));
container.classList.add('show');
}
// Start up.
onDocumentReady(document, () => {
build();
update();
});