From a0b18fb685bee7e3b0ae8fbdb7da14129f631958 Mon Sep 17 00:00:00 2001 From: "Pedro S. Lopez" Date: Tue, 21 Jun 2022 00:01:59 -0400 Subject: [PATCH] fix: set user agent as a launch arg for usage in serviceworker (#1518) --- src/Client.js | 7 ++++- tests/client.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/Client.js b/src/Client.js index 75cc723..74499da 100644 --- a/src/Client.js +++ b/src/Client.js @@ -92,7 +92,12 @@ class Client extends EventEmitter { browser = await puppeteer.connect(puppeteerOpts); page = await browser.newPage(); } else { - browser = await puppeteer.launch(puppeteerOpts); + const browserArgs = [...(puppeteerOpts.args || [])]; + if(!browserArgs.find(arg => arg.includes('--user-agent'))) { + browserArgs.push(`--user-agent=${this.options.userAgent}`); + } + + browser = await puppeteer.launch({...puppeteerOpts, args: browserArgs}); page = (await browser.pages())[0]; } diff --git a/tests/client.js b/tests/client.js index 37b7805..7642d2f 100644 --- a/tests/client.js +++ b/tests/client.js @@ -9,7 +9,7 @@ const Message = require('../src/structures/Message'); const MessageMedia = require('../src/structures/MessageMedia'); const Location = require('../src/structures/Location'); const LegacySessionAuth = require('../src/authStrategies/LegacySessionAuth'); -const { MessageTypes, WAState } = require('../src/util/Constants'); +const { MessageTypes, WAState, DefaultOptions } = require('../src/util/Constants'); const expect = chai.expect; chai.use(chaiAsPromised); @@ -18,6 +18,74 @@ const remoteId = helper.remoteId; const isMD = helper.isMD(); describe('Client', function() { + describe('User Agent', function () { + it('should set user agent on browser', async function () { + this.timeout(25000); + + const client = helper.createClient(); + client.initialize(); + + await helper.sleep(20000); + + const browserUA = await client.pupBrowser.userAgent(); + expect(browserUA).to.equal(DefaultOptions.userAgent); + + const pageUA = await client.pupPage.evaluate(() => window.navigator.userAgent); + expect(pageUA).to.equal(DefaultOptions.userAgent); + + await client.destroy(); + }); + + it('should set custom user agent on browser', async function () { + this.timeout(25000); + const customUA = DefaultOptions.userAgent.replace(/Chrome\/.* /, 'Chrome/99.9.9999.999 '); + + const client = helper.createClient({ + options: { + userAgent: customUA + } + }); + + client.initialize(); + await helper.sleep(20000); + + const browserUA = await client.pupBrowser.userAgent(); + expect(browserUA).to.equal(customUA); + expect(browserUA.includes('Chrome/99.9.9999.999')).to.equal(true); + + const pageUA = await client.pupPage.evaluate(() => window.navigator.userAgent); + expect(pageUA).to.equal(customUA); + + await client.destroy(); + }); + + it('should respect an existing user agent arg', async function () { + this.timeout(25000); + + const customUA = DefaultOptions.userAgent.replace(/Chrome\/.* /, 'Chrome/99.9.9999.999 '); + + const client = helper.createClient({ + options: { + puppeteer: { + args: [`--user-agent=${customUA}`] + } + } + }); + + client.initialize(); + await helper.sleep(20000); + + const browserUA = await client.pupBrowser.userAgent(); + expect(browserUA).to.equal(customUA); + expect(browserUA.includes('Chrome/99.9.9999.999')).to.equal(true); + + const pageUA = await client.pupPage.evaluate(() => window.navigator.userAgent); + expect(pageUA).to.equal(DefaultOptions.userAgent); + + await client.destroy(); + }); + }); + describe('Authentication', function() { it('should emit QR code if not authenticated', async function() { this.timeout(25000);