Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const puppeteer = require('puppeteer');
- let browser = null;
- let pages = [];
- async function getBrowser() {
- if (browser === null) {
- browser = await puppeteer.launch({
- args: ['--no-sandbox'],
- // headless: false
- });
- }
- return browser;
- }
- module.exports = async function newPage() {
- const chrome = await getBrowser();
- const page = await chrome.newPage();
- page.setUserAgent(
- 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
- );
- pages.push(page);
- return page;
- };
- const fs = require('fs');
- const newPage = require('./browser');
- function delay(t = 1000) {
- return new Promise(r => setTimeout(r, t));
- }
- (async function sendMsg() {
- const page = await newPage();
- const cookiesString = fs.readFileSync('./zalo.json');
- const cookies = JSON.parse(cookiesString);
- await page.setCookie(...cookies);
- await page.goto('https://chat.zalo.me/');
- console.log('Go zalo');
- await delay(2000);
- await page
- .waitForSelector('.avatar');
- await delay(1000);
- await page.click('.avatar');
- await delay(2000);
- await page.waitForSelector('.msg-item');
- await page.waitForSelector('#contact-search-input');
- await page.click('#contact-search-input');
- await page.type('#contact-search-input', 'Dropshipping');
- await delay(1000);
- await page.waitForSelector('#searchResultList');
- await page.click('#searchResultList .item');
- await delay(1000);
- await page.waitForSelector('#richInput');
- await page.click('#richInput > div');
- await delay(1000);
- page.type('#richInput > div', 'msg from puppeteer');
- await delay(1000);
- await page.keyboard.press(String.fromCharCode(13));
- console.log('done');
- await delay(100000);
- page.close();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement