c# – How can I create multiple web browser windows and access javascript objects from a main C # program?

Question:

How can I create multiple web browser windows and access javascript objects inside windows (or frames)?
The bottom line is that there is a service that performs certain calculations, but in order to use all the power, you need to keep 100-150 browser tabs open and at the same time monitor them for certain changes in the browser. Accordingly, you need to open certain pages in a web browser, get root access to them, and when events occur there, notify the main program. And the browser window itself can be skipped to save resources. I'm only interested in introducing hundreds of pages from a C # program (better) or Nodejs into the code.

Answer:

Chrome recently released a special headless browser mode, i.e. a full-fledged browser is launched, but without the UI. In parallel with this, they made a good nodejs lib to work with such a browser – puppeteer .

Here is a small example (more examples in the readme)

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');

  // Get the "viewport" of the page, as reported by the page.
  await page.evaluate(() => {
    return {
      width: document.documentElement.clientWidth,
      height: document.documentElement.clientHeight,
      deviceScaleFactor: window.devicePixelRatio
    };
  });

  await browser.close();
})();
Scroll to Top
AllEscort