简体   繁体   中英

How to have a javascript function running in page context call puppeteer functions?

I am trying to make a bot that automatically plays a game on a web page. As of right now, I can navigate to the website, sign in, and load the game page, but I am stuck here.

I would like to inject a script at the webpage level that uses jQuery to constantly scrape the webpage and determine what state the game is in. When a certain event happens, I want the script to fire a custom event(?) that would notify a function at the Node.js./Puppeteer level to execute. My problem is that I do not understand how to make Puppeteer react when a custom event from the page is fired.

For example, let's say we have a webpage level function that is on a 1 second timeout that scrapes the webpage for X. When X is found, I want to scrape Y data off the webpage and send it up to the node level. When Y is received at the node level, it moves the mouse to Y location, or do something else with Y that cannot be done at the browser level.

I'm not sure if this is the most appropriate way to handle this kind of task, but trying to understand how async and promises work on the Node.js level without being able to use jQuery to select elements is giving me an advanced form of terminal ebola AIDS.

This answer has couple of different ways how to send data back from an injected script to your Node.js code:

Communicate "out" from Chromium via DevTools protocol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM