Skip to content

Commit

Permalink
fix wait for loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Briuor committed Jun 3, 2021
1 parent 0aff7ab commit dbb752d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<img style="border-radius: 5px" src="https://raw.githubusercontent.com/Briuor/wbm/master/assets/demo.gif">
</p>

## Note
**wbm** is an **unofficial** solution. It's not recommended using **wbm** in your company or for marketing purpose.

## Installation
```bash
> npm install wbm
Expand Down Expand Up @@ -87,6 +90,7 @@ wbm.start().then(async () => {
Type: `boolean`<br />
* **session**<br />
Keep user session, so the user must scan the QR Code once.<br />
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.<br />
Default: `true`<br />
Type: `boolean`

Expand Down Expand Up @@ -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.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
29 changes: 19 additions & 10 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
);
Expand All @@ -77,7 +86,7 @@ function needsToScan() {
function isInsideChat() {
return from(
page
.waitForFunction(`document.getElementsByClassName('two')[0]`,
.waitForFunction(SELECTORS.INSIDE_CHAT,
{
timeout: 0,
}).then(() => true)
Expand All @@ -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;
}

Expand All @@ -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.");
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit dbb752d

Please sign in to comment.