* Loads the AMP_CONFIG objects from whatever the v0.js is that the user has * (depends on whether they opted into one of the pre-release channels or into a * specific RTV) so that experiment state can reflect the default activated * experiments. * @return {Promise } the active AMP_CONFIG, p
()
| 377 | * @return {Promise<JSON>} the active AMP_CONFIG, parsed as a JSON object |
| 378 | */ |
| 379 | function getAmpConfig() { |
| 380 | const deferred = new Deferred(); |
| 381 | const {promise, reject, resolve} = deferred; |
| 382 | const xhr = new XMLHttpRequest(); |
| 383 | xhr.addEventListener('load', () => { |
| 384 | resolve(xhr.responseText); |
| 385 | }); |
| 386 | xhr.addEventListener('error', () => { |
| 387 | reject(new Error(xhr.statusText)); |
| 388 | }); |
| 389 | // Cache bust, so we immediately reflect cookie changes. |
| 390 | xhr.open('GET', '/v0.js?' + Math.random(), true); |
| 391 | xhr.send(null); |
| 392 | return promise |
| 393 | .then((text) => { |
| 394 | const match = text.match(/self\.AMP_CONFIG=(\{.+?\})/); |
| 395 | if (!match) { |
| 396 | throw new Error("Can't find AMP_CONFIG in: " + text); |
| 397 | } |
| 398 | // Setting global var to make standard experiment code just work. |
| 399 | return (self.AMP_CONFIG = JSON.parse(match[1])); |
| 400 | }) |
| 401 | .catch((error) => { |
| 402 | console./*OK*/ error('Error fetching AMP_CONFIG', error); |
| 403 | return {}; |
| 404 | }); |
| 405 | } |
| 406 | |
| 407 | // Start up. |
| 408 | getAmpConfig().then(() => { |
no test coverage detected