请问朋友们 puppeteer 的一个问题

2020-03-16 05:27:44 +08:00
 longjiahui

请问 puppeteer 能不能 在 page goto 一个非标准 scheme 的 uri 时做 interception 代码如下,无法在 page.on('request', xxx)中监听到 request。 而是直接报错

const puppeteer = require('puppeteer-core');
const findChrome = require('./findChrome');

(async ()=>{
    const { executablePath, type } = await findChrome({});
    if (!executablePath) {
      console.error('Could not find Chrome installation, please make sure Chrome browser is installed from https://www.google.com/chrome/.');
      process.exit(0);
      return;
    }
  
    const browser = await puppeteer.launch({
        executablePath,
        headless: false,
        defaultViewport: null,
        userDataDir: '.local-data',
    });
    let pages = await browser.pages();
    let page = pages[0];
    page.on('request', (request)=>{
        console.log(request); // 👈 到达不了这里
        request.continue();
    });
    page.on('requestfinished', (request)=>{
        console.log(request);
    })
    await page.setRequestInterception(true);
    try{
        await page.goto('app://index.html');
    }catch(err){
        console.error(err);
    }
})();

报错信息:

Error: net::ERR_ABORTED at app://index.html
4631 次点击
所在节点    Node.js
5 条回复
qhxin
2020-03-16 11:16:10 +08:00
我是这样处理的,在调用之前会先对 url 做校验和过滤,不合法的 url 不调用
Bartholomew
2020-03-16 14:31:46 +08:00
请教一下,findChrome 文件里是什么逻辑啊
yamedie
2020-03-16 16:52:35 +08:00
@csdefault1997 搜到了, carlo
https://github.com/GoogleChromeLabs/carlo

npm i carlo
const findChrome = require('./node_modules/carlo/lib/find_chrome');
longjiahui
2020-03-16 23:20:33 +08:00
@qhxin 我本意是想访问一个不合法的 url,因为 scheme 不是标准的 scheme,但是因为 根本不会发出请求,所以不会进入请求回调,我想这么做的原因主要是因为之前在用 electron 的时候,它提供了 Protocol 模块来手动注册非标准的 scheme,这样可以在不开服务器的情况下适配许多前端的路由问题
Bartholomew
2020-03-17 11:51:47 +08:00
@yamedie 好的 感谢🙏

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/653124

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX