Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

google chrome devtools - Puppeteer: How to do a proper request url override?

In reading the documentation about request override in the Puppeteer API, I see the following notes of caution: enter image description here

Furthermore, here is the code I use (in PuppeteerSharp) to do the request override:

        static void Main(string[] args)
        {
            Task.Run(async () =>
            {
                Browser b = await Puppeteer.LaunchAsync(new LaunchOptions()
                {
                    ExecutablePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
                    DefaultViewport = null,
                    Headless = false
                });

                Page[] pages = await b.PagesAsync();
                Page page = pages[0];

                await page.SetRequestInterceptionAsync(true);

                // Subscribe to requests
                page.Request += HandleRequestIntercepted;
            }
        }

        public static void HandleRequestIntercepted(object sender, RequestEventArgs args)
        {
            Payload payload = new Payload();

            if (args.Request.Url.Contains("adservice.google.com"))
                payload.Url = "https://www.amazon.com"; // NEW URL

            args.Request.ContinueAsync(payload);
        }

And the result is that the request URL is not changed (just like the original documentation described): enter image description here

How can I do a request URL override where the request URL in DevTools is overridden to the new URL?

question from:https://stackoverflow.com/questions/66056455/puppeteer-how-to-do-a-proper-request-url-override

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...