Skip to content

Commit

Permalink
Merge pull request #232 from danielcardeenas/develop
Browse files Browse the repository at this point in the history
Adds useChrome parameter, adds debug url, updates UA
  • Loading branch information
danielcardeenas authored Apr 7, 2020
2 parents 14da2e4 + c3883e2 commit affd816
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "sulla",
"version": "2.3.2",
"version": "2.3.3",
"description": "Javascript whatsapp framework",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"start": "tsc && node dist/index.js",
"start": "tsc && node dist/app.js",
"build": "npm run build:wapi && npm run build:middleware && npm run build:jsQR && tsc",
"build:sulla": "tsc",
"build:wapi": "cd src/lib/wapi/ && webpack",
Expand Down
2 changes: 2 additions & 0 deletions src/config/create-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface CreateConfig {
headless?: boolean;
devtools?: boolean;
useChrome?: boolean;
debug?: boolean;
}
18 changes: 10 additions & 8 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function initWhatsapp(session: string, options: CreateConfig) {
const browser = await initBrowser(session, options);
const waPage = await getWhatsappPage(browser);
await waPage.setUserAgent(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
);

await waPage.goto(puppeteerConfig.whatsappUrl);
Expand Down Expand Up @@ -44,14 +44,16 @@ async function initBrowser(
options: CreateConfig,
extras = {}
) {
try {
const chromeInstalations = ChromeLauncher.Launcher.getInstallations();
if (chromeInstalations.length) {
extras = { ...extras, executablePath: chromeInstalations[0] };
if (options.useChrome) {
try {
const chromeInstalations = ChromeLauncher.Launcher.getInstallations();
if (chromeInstalations.length) {
extras = { ...extras, executablePath: chromeInstalations[0] };
}
} catch (error) {
console.log('Chrome not found, using chromium');
extras = {};
}
} catch (error) {
console.log('Chrome not found, using chromium');
extras = {};
}

// Use stealth plugin to avoid being detected as a bot
Expand Down
15 changes: 14 additions & 1 deletion src/controllers/initializer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ora from 'ora';
import { readFileSync } from 'fs';
import { Whatsapp } from '../api/whatsapp';
import { CreateConfig } from '../config/create-config';
import { isAuthenticated, isInsideChat, retrieveQR } from './auth';
Expand All @@ -11,7 +12,12 @@ const spinner = ora();
export async function create(
session = 'session',
catchQR?: (qrCode: string) => void,
options: CreateConfig = { headless: true, devtools: false }
options: CreateConfig = {
headless: true,
devtools: false,
useChrome: true,
debug: false,
}
) {
spinner.start('Initializing whatsapp');
let waPage = await initWhatsapp(session, options);
Expand Down Expand Up @@ -39,5 +45,12 @@ export async function create(
waPage = await injectApi(waPage);
spinner.succeed('Whatsapp is ready');

if (options.debug) {
const debugURL = `http://localhost:${readFileSync(
`./${session}/DevToolsActivePort`
).slice(0, -54)}`;
console.log(`\nDebug: \x1b[34m${debugURL}\x1b[0m`);
}

return new Whatsapp(waPage);
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"include": ["src/**/*"],
"exclude": ["src/middleware/middleware.ts"]
"exclude": ["src/middleware/middleware.ts", "src/api/app.ts"]
}

0 comments on commit affd816

Please sign in to comment.