I'm new to Puppeteer and have few problems with it.
Generally, I want to control the script with user input, e.g while the script is running tell it to change page or print element's contents. It will look like this:
- Run puppeteer script, open browser and page
- Let page do what it's doing
- Wait for user input e.g: [ >> changePage example.com ]
- Parse and execute user command e.g: [ await page.goto('example.com') ]
Here's what I'm trying to achieve, code below is just a pseudo-code.
const puppeteer = require('puppeteer');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin
});
function parse_user_input(user_str) // executes user commands
{
user_args = user_str.split(' ');
if (user_args[0] == "changePage")
{
await page.goto(user_args[1]);
}
}
function get_user_input() // returns user input
{
return rl.question('>> ');
}
(async() => {
// code for opening the browser and page (already written)
while (true) // I don't want to block the running page
{ // (in real code this gets wild and doesn't wait for input)
user_str = get_user_input();
parse_user_input(user_str);
}
});
Thanks for all your suggestions!