Skip to content

Commit

Permalink
fix: corretion for version not multidevice
Browse files Browse the repository at this point in the history
  • Loading branch information
orkestral committed Nov 8, 2021
1 parent 96c3163 commit e4d9c37
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 74 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ Installing the current repository "you can download the beta version from the cu
const venom = require('venom-bot');

venom
.create()
.create({
session: 'session-name', //name of session
multidevice: false // for version not multidevice use false.(default: true)
})
.then((client) => start(client))
.catch((erro) => {
console.log(erro);
Expand Down Expand Up @@ -178,7 +181,7 @@ venom
},
// options
{
multidevice: true, // enabled multidevice.(default: true)
multidevice: false, // for version not multidevice use false.(default: true)
folderNameToken: 'tokens', //folder name when saving tokens
mkdirFolderToken: '', //folder directory tokens, just inside the venom folder, example: { mkdirFolderToken: '/node_modules', } //will save the tokens folder in the node_modules directory
headless: true, // Headless chrome
Expand Down Expand Up @@ -250,7 +253,9 @@ venom
//Create session wss return "serverClose" case server for close
console.log('Session name: ', session);
},
undefined
{
multidevice: false // for version not multidevice use false.(default: true)
}
)
.then((client) => {
start(client);
Expand Down
7 changes: 5 additions & 2 deletions src/api/layers/host.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export class HostLayer {
constructor(public page: Page, session?: string, options?: CreateConfig) {
this.session = session;
this.options = { ...defaultOptions, ...options };

// this.spin('Initializing...', 'spinning');
//this._initialize(this.page);
}

protected spin(text?: string, status?: Spinnies.SpinnerStatus) {
Expand Down Expand Up @@ -269,7 +272,7 @@ export class HostLayer {

this.spin('Checking QRCode status...');
// Wait for interface update
await sleep(3000);
await sleep(200);
authenticated = await isAuthenticated(this.page).catch(() => null);

if (authenticated === null || JSON.stringify(authenticated) === '{}') {
Expand All @@ -295,7 +298,7 @@ export class HostLayer {
this.cancelAutoClose();
this.startAutoClose();
// Wait for interface update
await sleep(3000);
await sleep(200);
this.spin('Checking phone is connected...');
const inChat = await this.waitForInChat();

Expand Down
2 changes: 1 addition & 1 deletion src/config/WAuserAgente.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/

const useragentOverride =
'WhatsApp/2.2043.8 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36';
'WhatsApp/2.2043.8 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36';
export { useragentOverride };
4 changes: 2 additions & 2 deletions src/config/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ import { puppeteerConfig } from './puppeteer.config';

// Server config
export interface CreateConfig {
/**
* multidevice option
/** enabled multidevice
* @default true
*/
multidevice?: boolean;
/** folder name when saving tokens
Expand Down
6 changes: 3 additions & 3 deletions src/config/puppeteer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,13 @@ const puppeteerConfig = {
// `--app=${WAUrl}`,
'--log-level=3', // fatal only
//'--start-maximized',
'-wait-for-browser',
'--no-default-browser-check',
'--disable-site-isolation-trials',
'--no-experiments',
'--ignore-gpu-blacklist',
'--ignore-certificate-errors',
'--ignore-certificate-errors-spki-list',
'--disable-gpu',
'--disable-web-security',
'--disable-extensions',
'--disable-default-apps',
'--enable-features=NetworkService',
Expand All @@ -92,7 +90,9 @@ const puppeteerConfig = {
'--disable-app-list-dismiss-on-blur',
'--disable-accelerated-video-decode',
'--disable-dev-shm-usage',
'--user-agent=WhatsApp/2.2043.8 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
'--disable-gl-drawing-for-tests',
'--incognito',
'--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
]
};

Expand Down
13 changes: 4 additions & 9 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import * as path from 'path';
import * as puppeteer from 'puppeteer';
import * as qrcode from 'qrcode-terminal';
import { existsSync, readFileSync } from 'fs';
import { CreateConfig, defaultOptions } from '../config/create-config';
import { CreateConfig } from '../config/create-config';
import { ScrapQrcode } from '../api/model/qrcode';
import { tokenSession } from '../config/tokenSession.config';

Expand Down Expand Up @@ -211,8 +211,7 @@ export async function auth_InjectToken(
page: puppeteer.Page,
session: string,
options: CreateConfig,
token?: tokenSession,
clear = options.multidevice ? true : false
token?: tokenSession
) {
if (!token) {
const pathToken: string = path.join(
Expand All @@ -232,12 +231,8 @@ export async function auth_InjectToken(
}

//Auth with token ->start<-
await page.evaluate(async (session) => {
if (clear) {
await page.evaluate((session) => {
localStorage.clear();
});
}
await page.evaluate((session) => {
localStorage.clear();
Object.keys(session).forEach((key) => {
localStorage.setItem(key, session[key]);
});
Expand Down
56 changes: 4 additions & 52 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,54 +56,28 @@ import * as ChromeLauncher from 'chrome-launcher';
import * as path from 'path';
import { Browser, BrowserContext, Page } from 'puppeteer';
import puppeteer from 'puppeteer-extra';
import { CreateConfig, defaultOptions } from '../config/create-config';
import { CreateConfig } from '../config/create-config';
import { puppeteerConfig } from '../config/puppeteer.config';
import StealthPlugin = require('puppeteer-extra-plugin-stealth');
import { auth_InjectToken } from './auth';
import { useragentOverride } from '../config/WAuserAgente';
import { tokenSession } from '../config/tokenSession.config';
import * as os from 'os';
import * as rimraf from 'rimraf';
import { addExitCallback } from 'catch-exit';

export async function unregisterServiceWorker(page: Page) {
await page.evaluateOnNewDocument(() => {
// Remove existent service worker
navigator.serviceWorker
.getRegistrations()
.then((registrations) => {
for (let registration of registrations) {
registration.unregister();
}
})
.catch((err) => null);

// Disable service worker registration
// @ts-ignore
navigator.serviceWorker.register = undefined;
// Disable service worker detection
// @ts-ignore
delete navigator.serviceWorker;
});
}

export async function initWhatsapp(
session: string,
options: CreateConfig,
browser: Browser,
token?: tokenSession,
clear = options.multidevice ? true : false
token?: tokenSession
): Promise<false | Page> {
const waPage: Page = await getWhatsappPage(browser);
await unregisterServiceWorker(waPage);
if (waPage != null) {
try {
await waPage.setUserAgent(useragentOverride);
await waPage.goto(puppeteerConfig.whatsappUrl, {
waitUntil: 'domcontentloaded'
});
// Auth with token
await auth_InjectToken(waPage, session, options, token, clear);
await auth_InjectToken(waPage, session, options, token);
await waPage.evaluate(() => {
window.location.reload();
});
Expand All @@ -118,7 +92,7 @@ export async function initWhatsapp(
export async function injectApi(page: Page) {
const injected = await page
.evaluate(() => {
localStorage.setItem('md-opted-in', 'false');
// @ts-ignore
return (
typeof window.WAPI !== 'undefined' &&
typeof window.Store !== 'undefined'
Expand Down Expand Up @@ -203,28 +177,6 @@ export async function initBrowser(
browser = 'launch';
});
}
// Register an exit callback to remove user-data-dir
try {
const arg = browser
.process()
.spawnargs.find((s: string) => s.startsWith('--user-data-dir='));

if (arg) {
const tmpUserDataDir = arg.split('=')[1];

// Only if path is in TMP directory
if (path.relative(os.tmpdir(), tmpUserDataDir).startsWith('puppeteer')) {
addExitCallback((signal) => {
// Remove only on exit signal
if (signal === 'exit') {
try {
rimraf.sync(tmpUserDataDir);
} catch (error) {}
}
});
}
}
} catch (error) {}
return browser;
}

Expand Down
10 changes: 9 additions & 1 deletion src/controllers/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

import * as chalk from 'chalk';

import { readFileSync } from 'fs';
import * as fs from 'fs';

import { fstat, readFileSync } from 'fs';

import { Browser, Page } from 'puppeteer';

Expand Down Expand Up @@ -163,6 +165,12 @@ export async function create(
if (options?.multidevice != false) {
const dirPath = `./${defaultOptions.folderNameToken}/${session}`;

let existFile = fs.existsSync(dirPath + '.data.json');

if (existFile) {
fs.unlinkSync(dirPath + '.data.json');
}

defaultOptions.puppeteerOptions = {
userDataDir: dirPath
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/check-send-exist.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function sendCheckType(chatId = undefined) {
chatId,
true,
404,
'incorrect parameters! Use as an example: [email protected]'
'incorrect parameters! Use as an example: [email protected] or [email protected]'
);
}
}
Expand Down

0 comments on commit e4d9c37

Please sign in to comment.