请问 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
我后来改用了 创建server的方式来做比较方便。
![]() | 1 qhxin 2020-03-16 11:16:10 +08:00 我是这样处理的,在调用之前会先对 url 做校验和过滤,不合法的 url 不调用 |
![]() | 2 Bartholomew 2020-03-16 14:31:46 +08:00 请教一下,findChrome 文件里是什么逻辑啊 |
![]() | 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'); |
![]() | 4 longjiahui OP @qhxin 我本意是想访问一个不合法的 url,因为 scheme 不是标准的 scheme,但是因为 根本不会发出请求,所以不会进入请求回调,我想这么做的原因主要是因为之前在用 electron 的时候,它提供了 Protocol 模块来手动注册非标准的 scheme,这样可以在不开服务器的情况下适配许多前端的路由问题 |
![]() | 5 Bartholomew 2020-03-17 11:51:47 +08:00 @yamedie 好的 感谢 |