Add eslint config and fix linting issues

This commit is contained in:
Pedro Lopez
2020-02-02 14:50:04 -04:00
parent 8eb461ed32
commit cc477f9545
11 changed files with 1047 additions and 124 deletions

34
.eslintrc.json Normal file
View File

@@ -0,0 +1,34 @@
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}

View File

@@ -1,4 +1,4 @@
const { Client } = require('./index') const { Client } = require('./index');
const client = new Client({puppeteer: {headless: false}}); const client = new Client({puppeteer: {headless: false}});
// You can use an existing session and avoid scanning a QR code by adding a "session" object to the client options. // You can use an existing session and avoid scanning a QR code by adding a "session" object to the client options.
@@ -18,7 +18,7 @@ client.on('authenticated', (session) => {
client.on('auth_failure', msg => { client.on('auth_failure', msg => {
// Fired if session restore was unsuccessfull // Fired if session restore was unsuccessfull
console.error('AUTHENTICATION FAILURE', msg); console.error('AUTHENTICATION FAILURE', msg);
}) });
client.on('ready', () => { client.on('ready', () => {
console.log('READY'); console.log('READY');
@@ -125,9 +125,9 @@ client.on('message_create', (msg) => {
if(msg.fromMe) { if(msg.fromMe) {
// do stuff here // do stuff here
} }
}) });
client.on('disconnected', () => { client.on('disconnected', () => {
console.log('Client was logged out'); console.log('Client was logged out');
}) });

885
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -28,5 +28,8 @@
"jsqr": "^1.2.0", "jsqr": "^1.2.0",
"moduleraid": "git+https://github.com/pixeldesu/moduleRaid.git", "moduleraid": "git+https://github.com/pixeldesu/moduleRaid.git",
"puppeteer": "^2.1.0" "puppeteer": "^2.1.0"
},
"devDependencies": {
"eslint": "^6.8.0"
} }
} }

View File

