0

I am trying to run a puppeteer function on my HTML page and I always get the same 'require is not defined' error when I try to run my puppeteer function. I have tried the solution from this post: How to run Puppeteer code in any web browser?, But I need a way to do this without connecting to a WebSocket.

My puppeteer function:

puppeteer = require('puppeteer'); //the line that is causing the error.
var k = 1;
var z = 0;
var v = 2000;
var x = Math.floor(Math.random() * 6000 + 105000);
var j = 86400000 / (v) - x;
var l = x / 14000;
var k = 1;

async function start() {
      const url = 'https://www.youtube.com/watch?v=YHniMnZIlzg';
      const browser = await puppeteer.launch({
         args: ['--no-sandbox'],
         headless: true,
      });
      planA: for(var g = 3000; g > 0; g--){
      const page = await browser.newPage();
      page.setDefaultNavigationTimeout(0);
      await page.goto(url);
      await page.setViewport({ width: 1000, height: 700});
      await page.keyboard.type(' ');
      if (z < 1){
        await page.click('#toggleButton');
        z = z + 1;
      }
      for(var c = Math.floor(x / 14000); c > 0; c--){
        var h = x / l;
        console.log(h);
        await page.waitFor(h);
        element = ('.ytp-ad-skip-button-container');
        try{
          await page.click(element);
          console.log('ad');
        }
        catch{
          console.log('No ad');
        }
      }
      console.log(k);
      k += 1;
      try{
        await page.close();
      }
      catch{
        console.log('unable to close page');
      }
      finally{
        continue planA;
      }
    }
      browser.close();
}
hardkoded
  • 18,915
  • 3
  • 52
  • 64
coder420
  • 163
  • 3
  • 8
  • 2
    If I understand correctly what you want to do, then you want to run puppeteer from a webpage in someone's browser, without having puppeteer even installed on that computer? That is not possible. – CherryDT Jun 25 '20 at 16:28
  • 2
    Node is not javascript in a browser, you can't just run any Node code in a browser. Also, please read the full answer in the solution you posted above, it does a good job of explaining how Puppeteer works, and how you can use it in any browser. – nzajt Jun 25 '20 at 16:55
  • Does this answer your question? [How to run Puppeteer code in any web browser?](https://stackoverflow.com/questions/54647694/how-to-run-puppeteer-code-in-any-web-browser) – ggorlen Dec 30 '22 at 16:55

0 Answers0