'Reuse a puppeteer instance (created by another module, Node.js)

The following Q&A's are related, but don't quite answer my question:

Also, it seems to me that this topic goes beyond this specific use case, probably a thing of Node itself, so I will be happy for links to a broader explanation.

Anyway: My Node app uses a module, which uses Puppeteer. I can supply launch params to it, but cannot access it later (or at least don't know how, or if even possible). This obviously runs it, creates a new page, and does its thing. Then I need to use Puppeteer directly, to render specific HTML given to it. Now, I could puppeteer.launch() and browser.newPage(), but this would create another Puppeteer instance (..right?). How can use/access the instance created by the mentioned module? Or, is there a way to create a single Pup instance and have other code use it by default, perhaps?



Solution 1:[1]

Ok,so trying further to answer my question I traversed through variables of the module which spawns puppeteer. In my case, we this is whatsapp-web.js. When the client is initialize()d, waclient.pupBrowser._connection._url is the value to pass to browserWSEndpoint parameter of puppeteer.connect(options). The browser then can be const browser = await pup.connect({browserWSEndpoint: waBrowserURL});. Also, const pup = require('puppeteer') is necessary.

Or better, I can just directly use waclient.pupBrowser, which is the browser instance created by whatsapp-web.js.

So, while this does solve my case, I am not sure how to proceed in scenarios, where the browser is not exposed/accessible as described above.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marko-36