Skip to content

Instantly share code, notes, and snippets.

@VikramTiwari
Last active November 27, 2019 23:15
Show Gist options
  • Save VikramTiwari/77d0b81e605b0af161e0021e130d34d3 to your computer and use it in GitHub Desktop.
Save VikramTiwari/77d0b81e605b0af161e0021e130d34d3 to your computer and use it in GitHub Desktop.
poc-chrome-background-throttle
const os = require('os')
const { launch } = require('chrome-launcher')
const puppeteer = require('puppeteer')
async function startChrome() {
const chrome = await launch({
ignoreDefaultFlags: true,
chromeFlags: [
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-ipc-flooding-protection',
'--disable-renderer-backgrounding',
'--enable-automation',
'--no-first-run',
'--profile-directory=Default'
],
startingUrl: 'chrome-search://local-ntp/local-ntp.html',
userDataDir: `/Users/${
os.userInfo().username
}/Library/Application Support/Google/Chrome`
})
return chrome
}
async function getBrowser(chrome) {
let browser = {}
browser = await puppeteer.connect({
browserURL: `http://127.0.0.1:${chrome.port}`,
defaultViewport: null
})
return browser
}
module.exports = {
getBrowser,
startChrome
}
const { getBrowser, startChrome } = require('./chrome')
const xpaths = [
'/html/body/div[6]/div[4]/div[3]/div/div/div/div/div[2]/div[1]/div[1]/div/section/div[2]/div[1]/div[2]/div/div/div/artdeco-dropdown/artdeco-dropdown-trigger/button/span',
'/html/body/div[6]/div[4]/div[3]/div/div/div/div/div[2]/div[1]/div[1]/div/section/div[2]/div[1]/div[2]/div/div/div/artdeco-dropdown/artdeco-dropdown-content/div/ul/li[2]/div/artdeco-dropdown-item/span[1]'
]
const mousemoves = [
[0, 0],
[10, 10],
[20, 20],
[30, 30],
[40, 40],
[50, 50],
[60, 60],
[70, 70],
[80, 80],
[90, 90],
[100, 100]
]
async function randomMouseMoves(page) {
for (let index = 0; index < mousemoves.length; index++) {
const mousemove = mousemoves[index]
console.log(`mousemove start: ${new Date()}`)
await page.mouse.move(mousemove[0], mousemove[1])
console.log(`mousemove end: ${new Date()}`)
}
}
async function run() {
const chrome = await startChrome()
const browser = await getBrowser(chrome)
const page = await browser.newPage()
// to move the new tab in background
const allPages = await browser.pages()
allPages[0].bringToFront()
// start execution in the current tab
await page.goto('https://www.linkedin.com/in/vikramtiwari/')
for (let index = 0; index < xpaths.length; index++) {
const xpath = xpaths[index]
let value = ''
await randomMouseMoves(page)
console.log(`${new Date()}`)
await page.waitForXPath(xpath)
const element = (await page.$x(xpath))[0]
if (element) {
value = await page.evaluate(el => el.innerText, element)
}
console.log(`${new Date()} - ${xpath} - ${value}`)
await element.click()
}
}
run()
{
"name": "poc-chrome-background-throttle",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"chrome-launcher": "^0.11.2",
"puppeteer": "^1.19.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment