longjiahui
V2EX  ›  Node.js

请问朋友们 puppeteer 的一个问题

  •  
  •   longjiahui · Mar 16, 2020 · 5742 views
    This topic created in 2293 days ago, the information mentioned may be changed or developed.

    请问 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
    
    Supplement 1  ·  Mar 16, 2020

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

    5 replies    2020-03-17 11:51:47 +08:00
    qhxin
        1
    qhxin  
       Mar 16, 2020
    我是这样处理的,在调用之前会先对 url 做校验和过滤,不合法的 url 不调用
    Bartholomew
        2
    Bartholomew  
       Mar 16, 2020
    请教一下,findChrome 文件里是什么逻辑啊
    yamedie
        3
    yamedie  
       Mar 16, 2020
    @csdefault1997 搜到了, carlo
    https://github.com/GoogleChromeLabs/carlo

    npm i carlo
    const findChrome = require('./node_modules/carlo/lib/find_chrome');
    longjiahui
        4
    longjiahui  
    OP
       Mar 16, 2020
    @qhxin 我本意是想访问一个不合法的 url,因为 scheme 不是标准的 scheme,但是因为 根本不会发出请求,所以不会进入请求回调,我想这么做的原因主要是因为之前在用 electron 的时候,它提供了 Protocol 模块来手动注册非标准的 scheme,这样可以在不开服务器的情况下适配许多前端的路由问题
    Bartholomew
        5
    Bartholomew  
       Mar 17, 2020
    @yamedie 好的 感谢🙏
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2766 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 93ms · UTC 14:24 · PVG 22:24 · LAX 07:24 · JFK 10:24
    ♥ Do have faith in what you're doing.