Skip to content

Instantly share code, notes, and snippets.

@CQBinh
Created February 25, 2019 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CQBinh/283efd710d8dd6752f2e128988cb48d7 to your computer and use it in GitHub Desktop.
Save CQBinh/283efd710d8dd6752f2e128988cb48d7 to your computer and use it in GitHub Desktop.
import moment from 'moment'
import lodash from 'lodash'
import map from 'apr-map'
import timeFormater from 'moment-duration-format'
import asyncUtil from './util/async'
import util from './util/index'
import ncp from 'copy-paste'
require('colors')
const browserHelper = require('./util/browser-helper').instance
const FB_HOST = 'https://facebook.com'
timeFormater(moment)
const LOGO_SELECTOR = 'span._2md'
const DISCUSS_SEL = 'a._4-h7.fbReactComposerAttachmentSelector_STATUS > span._4-fs > span._5qtp'
const POLL_SEL = 'tr._51mx:nth-child(2) > td.pas._1fng._51m-:nth-child(1)'
const SUBMIT_BUTTON_SEL = 'div._332r > button'
const LOAD_LINK_TIMEOUT = 120000
let defaultPage
let browser
let linkToShare
let textOnStatus
async function start(link, text, data) {
linkToShare = link
textOnStatus = text
await init()
// browserHelper.getDefaultPage().on('console', msg => {
// console.log(msg)
// })
const startTime = Date.now()
let numberOfGroups = 0
const numberOfAccount = data.length
await map.series(data, async (row) => {
row.groupIds = row.groupIds.map(item => {
if (!item.trim()) {
return null
}
return `${FB_HOST}/groups/${item.trim()}`
}).filter(item => !!item)
const credential = lodash.pick(row, ['email', 'pass'])
numberOfGroups += row.groupIds.length
await login(credential)
await asyncUtil.wait()
await shareAsAdmin(row.groupIds)
// await shareAsMember(data.groupIds.member)
await logout()
})
let time = Date.now() - startTime
time = moment.duration(time, 'ms').format()
console.log(`Share thanh cong den ${numberOfGroups} group su dung ${numberOfAccount} tai khoan trong thoi gian ${time}`.green)
browser.close()
}
async function shareAsAdmin(groups) {
await map.series(groups, async (groupLink) => {
await loadLinkOnNewTab(groupLink)
})
await map.series(groups, async (_, index) => {
await pasteLinkOnTab(index + 1)
})
await map.series(groups, async (_, index) => {
await submitOnTab(index + 1)
})
await asyncUtil.wait(util.time.randomBetween(3456, 4567))
console.log('Chia se thanh cong den cac group admin'.green)
}
async function shareAsMember(groups) {
await map.series(groups, async (groupLink) => {
await loadLink(groupLink)
await asyncUtil.wait()
await shareLink()
await waitForLogo()
})
}
async function init() {
browser = await browserHelper.launchBrowser()
}
async function login(credential) {
defaultPage = await browserHelper.getDefaultPage()
await defaultPage.goto('https://fb.com', { waitUntil: 'networkidle2' })
const EMAIL_SELECTOR = 'input#email.inputtext'
const PASS_SELECTOR = 'input#pass.inputtext'
await defaultPage.waitFor(EMAIL_SELECTOR)
await defaultPage.focus(EMAIL_SELECTOR)
await defaultPage.keyboard.type(credential.email)
await defaultPage.focus(PASS_SELECTOR)
await defaultPage.keyboard.type(credential.pass)
await defaultPage.keyboard.press(String.fromCharCode(13))
await waitForLogo()
console.log(`Dang nhap thanh cong vao tai khoan: ${credential.email}`.green)
}
async function logout() {
browser.close()
browser = await browserHelper.launchBrowser()
}
async function loadLink(link) {
defaultPage = await browserHelper.getDefaultPage()
console.log(`Tien hanh chia se vao group: ${link}`.yellow)
await defaultPage.goto(link).catch(async (error) => {
console.log(`Loi khi truy cap group: ${link}`.red, error)
await asyncUtil.timeout(LOAD_LINK_TIMEOUT)
await defaultPage.goto(link)
})
await waitForLogo()
const discussElement = await defaultPage.$(DISCUSS_SEL)
await discussElement.click()
const pollElement = await defaultPage.$(POLL_SEL)
if (pollElement) {
await pollElement.click()
await asyncUtil.wait()
await discussElement.click()
}
}
async function loadLinkOnNewTab(link) {
console.log(`Tien hanh chia se bang nick admin vao group: ${link}`.yellow)
const newPageData = await browserHelper.getNewPage()
await newPageData.goto(link).catch(async (error) => {
console.log(`Loi khi truy cap group: ${link}`.red, error)
await asyncUtil.timeout(LOAD_LINK_TIMEOUT)
await newPageData.goto(link)
})
await waitForLogo()
const discussElement = await newPageData.$(DISCUSS_SEL)
await discussElement.click()
const pollElement = await newPageData.$(POLL_SEL)
if (pollElement) {
await pollElement.click()
await asyncUtil.wait()
await discussElement.click()
}
}
async function shareLink() {
defaultPage = await browserHelper.getDefaultPage()
ncp.copy(linkToShare)
await paste()
ncp.copy(textOnStatus)
await asyncUtil.wait(util.time.randomBetween(4142, 6969))
await clear()
await paste()
await asyncUtil.wait()
await defaultPage.click(SUBMIT_BUTTON_SEL)
console.log('Chia se thanh cong'.green)
await asyncUtil.wait(util.time.randomBetween(3456, 4567))
}
async function pasteLinkOnTab(index) {
const page = await browserHelper.getPage(index)
await page.bringToFront()
ncp.copy(linkToShare)
await paste(index)
await asyncUtil.wait(util.time.randomBetween(1234, 2121))
}
async function submitOnTab(index) {
const page = await browserHelper.getPage(index)
await page.bringToFront()
ncp.copy(textOnStatus)
await clear(index)
await paste(index)
await asyncUtil.wait()
await page.click(SUBMIT_BUTTON_SEL)
}
async function paste(index = 0) {
const page = await browserHelper.getPage(index)
await page.keyboard.down('ControlLeft')
await page.keyboard.down('KeyV')
await page.keyboard.up('KeyV')
await page.keyboard.up('ControlLeft')
}
async function clear(index = 0) {
const page = await browserHelper.getPage(index)
await page.keyboard.down('ControlLeft')
await page.keyboard.down('KeyA')
await page.keyboard.up('KeyA')
await page.keyboard.up('ControlLeft')
await page.keyboard.press('Delete')
}
async function waitForLogo(index = 0) {
const page = await browserHelper.getPage(index)
await page.waitFor(LOGO_SELECTOR)
}
export default {
start
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment