From dbb752d08b44a7d58d5c4c823c91fc013004d069 Mon Sep 17 00:00:00 2001
From: Briuor
Date: Thu, 3 Jun 2021 14:31:34 -0300
Subject: [PATCH] fix wait for loading
---
README.md | 8 ++++----
test.js => example.js | 0
package-lock.json | 2 +-
package.json | 2 +-
src/api.js | 29 +++++++++++++++++++----------
5 files changed, 25 insertions(+), 16 deletions(-)
rename test.js => example.js (100%)
diff --git a/README.md b/README.md
index 91c3ef4..0d875b7 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,9 @@
+## Note
+**wbm** is an **unofficial** solution. It's not recommended using **wbm** in your company or for marketing purpose.
+
## Installation
```bash
> npm install wbm
@@ -87,6 +90,7 @@ wbm.start().then(async () => {
Type: `boolean`
* **session**
Keep user session, so the user must scan the QR Code once.
+ If you already is authenticated and **wbm** is asking for QR Code, please run using **session: false** once to reset your session. Then you use **session: true** again.
Default: `true`
Type: `boolean`
@@ -162,10 +166,6 @@ wbm.start().then(async () => {
### end()
This method must be used at the end of wbm.start() to finish the browser.
-
-## Note
-**wbm** is an **unofficial** solution. It's not recommended using **wbm** in your company or for marketing purpose.
-
## Contributing
Feel free to create pull requests. For major changes, please open an issue first to discuss what you would like to change.
diff --git a/test.js b/example.js
similarity index 100%
rename from test.js
rename to example.js
diff --git a/package-lock.json b/package-lock.json
index f9c1c28..38824e1 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "wbm",
- "version": "1.1.14",
+ "version": "1.1.15",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
diff --git a/package.json b/package.json
index 2d73870..2d1ae1e 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,7 @@
},
"name": "wbm",
"description": "wbm is an API to send bulk messages in whatsapp.",
- "version": "1.1.14",
+ "version": "1.1.15",
"main": "src/index.js",
"devDependencies": {},
"repository": {
diff --git a/src/api.js b/src/api.js
index ea3604d..e302ecc 100644
--- a/src/api.js
+++ b/src/api.js
@@ -10,6 +10,15 @@ let page = null;
let counter = { fails: 0, success: 0 }
const tmpPath = path.resolve(__dirname, '../tmp');
+const SELECTORS = {
+ LOADING: "progress",
+ INSIDE_CHAT: "document.getElementsByClassName('two')[0]",
+ QRCODE_PAGE: "body > div > div > .landing-wrapper",
+ QRCODE_DATA: "div[data-ref]",
+ QRCODE_DATA_ATTR: "data-ref",
+ SEND_BUTTON: 'div:nth-child(3) > button > span[data-icon="send"]'
+};
+
/**
* Initialize browser, page and setup page desktop mode
*/
@@ -68,7 +77,7 @@ function isAuthenticated() {
function needsToScan() {
return from(
page
- .waitForSelector('body > div > div > .landing-wrapper', {
+ .waitForSelector(SELECTORS.QRCODE_PAGE, {
timeout: 0,
}).then(() => false)
);
@@ -77,7 +86,7 @@ function needsToScan() {
function isInsideChat() {
return from(
page
- .waitForFunction(`document.getElementsByClassName('two')[0]`,
+ .waitForFunction(SELECTORS.INSIDE_CHAT,
{
timeout: 0,
}).then(() => true)
@@ -91,11 +100,11 @@ function deleteSession() {
* return the data used to create the QR Code
*/
async function getQRCodeData() {
- await page.waitForSelector("div[data-ref]", { timeout: 60000 });
- const qrcodeData = await page.evaluate(() => {
- let qrcodeDiv = document.querySelector("div[data-ref]");
- return qrcodeDiv.getAttribute("data-ref");
- });
+ await page.waitForSelector(SELECTORS.QRCODE_DATA, { timeout: 60000 });
+ const qrcodeData = await page.evaluate((SELECTORS) => {
+ let qrcodeDiv = document.querySelector(SELECTORS.QRCODE_DATA);
+ return qrcodeDiv.getAttribute(SELECTORS.QRCODE_DATA_ATTR);
+ }, SELECTORS);
return await qrcodeData;
}
@@ -120,7 +129,7 @@ async function generateQRCode() {
async function waitQRCode() {
// if user scan QR Code it will be hidden
try {
- await page.waitForSelector("div[data-ref]", { timeout: 30000, hidden: true });
+ await page.waitForSelector(SELECTORS.QRCODE_PAGE, { timeout: 30000, hidden: true });
} catch (err) {
throw await QRCodeExeption("Dont't be late to scan the QR Code.");
}
@@ -149,8 +158,8 @@ async function sendTo(phoneOrContact, message) {
try {
process.stdout.write("Sending Message...\r");
await page.goto(`https://web.whatsapp.com/send?phone=${phone}&text=${encodeURIComponent(message)}`);
- await page.waitForSelector("div#startup", { hidden: true, timeout: 60000 });
- await page.waitForSelector('#main > footer > div.vR1LG._3wXwX.copyable-area > div._2A8P4 > div > div._2_1wd.copyable-text.selectable-text', { timeout: 5000 });
+ await page.waitForSelector(SELECTORS.LOADING, { hidden: true, timeout: 60000 });
+ await page.waitForSelector(SELECTORS.SEND_BUTTON, { timeout: 5000 });
await page.keyboard.press("Enter");
await page.waitFor(1000);
process.stdout.clearLine();