代码之家  ›  专栏  ›  技术社区  ›  hretic

暂停PoPPETER进程直到全局var值从false变为字符串

  •  1
  • hretic  · 技术社区  · 7 年前

    我在nodejs服务器中运行puppeter…我们的想法是打开一个页面,通过socket.io读取并发送验证码给用户,当收到用户输入的值验证码后,就可以继续进行木偶制作了。这是我的代码的简化版本

       var captcha = false ;
    
        io.on('connection' , function(socket){
    
            socket.on('start_puppeteer' , function (data) {
                io.emit('push_msg' , {text : 'starting puppeteer  ....  '  });
                run(io);
            })
    
            socket.on('get_captcha_from_client' , function (data) {
    
                captcha = data.text ;
                console.log( 'capthca set : ' + captcha);
            })
    
        });
    
        function get_captcha() {
            return new Promise(function(resolve , reject ) {
                if(captcha === false )
                    reject(captcha );
                else
                    resolve(captcha);
            });
        }
    
    
    async function run(io) {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
    
        await page.goto('http://localhost');
    
        console.log( ' sending captcha to client ' );
    
        io.emit('send_captcha_to_client' , {text : 'captcha text'  });
    
        var captcha = await get_captcha();
    
        console.log( ' captcha confirm -> ' + captcha );
    
        browser.close();
    }
    

    问题是,即使设置了验证码,代码也不会移动,我也无法获得 captcha confirm 在控制台

    $ node pop.js
     sending captcha to client
    (node:9356) UnhandledPromiseRejectionWarning: false
    (node:9356) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
     (rejection id: 3)
    (node:9356) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    capthca set : 1234
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Vaviloff    7 年前

    page.waitForFunction 评估函数 in browser context ,也就是说,您可以检查是否存在某个dom节点或某个输入值,但您只能访问该函数中的页面对象。

    现在如果你只想等验证码被破解,让你的 get_captcha 返回 Promise await 对于其结果:

    function get_captcha() {
        return new Promise(resolve => {
    
            // ...Do something lengthy here...
    
            resolve(captcha);
        });
    }
    
    async function run(io) {
        // ....
    
        await page.goto('http://localhost');
        io.emit('send_captcha_to_client' , {text : 'captcha text'  });
        var captcha = await get_captcha();
    
        // ....
    }
    
        2
  •  0
  •   hretic    7 年前

    Thanx to@Vaviloff and@Yaniv Efraim我解决了这样的问题

    var fullfillCaptcha;
    var captchaPromise = new Promise(resolve => fullfillCaptcha = resolve);
    
    
        socket.on('get_captcha_from_client' , function (data) {
    
            fullfillCaptcha(data.text);
        })
    

    在里面 async function run

      var captcha = await captchaPromise;