V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
longjiahui
V2EX  ›  Node.js

请问朋友们 puppeteer 的一个问题

  •  
  •   longjiahui · 2020-03-16 05:27:44 +08:00 · 4620 次点击
    这是一个创建于 1495 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请问 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
    
    第 1 条附言  ·  2020-03-16 23:17:02 +08:00

    我后来改用了 创建server的方式来做比较方便。

    5 条回复    2020-03-17 11:51:47 +08:00
    qhxin
        1
    qhxin  
       2020-03-16 11:16:10 +08:00
    我是这样处理的,在调用之前会先对 url 做校验和过滤,不合法的 url 不调用
    Bartholomew
        2
    Bartholomew  
       2020-03-16 14:31:46 +08:00
    请教一下,findChrome 文件里是什么逻辑啊
    yamedie
        3
    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
        4
    longjiahui  
    OP
       2020-03-16 23:20:33 +08:00
    @qhxin 我本意是想访问一个不合法的 url,因为 scheme 不是标准的 scheme,但是因为 根本不会发出请求,所以不会进入请求回调,我想这么做的原因主要是因为之前在用 electron 的时候,它提供了 Protocol 模块来手动注册非标准的 scheme,这样可以在不开服务器的情况下适配许多前端的路由问题
    Bartholomew
        5
    Bartholomew  
       2020-03-17 11:51:47 +08:00
    @yamedie 好的 感谢🙏
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1774 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:32 · PVG 00:32 · LAX 09:32 · JFK 12:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.