Executing JavaScript code
The first question you might ask is: "Why would I need to run JavaScript code? Shouldn't Puppeteer give me all the APIs I need?" Well, yes and no.
Before getting into the different possible use cases, let's see how this feature works.
Variable scopes in JavaScript
One thing that makes JavaScript so flexible is that functions are first-class citizens. You can declare functions, assign them to variables, and pass them as an argument. You could even return functions from other functions, like in this example:
function getFunc() { let word = 'world'; return function() { console.log('Hello ' + word); } } getFunc()();
That code is pretty fun. getFunc
returns another function. When we do getFunc()()
, we are calling the function returned by getFunc
.
This piece of code will print 'Hello...