【问题标题】:How to proxy specific request in Puppeteer如何在 Puppeteer 中代理特定请求
【发布时间】:2019-04-17 16:06:11
【问题描述】:

有没有办法将特定的网址代理/重定向给其他人? 例如,当 Puppeteer 页面转到“mydomain.com”时,我希望所有对“mydomain.com/styles/.css”的调用都被代理到“localhost:8080/styles/.css”。 我不希望所有请求都通过代理重定向。但类似于 https://chrome.google.com/webstore/detail/resource-override/pkoacgokdfckfpndoffpifphamojphii?hl=en chrome 扩展所做的事情。

【问题讨论】:

标签: javascript node.js proxy puppeteer


【解决方案1】:

作为@Hellonearthis 链接,解决方案似乎是

const page = await browser.newPage();

await page.setRequestInterception(true);

page.on('request', (request) => {
  if (request.url().indexOf("mydomain.com") !== -1) {
    // simply replace with another url
    request.continue((request.url().replace("mydomain.com", "http://localhost:8080/styles"));
  }
  else {
    request.continue();
  }

});

【讨论】:

    猜你喜欢
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多