Skip to content

Commit

Permalink
fix: Logged out correctly - logout()
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Feb 20, 2021
1 parent 50885b5 commit 64daabc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
11 changes: 9 additions & 2 deletions src/api/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

/**
Expand Down
47 changes: 26 additions & 21 deletions src/lib/wapi/functions/await-inside-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 10 additions & 4 deletions src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
4 changes: 2 additions & 2 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ interface WAPI {
includeNotifications: boolean
) => Message[];
loadEarlierMessages: (contactId: string) => Message[];
logout: () => Promise<boolean>;
logout: () => Promise<object>;
markUnseenMessage: (messageId: string) => boolean;
onAddedToGroup: (callback: Function) => any;
onIncomingCall: (callback: Function) => any;
Expand Down Expand Up @@ -258,7 +258,7 @@ interface WAPI {
sendSeen: (to: string) => void;
_profilePicfunc: (contactId: string) => Promise<object>;
sendStatusText: (text: string) => Promise<object>;
isInsideChat: () => boolean;
isInsideChat: (type: boolean) => boolean;
}

declare global {
Expand Down

1 comment on commit 64daabc

@jonalan7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.