diff --git a/src/api/whatsapp.ts b/src/api/whatsapp.ts index ff0b79cba..f5e43383c 100644 --- a/src/api/whatsapp.ts +++ b/src/api/whatsapp.ts @@ -119,10 +119,17 @@ export class Whatsapp extends ControlsLayer { /** * Logout whastapp - * @returns boolean + * @returns object */ public async logout() { - return await this.page.evaluate(() => WAPI.logout()); + return new Promise(async (resolve, reject) => { + const result = await this.page.evaluate(() => WAPI.logout()); + if (result['erro'] === true) { + reject(result); + } else { + resolve(result); + } + }); } /** diff --git a/src/lib/wapi/functions/await-inside-chat.js b/src/lib/wapi/functions/await-inside-chat.js index b7ee985a7..ca753e943 100644 --- a/src/lib/wapi/functions/await-inside-chat.js +++ b/src/lib/wapi/functions/await-inside-chat.js @@ -53,26 +53,31 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM */ -export async function isInsideChat() { - let err; - do { - try { - await new Promise((r) => setTimeout(r, 2000)); - if ( - (document.getElementsByClassName('app')[0] && - document.getElementsByClassName('app')[0].attributes && - !!document.getElementsByClassName('app')[0].attributes.tabindex) || - (document.getElementsByClassName('two')[0] && - document.getElementsByClassName('two')[0].attributes && - !!document.getElementsByClassName('two')[0].attributes.tabindex) - ) { - err = false; - return true; - } else { - throw 1; +export async function isInsideChat(type = true) { + const scope = + (document.getElementsByClassName('app')[0] && + document.getElementsByClassName('app')[0].attributes && + !!document.getElementsByClassName('app')[0].attributes.tabindex) || + (document.getElementsByClassName('two')[0] && + document.getElementsByClassName('two')[0].attributes && + !!document.getElementsByClassName('two')[0].attributes.tabindex); + + if (type) { + let err; + do { + try { + await new Promise((r) => setTimeout(r, 2000)); + if (scope) { + err = false; + return true; + } else { + throw 1; + } + } catch (e) { + err = true; } - } catch (e) { - err = true; - } - } while (err); + } while (err); + } else { + return scope ? true : false; + } } diff --git a/src/lib/wapi/wapi.js b/src/lib/wapi/wapi.js index cf7f51f95..a7228f581 100644 --- a/src/lib/wapi/wapi.js +++ b/src/lib/wapi/wapi.js @@ -553,10 +553,16 @@ if (typeof window.WAPI === 'undefined') { }; window.WAPI.logout = async function () { - return await window.WAPI.waitForStore(['ws2'], () => { - window.Store.ws2.logout(); - return true; - }); + let error = true, + chat = await WAPI.isInsideChat(false), + text = undefined; + if (Store.ws2.stream === 'CONNECTED' || chat) { + error = typeof Store.ws2.logout() === 'undefined' ? false : true; + } + if (error) { + text = 'The browser is closed or the client is not connected!'; + } + return WAPI.scope(undefined, error, undefined, text); }; window.WAPI.storePromises = {}; diff --git a/src/types/WAPI.d.ts b/src/types/WAPI.d.ts index bc4514052..e7bf18343 100644 --- a/src/types/WAPI.d.ts +++ b/src/types/WAPI.d.ts @@ -141,7 +141,7 @@ interface WAPI { includeNotifications: boolean ) => Message[]; loadEarlierMessages: (contactId: string) => Message[]; - logout: () => Promise; + logout: () => Promise; markUnseenMessage: (messageId: string) => boolean; onAddedToGroup: (callback: Function) => any; onIncomingCall: (callback: Function) => any; @@ -258,7 +258,7 @@ interface WAPI { sendSeen: (to: string) => void; _profilePicfunc: (contactId: string) => Promise; sendStatusText: (text: string) => Promise; - isInsideChat: () => boolean; + isInsideChat: (type: boolean) => boolean; } declare global {