diff --git a/README.md b/README.md index fdf6ae3..2a27c97 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Please note that Node v14+ is required. ```js const { Client } = require('whatsapp-web.js'); + const client = new Client(); client.on('qr', (qr) => { @@ -37,6 +38,39 @@ client.on('message', msg => { client.initialize(); ``` +## Remote Access + +You could also connect to any previously existing browser instance: + +```js +const client = new Client({ + puppeteer: { + browserWSEndpoint: `ws://localhost:3000` + } +}); +``` + +### Docker + +1) Installing a browser using browserless: + +``` +docker run \ + --rm \ + -p 3000:3000 \ + -e "MAX_CONCURRENT_SESSIONS=1" \ + browserless/chrome:latest +``` + +Reference: https://docs.browserless.io/docs/docker-quickstart.html + +### Remote Debugging + +2) Running a browser with websocket remote debugging enabled: +> chrome.exe --remote-debugging-port=9222 + +After that check the following webpage and check http://127.0.0.1:9220/json and get the **webSocketDebuggerUrl** + Take a look at [example.js](https://github.com/pedroslopez/whatsapp-web.js/blob/master/example.js) for another example with more use cases. ## Supported features diff --git a/example.js b/example.js index 803bcb9..8f87101 100644 --- a/example.js +++ b/example.js @@ -11,6 +11,13 @@ const client = new Client({ puppeteer: { headless: false }, session: sessionCfg // You can use an existing session and avoid scanning a QR code by adding a "session" object to the client options. // This object must include WABrowserId, WASecretBundle, WAToken1 and WAToken2. +// You also could connect to an existing instance of a browser +// { +// puppeteer: { +// browserWSEndpoint: `ws://localhost:3000` +// } +// } + client.initialize(); client.on('qr', (qr) => { diff --git a/src/Client.js b/src/Client.js index 690a5b2..67955cf 100644 --- a/src/Client.js +++ b/src/Client.js @@ -65,8 +65,16 @@ class Client extends EventEmitter { * Sets up events and requirements, kicks off authentication request */ async initialize() { - const browser = await puppeteer.launch(this.options.puppeteer); - const page = (await browser.pages())[0]; + let [browser, page] = [null, null]; + + if(this.options.puppeteer && this.options.puppeteer.browserWSEndpoint) { + browser = await puppeteer.connect(this.options.puppeteer); + page = await browser.newPage(); + } else { + browser = await puppeteer.launch(this.options.puppeteer); + page = (await browser.pages())[0]; + } + page.setUserAgent(this.options.userAgent); this.pupBrowser = browser;