N8n, puppeteer, javascript and webhooks

I’m creating some puppeteer scripts and its all javascript and very similar to some of the stuff we use in the function nodes.

One question I have is when my script in puppeteer is finishing iterating its array the output I want to send to n8n webhook or something to get the results, but not sure how to output into a webhook

an example of the end of the script is like this

const url = [ingest n8n array output here]
   const results = await bluebird.map(urls, async (url) => {
   const page = await browser.newPage();
   await page.goto('{{$parameter["values"]["string"][0]["value"].toString().replace('=','')}}'+url);
   
   // You would add additional code to do stuff... 
   await page.setViewport({width: 1920,height: 1080,deviceScaleFactor: 1,});
   // await sleep(10000);
	// await page.screenshot({path: 'xyz-screenshot.png'});
   const html = await page.content();
   //fs.writeFileSync('xyz-source.htm', html); // create a random name here for each url output
   console.log(colors.green.bold('Script Completed'));
   return html;
   });```




the return html should have the array output results, I want to send this to a webhook for n8n to ingest (as the puppeteer host is not the same host as n8n)

any thoughts?

You can make an HTTP request to the webhook and include the HTML in the body.

Do you have any code I can borrow?

This is what i have so far:

const puppeteer = require('puppeteer');
const colors = require('colors/safe');
const fs = require('fs');
const bluebird = require("bluebird");



(async () => {
    const args = ['--proxy-server=socks5://127.0.0.1:9050'];
    const browser = await puppeteer.launch({ args });
    const page = await browser.newPage();
    await page.goto('https://check.torproject.org/');
    const isUsingTor = await page.$eval('body', el =>
        el.innerHTML.includes('Congratulations. This browser is configured to use Tor')
    );



    if (!isUsingTor) {
        console.log(colors.red.bold('Not using Tor. Closing...'));
        return await browser.close();
    }

    console.log(colors.green.bold('Using Tor. Continuing... '));

    // Now you can go wherever you want
    const urls = ['/xxxx/xxxxxx','/xxxx/xxxxx','etc']
    const results = await bluebird.map(urls, async (url) => {
    const page = await browser.newPage();
    await page.goto('http://xxxxxxxx.xxxxxxxxx'+url);
    const html = await page.content();
    return html;
    }, {concurrency: 3}); 

    // Then when you're done, just close
    await browser.close();

function sleep(ms) {
 return new Promise((resolve) => {
  setTimeout(resolve, ms);

 });

}
})();

Now trying to work out how to get the output of the return html into a webhook

aha think i have solved it now, webhook works