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

请问朋友们 puppeteer 的一个问题

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

    请问 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     Solana     3304 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 11:47 PVG 19:47 LAX 04:47 JFK 07:47
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86