diff --git a/front_end/ndb/NetworkInterceptor.js b/front_end/ndb/NetworkInterceptor.js index 3a2b8a49..9a5e63ad 100644 --- a/front_end/ndb/NetworkInterceptor.js +++ b/front_end/ndb/NetworkInterceptor.js @@ -35,7 +35,7 @@ Ndb.NetworkInterceptor = class extends Ndb.ConnectionInterceptor { _sendRawMessage(rawMessage) {} - _listen() { + async _listen() { InspectorFrontendHost.sendMessageToBackend = rawMessage => { const message = JSON.parse(rawMessage); @@ -54,7 +54,7 @@ Ndb.NetworkInterceptor = class extends Ndb.ConnectionInterceptor { id: message.id, result: { base64Encoded: true, - body: request.payload.data + body: 'foo' } } ); @@ -63,43 +63,39 @@ Ndb.NetworkInterceptor = class extends Ndb.ConnectionInterceptor { // we need to setTimeout here because the httpMonkeyPatchingSource is loaded // after this script - setTimeout(async() => { - while (this._target) { - try { - const raw = await this._target - .runtimeAgent() - .invoke_evaluate({ - expression: `process._fetchNetworkMessages()`, - awaitPromise: true, - returnByValue: true - }); - - const { - result: { value: messages } - } = raw; - - if (!messages) return; - - // messages is array-like - const messagesArr = Array.from(JSON.parse(messages)); - - for (const message of messagesArr) { - const { type, payload } = message; - this._cacheRequests.push(message); - - // this is on the way back, this way doesn't work - if (type !== 'Network.getResponseBody') { - // but this does - SDK._mainConnection._onMessage(JSON.stringify({ - method: type, - params: payload - })); - } - } - } catch (err) { - console.log({ err }); + while (this._target) { + const rawResponse = await this._target + .runtimeAgent() + .invoke_evaluate({ + expression: `process._fetchNetworkMessages()`, + awaitPromise: true, + returnByValue: true + }); + + if (!rawResponse || !rawResponse.result) return; + + const { + result: { value: messages } + } = rawResponse; + + if (!messages) return; + + // messages is array-like + const messagesArr = Array.from(JSON.parse(messages)); + + for (const message of messagesArr) { + const { type, payload } = message; + this._cacheRequests.push(message); + + // this is on the way back, this way doesn't work + if (type !== 'Network.getResponseBody') { + // but this does + SDK._mainConnection._onMessage(JSON.stringify({ + method: type, + params: payload + })); } } - }, 0); + } } };