chromedp / chromedp

A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to modify response in ajax

tuwibu opened this issue · comments

What versions are you running?

$ go list -m github.com/chromedp/chromedp
<!--replace this line with the output-->
$ google-chrome --version
<!--replace this line with the output-->
$ go version
<!--replace this line with the output-->

What did you do? Include clear steps.

I want convert this code to chromedp, how to do that

import axios from 'axios';
import puppeteer from 'puppeteer';
(async () => {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  await page.setRequestInterception(true);
  page.on('request', async (request) => {
    if (request.url.includes('/ajax/login')) {
      const response = await axios({
        method: request.method(),
        url: request.url(),
        headers: request.headers(),
        data: request.postData()
      });
      // modify response here
      const data = {
        ...response.data,
        msg: 'hello world'
      };

      await page.setRequestInterception(false);
      await page.removeAllListeners('request');

      await request.respond({
        status: response.status,
        headers: response.headers,
        contentType: response.headers['content-type'],
        body: JSON.stringify(data)
      });
    }
  })
})()
chromedp.ListenTarget(page.Ctx, func(ev interface{}) {
	switch ev := ev.(type) {
	case *fetch.EventRequestPaused:
		go func(ev *fetch.EventRequestPaused) {
			fetchRequest := fetch.ContinueRequest(ev.RequestID)
			// to reduce the noises in the log
			if ev.ResourceType == "Document" {
				fetchRequest.InterceptResponse = true
				// ... i dont know modify response
			}
			_ = chromedp.Run(page.Ctx, fetchRequest)
		}(ev)
	}
})

What did you expect to see?

What did you see instead?