@@ -38,10 +38,10 @@ class Client extends EventEmitter {
await page.evaluateOnNewDocument( await page.evaluateOnNewDocument(
session => { session => {
localStorage.clear(); localStorage.clear();
localStorage.setItem("WABrowserId", session.WABrowserId); localStorage.setItem('WABrowserId', session.WABrowserId);
localStorage.setItem("WASecretBundle", session.WASecretBundle); localStorage.setItem('WASecretBundle', session.WASecretBundle);
localStorage.setItem("WAToken1", session.WAToken1); localStorage.setItem('WAToken1', session.WAToken1);
localStorage.setItem("WAToken2", session.WAToken2); localStorage.setItem('WAToken2', session.WAToken2);
}, this.options.session); }, this.options.session);
} }
@@ -89,11 +89,11 @@ class Client extends EventEmitter {
WASecretBundle: localStorage.WASecretBundle, WASecretBundle: localStorage.WASecretBundle,
WAToken1: localStorage.WAToken1, WAToken1: localStorage.WAToken1,
WAToken2: localStorage.WAToken2 WAToken2: localStorage.WAToken2
} };
this.emit(Events.AUTHENTICATED, session); this.emit(Events.AUTHENTICATED, session);
// Check Store Injection // Check window.Store Injection
await page.waitForFunction('window.Store != undefined'); await page.waitForFunction('window.Store != undefined');
//Load util functions (serializers, helper functions) //Load util functions (serializers, helper functions)
@@ -101,7 +101,7 @@ class Client extends EventEmitter {
// Expose client info // Expose client info
this.info = new ClientInfo(this, await page.evaluate(() => { this.info = new ClientInfo(this, await page.evaluate(() => {
return Store.Conn.serialize(); return window.Store.Conn.serialize();
})); }));
// Register events // Register events
@@ -124,8 +124,8 @@ class Client extends EventEmitter {
}); });
await page.evaluate(() => { await page.evaluate(() => {
Store.Msg.on('add', onAddMessageEvent); window.Store.Msg.on('add', window.onAddMessageEvent);
Store.AppState.on('change:state', onAppStateChangedEvent); window.Store.AppState.on('change:state', window.onAppStateChangedEvent);
}); });
this.pupBrowser = browser; this.pupBrowser = browser;
@@ -145,7 +145,7 @@ class Client extends EventEmitter {
*/ */
async sendMessage(chatId, message) { async sendMessage(chatId, message) {
const newMessage = await this.pupPage.evaluate(async (chatId, message) => { const newMessage = await this.pupPage.evaluate(async (chatId, message) => {
const msg = await WWebJS.sendMessage(Store.Chat.get(chatId), message); const msg = await window.WWebJS.sendMessage(window.Store.Chat.get(chatId), message);
return msg.serialize(); return msg.serialize();
}, chatId, message); }, chatId, message);
@@ -157,7 +157,7 @@ class Client extends EventEmitter {
*/ */
async getChats() { async getChats() {
let chats = await this.pupPage.evaluate(() => { let chats = await this.pupPage.evaluate(() => {
return WWebJS.getChats(); return window.WWebJS.getChats();
}); });
return chats.map(chat => ChatFactory.create(this, chat)); return chats.map(chat => ChatFactory.create(this, chat));
@@ -169,7 +169,7 @@ class Client extends EventEmitter {
*/ */
async getChatById(chatId) { async getChatById(chatId) {
let chat = await this.pupPage.evaluate(chatId => { let chat = await this.pupPage.evaluate(chatId => {
return WWebJS.getChat(chatId); return window.WWebJS.getChat(chatId);
}, chatId); }, chatId);
return ChatFactory.create(this, chat); return ChatFactory.create(this, chat);
@@ -181,8 +181,8 @@ class Client extends EventEmitter {
*/ */
async acceptInvite(inviteCode) { async acceptInvite(inviteCode) {
const chat = await this.pupPage.evaluate(async inviteCode => { const chat = await this.pupPage.evaluate(async inviteCode => {
const chatId = await Store.Invite.sendJoinGroupViaInvite(inviteCode); const chatId = await window.Store.Invite.sendJoinGroupViaInvite(inviteCode);
return WWebJS.getChat(chatId._serialized); return window.WWebJS.getChat(chatId._serialized);
}, inviteCode); }, inviteCode);
return ChatFactory.create(this.client, chat); return ChatFactory.create(this.client, chat);

View File

@@ -46,7 +46,7 @@ class GroupChat extends Chat {
*/ */
async addParticipants(participantIds) { async addParticipants(participantIds) {
return await this.client.pupPage.evaluate((chatId, participantIds) => { return await this.client.pupPage.evaluate((chatId, participantIds) => {
return Store.Wap.addParticipants(chatId, participantIds); return window.Store.Wap.addParticipants(chatId, participantIds);
}, this.id._serialized, participantIds); }, this.id._serialized, participantIds);
} }
@@ -56,7 +56,7 @@ class GroupChat extends Chat {
*/ */
async removeParticipants(participantIds) { async removeParticipants(participantIds) {
return await this.client.pupPage.evaluate((chatId, participantIds) => { return await this.client.pupPage.evaluate((chatId, participantIds) => {
return Store.Wap.removeParticipants(chatId, participantIds); return window.Store.Wap.removeParticipants(chatId, participantIds);
}, this.id._serialized, participantIds); }, this.id._serialized, participantIds);
} }
@@ -66,7 +66,7 @@ class GroupChat extends Chat {
*/ */
async promoteParticipants(participantIds) { async promoteParticipants(participantIds) {
return await this.client.pupPage.evaluate((chatId, participantIds) => { return await this.client.pupPage.evaluate((chatId, participantIds) => {
return Store.Wap.promoteParticipants(chatId, participantIds); return window.Store.Wap.promoteParticipants(chatId, participantIds);
}, this.id._serialized, participantIds); }, this.id._serialized, participantIds);
} }
@@ -76,7 +76,7 @@ class GroupChat extends Chat {
*/ */
async demoteParticipants(participantIds) { async demoteParticipants(participantIds) {
return await this.client.pupPage.evaluate((chatId, participantIds) => { return await this.client.pupPage.evaluate((chatId, participantIds) => {
return Store.Wap.demoteParticipants(chatId, participantIds); return window.Store.Wap.demoteParticipants(chatId, participantIds);
}, this.id._serialized, participantIds); }, this.id._serialized, participantIds);
} }
@@ -86,7 +86,7 @@ class GroupChat extends Chat {
*/ */
async setSubject(subject) { async setSubject(subject) {
let res = await this.client.pupPage.evaluate((chatId, subject) => { let res = await this.client.pupPage.evaluate((chatId, subject) => {
return Store.Wap.changeSubject(chatId, subject); return window.Store.Wap.changeSubject(chatId, subject);
}, this.id._serialized, subject); }, this.id._serialized, subject);
if(res.status == 200) { if(res.status == 200) {
@@ -100,8 +100,8 @@ class GroupChat extends Chat {
*/ */
async setDescription(description) { async setDescription(description) {
let res = await this.client.pupPage.evaluate((chatId, description) => { let res = await this.client.pupPage.evaluate((chatId, description) => {
let descId = Store.GroupMetadata.get(chatId).descId; let descId = window.Store.GroupMetadata.get(chatId).descId;
return Store.Wap.setGroupDescription(chatId, description, Store.genId(), descId); return window.Store.Wap.setGroupDescription(chatId, description, window.Store.genId(), descId);
}, this.id._serialized, description); }, this.id._serialized, description);
if (res.status == 200) { if (res.status == 200) {
@@ -114,14 +114,14 @@ class GroupChat extends Chat {
*/ */
async getInviteCode() { async getInviteCode() {
let res = await this.client.pupPage.evaluate(chatId => { let res = await this.client.pupPage.evaluate(chatId => {
return Store.Wap.groupInviteCode(chatId); return window.Store.Wap.groupInviteCode(chatId);
}, this.id._serialized); }, this.id._serialized);
if (res.status == 200) { if (res.status == 200) {
return res.code; return res.code;
} }
throw new Error('Not authorized') throw new Error('Not authorized');
} }
/** /**
@@ -129,8 +129,8 @@ class GroupChat extends Chat {
*/ */
async revokeInvite() { async revokeInvite() {
return await this.client.pupPage.evaluate(chatId => { return await this.client.pupPage.evaluate(chatId => {
return Store.Wap.revokeGroupInvite(chatId); return window.Store.Wap.revokeGroupInvite(chatId);
}, chatId); }, this.id._serialized);
} }
/** /**
@@ -139,7 +139,7 @@ class GroupChat extends Chat {
*/ */
static async getInviteInfo(inviteCode) { static async getInviteInfo(inviteCode) {
return await this.client.pupPage.evaluate(inviteCode => { return await this.client.pupPage.evaluate(inviteCode => {
return Store.Wap.groupInviteInfo(inviteCode); return window.Store.Wap.groupInviteInfo(inviteCode);
}, inviteCode); }, inviteCode);
} }
@@ -149,7 +149,7 @@ class GroupChat extends Chat {
*/ */
static async join(inviteCode) { static async join(inviteCode) {
return await this.client.pupPage.evaluate(inviteCode => { return await this.client.pupPage.evaluate(inviteCode => {
return Store.Wap.acceptGroupInvite(inviteCode); return window.Store.Wap.acceptGroupInvite(inviteCode);
}, inviteCode); }, inviteCode);
} }
@@ -158,7 +158,7 @@ class GroupChat extends Chat {
*/ */
async leave() { async leave() {
return await this.client.pupPage.evaluate(chatId => { return await this.client.pupPage.evaluate(chatId => {
return Store.Wap.leaveGroup(chatId); return window.Store.Wap.leaveGroup(chatId);
}, this.id._serialized); }, this.id._serialized);
} }

View File

@@ -19,9 +19,9 @@ class Message extends Base {
this.body = this.hasMedia ? data.caption || '' : data.body || ''; this.body = this.hasMedia ? data.caption || '' : data.body || '';
this.type = data.type; this.type = data.type;
this.timestamp = data.t; this.timestamp = data.t;
this.from = typeof (data.from) === "object" ? data.from._serialized : data.from; this.from = typeof (data.from) === 'object' ? data.from._serialized : data.from;
this.to = typeof (data.to) === "object" ? data.to._serialized : data.to; this.to = typeof (data.to) === 'object' ? data.to._serialized : data.to;
this.author = typeof (data.author) === "object" ? data.author._serialized : data.author; this.author = typeof (data.author) === 'object' ? data.author._serialized : data.author;
this.isForwarded = data.isForwarded; this.isForwarded = data.isForwarded;
this.broadcast = data.broadcast; this.broadcast = data.broadcast;
this.fromMe = data.id.fromMe; this.fromMe = data.id.fromMe;
@@ -48,7 +48,7 @@ class Message extends Base {
if (!this.hasQuotedMsg) return undefined; if (!this.hasQuotedMsg) return undefined;
const quotedMsg = await this.client.pupPage.evaluate((msgId) => { const quotedMsg = await this.client.pupPage.evaluate((msgId) => {
let msg = Store.Msg.get(msgId); let msg = window.Store.Msg.get(msgId);
return msg.quotedMsgObj().serialize(); return msg.quotedMsgObj().serialize();
}, this.id._serialized); }, this.id._serialized);
@@ -68,10 +68,10 @@ class Message extends Base {
} }
const newMessage = await this.client.pupPage.evaluate(async (chatId, quotedMessageId, message) => { const newMessage = await this.client.pupPage.evaluate(async (chatId, quotedMessageId, message) => {
let quotedMessage = Store.Msg.get(quotedMessageId); let quotedMessage = window.Store.Msg.get(quotedMessageId);
if(quotedMessage.canReply()) { if(quotedMessage.canReply()) {
const chat = Store.Chat.get(chatId); const chat = window.Store.Chat.get(chatId);
const newMessage = await WWebJS.sendMessage(chat, message, quotedMessage.msgContextInfo(chat)); const newMessage = await window.WWebJS.sendMessage(chat, message, quotedMessage.msgContextInfo(chat));
return newMessage.serialize(); return newMessage.serialize();
} else { } else {
throw new Error('This message cannot be replied to.'); throw new Error('This message cannot be replied to.');
@@ -87,16 +87,16 @@ class Message extends Base {
} }
return await this.client.pupPage.evaluate(async (msgId) => { return await this.client.pupPage.evaluate(async (msgId) => {
const msg = Store.Msg.get(msgId); const msg = window.Store.Msg.get(msgId);
const buffer = await WWebJS.downloadBuffer(msg.clientUrl); const buffer = await window.WWebJS.downloadBuffer(msg.clientUrl);
const decrypted = await Store.CryptoLib.decryptE2EMedia(msg.type, buffer, msg.mediaKey, msg.mimetype); const decrypted = await window.Store.CryptoLib.decryptE2EMedia(msg.type, buffer, msg.mediaKey, msg.mimetype);
const data = await WWebJS.readBlobAsync(decrypted._blob); const data = await window.WWebJS.readBlobAsync(decrypted._blob);
return { return {
data, data,
mimetype: msg.mimetype, mimetype: msg.mimetype,
filename: msg.filename filename: msg.filename
} };
}, this.id._serialized); }, this.id._serialized);
} }

View File

@@ -1,6 +1,6 @@
'use strict'; 'use strict';
exports.WhatsWebURL = 'https://web.whatsapp.com/' exports.WhatsWebURL = 'https://web.whatsapp.com/';
exports.UserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'; exports.UserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36';
@@ -9,13 +9,13 @@ exports.DefaultOptions = {
headless: true headless: true
}, },
session: false session: false
} };
exports.Status = { exports.Status = {
INITIALIZING: 0, INITIALIZING: 0,
AUTHENTICATING: 1, AUTHENTICATING: 1,
READY: 3 READY: 3
} };
exports.Events = { exports.Events = {
AUTHENTICATED: 'authenticated', AUTHENTICATED: 'authenticated',
@@ -25,7 +25,7 @@ exports.Events = {
MESSAGE_CREATE: 'message_create', MESSAGE_CREATE: 'message_create',
QR_RECEIVED: 'qr', QR_RECEIVED: 'qr',
DISCONNECTED: 'disconnected' DISCONNECTED: 'disconnected'
} };
exports.MessageTypes = { exports.MessageTypes = {
TEXT: 'chat', TEXT: 'chat',
@@ -35,25 +35,25 @@ exports.MessageTypes = {
VIDEO: 'video', VIDEO: 'video',
DOCUMENT: 'document', DOCUMENT: 'document',
STICKER: 'sticker' STICKER: 'sticker'
} };
exports.ChatTypes = { exports.ChatTypes = {
SOLO: 'solo', SOLO: 'solo',
GROUP: 'group', GROUP: 'group',
UNKNOWN: 'unknown' UNKNOWN: 'unknown'
} };
exports.WAState = { exports.WAState = {
CONFLICT: "CONFLICT", CONFLICT: 'CONFLICT',
CONNECTED: "CONNECTED", CONNECTED: 'CONNECTED',
DEPRECATED_VERSION: "DEPRECATED_VERSION", DEPRECATED_VERSION: 'DEPRECATED_VERSION',
OPENING: "OPENING", OPENING: 'OPENING',
PAIRING: "PAIRING", PAIRING: 'PAIRING',
PROXYBLOCK: "PROXYBLOCK", PROXYBLOCK: 'PROXYBLOCK',
SMB_TOS_BLOCK: "SMB_TOS_BLOCK", SMB_TOS_BLOCK: 'SMB_TOS_BLOCK',
TIMEOUT: "TIMEOUT", TIMEOUT: 'TIMEOUT',
TOS_BLOCK: "TOS_BLOCK", TOS_BLOCK: 'TOS_BLOCK',
UNLAUNCHED: "UNLAUNCHED", UNLAUNCHED: 'UNLAUNCHED',
UNPAIRED: "UNPAIRED", UNPAIRED: 'UNPAIRED',
UNPAIRED_IDLE: "UNPAIRED_IDLE" UNPAIRED_IDLE: 'UNPAIRED_IDLE'
} };

View File

@@ -4,34 +4,35 @@
* Exposes the internal Store to the WhatsApp Web client * Exposes the internal Store to the WhatsApp Web client
*/ */
exports.ExposeStore = (moduleRaidStr) => { exports.ExposeStore = (moduleRaidStr) => {
eval("var moduleRaid = " + moduleRaidStr); eval('var moduleRaid = ' + moduleRaidStr);
// eslint-disable-next-line no-undef
window.mR = moduleRaid(); window.mR = moduleRaid();
window.Store = window.mR.findModule("Chat")[1].default; window.Store = window.mR.findModule('Chat')[1].default;
window.Store.AppState = window.mR.findModule("STREAM")[0].default; window.Store.AppState = window.mR.findModule('STREAM')[0].default;
window.Store.Conn = window.mR.findModule("Conn")[0].default; window.Store.Conn = window.mR.findModule('Conn')[0].default;
window.Store.CryptoLib = window.mR.findModule("decryptE2EMedia")[0]; window.Store.CryptoLib = window.mR.findModule('decryptE2EMedia')[0];
window.Store.Wap = window.mR.findModule("Wap")[0].default; window.Store.Wap = window.mR.findModule('Wap')[0].default;
window.Store.genId = window.mR.findModule((module) => module.default && typeof module.default === 'function' && module.default.toString().match(/crypto/))[0].default; window.Store.genId = window.mR.findModule((module) => module.default && typeof module.default === 'function' && module.default.toString().match(/crypto/))[0].default;
window.Store.SendMessage = window.mR.findModule("addAndSendMsgToChat")[0]; window.Store.SendMessage = window.mR.findModule('addAndSendMsgToChat')[0];
window.Store.MsgKey = window.mR.findModule((module) => module.default && module.default.fromString)[0].default; window.Store.MsgKey = window.mR.findModule((module) => module.default && module.default.fromString)[0].default;
window.Store.Invite = window.mR.findModule("sendJoinGroupViaInvite")[0]; window.Store.Invite = window.mR.findModule('sendJoinGroupViaInvite')[0];
} };
exports.LoadUtils = () => { exports.LoadUtils = () => {
window.WWebJS = {}; window.WWebJS = {};
window.WWebJS.sendMessage = async (chat, content, options) => { window.WWebJS.sendMessage = async (chat, content, options) => {
const newMsgId = new Store.MsgKey({ const newMsgId = new window.Store.MsgKey({
from: Store.Conn.me, from: window.Store.Conn.me,
to: chat.id, to: chat.id,
id: Store.genId(), id: window.Store.genId(),
}); });
const message = { const message = {
id: newMsgId, id: newMsgId,
ack: 0, ack: 0,
body: content, body: content,
from: Store.Conn.me, from: window.Store.Conn.me,
to: chat.id, to: chat.id,
local: true, local: true,
self: 'out', self: 'out',
@@ -39,11 +40,11 @@ exports.LoadUtils = () => {
isNewMsg: true, isNewMsg: true,
type: 'chat', type: 'chat',
...options ...options
} };
await Store.SendMessage.addAndSendMsgToChat(chat, message); await window.Store.SendMessage.addAndSendMsgToChat(chat, message);
return Store.Msg.get(newMsgId._serialized); return window.Store.Msg.get(newMsgId._serialized);
} };
window.WWebJS.getChatModel = chat => { window.WWebJS.getChatModel = chat => {
let res = chat.serialize(); let res = chat.serialize();
@@ -55,22 +56,22 @@ exports.LoadUtils = () => {
} }
return res; return res;
} };
window.WWebJS.getChat = chatId => { window.WWebJS.getChat = chatId => {
const chat = Store.Chat.get(chatId); const chat = window.Store.Chat.get(chatId);
return WWebJS.getChatModel(chat); return window.WWebJS.getChatModel(chat);
} };
window.WWebJS.getChats = () => { window.WWebJS.getChats = () => {
const chats = Store.Chat.models; const chats = window.Store.Chat.models;
return chats.map(chat => WWebJS.getChatModel(chat)); return chats.map(chat => window.WWebJS.getChatModel(chat));
} };
window.WWebJS.downloadBuffer = (url) => { window.WWebJS.downloadBuffer = (url) => {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.open("GET", url); xhr.open('GET', url);
xhr.responseType = 'arraybuffer'; xhr.responseType = 'arraybuffer';
xhr.onload = function () { xhr.onload = function () {
if (xhr.status == 200) { if (xhr.status == 200) {
@@ -90,7 +91,7 @@ exports.LoadUtils = () => {
}; };
xhr.send(null); xhr.send(null);
}); });
} };
window.WWebJS.readBlobAsync = (blob) => { window.WWebJS.readBlobAsync = (blob) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -103,14 +104,14 @@ exports.LoadUtils = () => {
reader.onerror = reject; reader.onerror = reject;
reader.readAsDataURL(blob); reader.readAsDataURL(blob);
}) });
} };
} };
exports.MarkAllRead = () => { exports.MarkAllRead = () => {
let Chats = Store.Chat.models; let Chats = window.Store.Chat.models;
for (chatIndex in Chats) { for (let chatIndex in Chats) {
if (isNaN(chatIndex)) { if (isNaN(chatIndex)) {
continue; continue;
} }
@@ -119,7 +120,7 @@ exports.MarkAllRead = () => {
if (chat.unreadCount > 0) { if (chat.unreadCount > 0) {
chat.markSeen(); chat.markSeen();
Store.Wap.sendConversationSeen(chat.id, chat.getLastMsgKeyForAction(), chat.unreadCount - chat.pendingSeenCount); window.Store.Wap.sendConversationSeen(chat.id, chat.getLastMsgKeyForAction(), chat.unreadCount - chat.pendingSeenCount);
} }
} }
} };