From 74690f1ec09d7c40607aea87ccda190ceb017caa Mon Sep 17 00:00:00 2001 From: Paul Lewis Date: Wed, 17 Aug 2016 18:57:32 +0100 Subject: [PATCH] Moves from XHR to DevTools Protocol for manifest retrieval (#600) * Moves from XHR to DevTools Protocol for manifest --- lighthouse-core/gather/gatherers/manifest.js | 59 ++++--------------- .../test/gather/gatherers/manifest.js | 17 +++--- 2 files changed, 19 insertions(+), 57 deletions(-) diff --git a/lighthouse-core/gather/gatherers/manifest.js b/lighthouse-core/gather/gatherers/manifest.js index 495f6c926b33..490bdd0c856a 100644 --- a/lighthouse-core/gather/gatherers/manifest.js +++ b/lighthouse-core/gather/gatherers/manifest.js @@ -19,40 +19,6 @@ const Gatherer = require('./gatherer'); const manifestParser = require('../../lib/manifest-parser'); -/* global document, XMLHttpRequest, __returnResults */ - -/* istanbul ignore next */ -function getManifestContent() { - function post(response) { - // __returnResults is magically inserted by driver.evaluateAsync - __returnResults(response); - } - - const manifestNode = document.querySelector('link[rel=manifest]'); - if (!manifestNode) { - return post({error: 'No found in DOM.'}); - } - - const manifestURL = manifestNode.href; - if (!manifestURL) { - return post({error: 'No href found on .'}); - } - - const req = new XMLHttpRequest(); - req.open('GET', manifestURL); - req.onload = function() { - if (req.status !== 200) { - return post({ - error: `Unable to fetch manifest at \ - ${manifestURL}: ${req.status} - ${req.statusText}` - }); - } - - post({manifestContent: req.response}); - }; - req.send(); -} - class Manifest extends Gatherer { static _errorManifest(errorString) { @@ -70,23 +36,18 @@ class Manifest extends Gatherer { * potentially lead to a different asset. Using the original manifest * resource is tracked in issue #83 */ - return driver.evaluateAsync(`(${getManifestContent.toString()}())`) - - .then(returnedValue => { - if (!returnedValue) { + return driver.sendCommand('Page.getAppManifest') + .then(response => { + if (response.errors.length) { + this.artifact = Manifest._errorManifest(response.errors.join(', ')); + return; + } + + this.artifact = manifestParser(response.data); + }, _ => { this.artifact = Manifest._errorManifest('Unable to retrieve manifest'); return; - } - - if (returnedValue.error) { - this.artifact = Manifest._errorManifest(returnedValue.error); - } else { - this.artifact = manifestParser(returnedValue.manifestContent); - } - }, _ => { - this.artifact = Manifest._errorManifest('Unable to retrieve manifest'); - return; - }); + }); } } diff --git a/lighthouse-core/test/gather/gatherers/manifest.js b/lighthouse-core/test/gather/gatherers/manifest.js index 279633690e69..827a6bb8de49 100644 --- a/lighthouse-core/test/gather/gatherers/manifest.js +++ b/lighthouse-core/test/gather/gatherers/manifest.js @@ -34,8 +34,8 @@ describe('Manifest gatherer', () => { it('returns an artifact', () => { return manifestGather.afterPass({ driver: { - evaluateAsync() { - return Promise.resolve(''); + sendCommand() { + return Promise.resolve({data: '', errors: [], url: 'https://example.com/manifest.json'}); } } }).then(_ => { @@ -46,7 +46,7 @@ describe('Manifest gatherer', () => { it('handles driver failure', () => { return manifestGather.afterPass({ driver: { - evaluateAsync() { + sendCommand() { return Promise.reject('such a fail'); } } @@ -61,9 +61,9 @@ describe('Manifest gatherer', () => { const error = 'There was an error.'; return manifestGather.afterPass({ driver: { - evaluateAsync() { + sendCommand() { return Promise.resolve({ - error + errors: [error] }); } } @@ -73,14 +73,15 @@ describe('Manifest gatherer', () => { }); it('creates a manifest object for valid manifest content', () => { - const manifestContent = JSON.stringify({ + const data = JSON.stringify({ name: 'App' }); return manifestGather.afterPass({ driver: { - evaluateAsync() { + sendCommand() { return Promise.resolve({ - manifestContent + errors: [], + data }); } }