title: How to have a javascript function running in page context call puppeteer functions?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>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.</p>