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}});
// 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 => {
// Fired if session restore was unsuccessfull
console.error('AUTHENTICATION FAILURE', msg);
})
});
client.on('ready', () => {
console.log('READY');
@@ -125,9 +125,9 @@ client.on('message_create', (msg) => {
if(msg.fromMe) {
// do stuff here
}
})
});
client.on('disconnected', () => {
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",
"moduleraid": "git+https://github.com/pixeldesu/moduleRaid.git",
"puppeteer": "^2.1.0"
},
"devDependencies": {
"eslint": "^6.8.0"
}
}

View File

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

View File

@@ -14,7 +14,7 @@ class Base {
_clone() {
return Object.assign(Object.create(this), this);
}
}
_patch(data) { return data; }
}

View File

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

View File

@@ -19,9 +19,9 @@ class Message extends Base {
this.body = this.hasMedia ? data.caption || '' : data.body || '';
this.type = data.type;
this.timestamp = data.t;
this.from = typeof (data.from) === "object" ? data.from._serialized : data.from;
this.to = typeof (data.to) === "object" ? data.to._serialized : data.to;
this.author = typeof (data.author) === "object" ? data.author._serialized : data.author;
this.from = typeof (data.from) === 'object' ? data.from._serialized : data.from;
this.to = typeof (data.to) === 'object' ? data.to._serialized : data.to;
this.author = typeof (data.author) === 'object' ? data.author._serialized : data.author;
this.isForwarded = data.isForwarded;
this.broadcast = data.broadcast;
this.fromMe = data.id.fromMe;
@@ -48,7 +48,7 @@ class Message extends Base {
if (!this.hasQuotedMsg) return undefined;
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();
}, this.id._serialized);
@@ -68,10 +68,10 @@ class Message extends Base {
}
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()) {
const chat = Store.Chat.get(chatId);
const newMessage = await WWebJS.sendMessage(chat, message, quotedMessage.msgContextInfo(chat));
const chat = window.Store.Chat.get(chatId);
const newMessage = await window.WWebJS.sendMessage(chat, message, quotedMessage.msgContextInfo(chat));
return newMessage.serialize();
} else {
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) => {
const msg = Store.Msg.get(msgId);
const buffer = await WWebJS.downloadBuffer(msg.clientUrl);
const decrypted = await Store.CryptoLib.decryptE2EMedia(msg.type, buffer, msg.mediaKey, msg.mimetype);
const data = await WWebJS.readBlobAsync(decrypted._blob);
const msg = window.Store.Msg.get(msgId);
const buffer = await window.WWebJS.downloadBuffer(msg.clientUrl);
const decrypted = await window.Store.CryptoLib.decryptE2EMedia(msg.type, buffer, msg.mediaKey, msg.mimetype);
const data = await window.WWebJS.readBlobAsync(decrypted._blob);
return {
data,
mimetype: msg.mimetype,
filename: msg.filename
}
};
}, this.id._serialized);
}

View File

@@ -1,6 +1,6 @@
'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';
@@ -9,13 +9,13 @@ exports.DefaultOptions = {
headless: true
},
session: false
}
};
exports.Status = {
INITIALIZING: 0,
AUTHENTICATING: 1,
READY: 3
}
};
exports.Events = {
AUTHENTICATED: 'authenticated',
@@ -25,7 +25,7 @@ exports.Events = {
MESSAGE_CREATE: 'message_create',
QR_RECEIVED: 'qr',
DISCONNECTED: 'disconnected'
}
};
exports.MessageTypes = {
TEXT: 'chat',
@@ -35,25 +35,25 @@ exports.MessageTypes = {
VIDEO: 'video',
DOCUMENT: 'document',
STICKER: 'sticker'
}
};
exports.ChatTypes = {
SOLO: 'solo',
GROUP: 'group',
UNKNOWN: 'unknown'
}
};
exports.WAState = {
CONFLICT: "CONFLICT",
CONNECTED: "CONNECTED",
DEPRECATED_VERSION: "DEPRECATED_VERSION",
OPENING: "OPENING",
PAIRING: "PAIRING",
PROXYBLOCK: "PROXYBLOCK",
SMB_TOS_BLOCK: "SMB_TOS_BLOCK",
TIMEOUT: "TIMEOUT",
TOS_BLOCK: "TOS_BLOCK",
UNLAUNCHED: "UNLAUNCHED",
UNPAIRED: "UNPAIRED",
UNPAIRED_IDLE: "UNPAIRED_IDLE"
}
CONFLICT: 'CONFLICT',
CONNECTED: 'CONNECTED',
DEPRECATED_VERSION: 'DEPRECATED_VERSION',
OPENING: 'OPENING',
PAIRING: 'PAIRING',
PROXYBLOCK: 'PROXYBLOCK',
SMB_TOS_BLOCK: 'SMB_TOS_BLOCK',
TIMEOUT: 'TIMEOUT',
TOS_BLOCK: 'TOS_BLOCK',
UNLAUNCHED: 'UNLAUNCHED',
UNPAIRED: 'UNPAIRED',
UNPAIRED_IDLE: 'UNPAIRED_IDLE'
};

View File

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

View File

@@ -7,29 +7,29 @@ const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
*/
class Util {
constructor() {
throw new Error(`The ${this.constructor.name} class may not be instantiated.`);
}
/**
* Sets default properties on an object that aren't already specified.
* @param {Object} def Default properties
* @param {Object} given Object to assign defaults to
* @returns {Object}
* @private
*/
static mergeDefault(def, given) {
if (!given) return def;
for (const key in def) {
if (!has(given, key) || given[key] === undefined) {
given[key] = def[key];
} else if (given[key] === Object(given[key])) {
given[key] = Util.mergeDefault(def[key], given[key]);
}
constructor() {
throw new Error(`The ${this.constructor.name} class may not be instantiated.`);
}
return given;
}
/**
* Sets default properties on an object that aren't already specified.
* @param {Object} def Default properties
* @param {Object} given Object to assign defaults to
* @returns {Object}
* @private
*/
static mergeDefault(def, given) {
if (!given) return def;
for (const key in def) {
if (!has(given, key) || given[key] === undefined) {
given[key] = def[key];
} else if (given[key] === Object(given[key])) {
given[key] = Util.mergeDefault(def[key], given[key]);
}
}
return given;
}
}
module.exports = Util;