Skip to content

Instantly share code, notes, and snippets.

@RoyiNamir
Created November 18, 2018 16:13
Show Gist options
  • Save RoyiNamir/b98466957296f547f208167050be8197 to your computer and use it in GitHub Desktop.
Save RoyiNamir/b98466957296f547f208167050be8197 to your computer and use it in GitHub Desktop.
import puppeteer, {ElementHandle, Page} from "puppeteer";
import {LOGIN_URL, USER} from "./models/consts";
import {IUser} from "./models/i-user";
const getPropAsync = async (elementHandle: ElementHandle, propertyName: string) =>
{
let href = await elementHandle.getProperty(propertyName);
return await (await elementHandle.getProperty(propertyName)).jsonValue();
};
async function doLogin(page: Page, user: IUser)
{
console.log(user);
await page.goto(LOGIN_URL);
let inputEmail: ElementHandle = await page.waitForSelector("#login_email");
await page.$eval('#login_email', (el, a) => (el as HTMLInputElement).value = a.username, user);
await page.$eval('#login_passw', (el, a) => (el as HTMLInputElement).value = a.password, user);
await page.evaluate((a) =>
{
// click the first element
return ([...document.querySelectorAll(".btn.btn-1")][0] as HTMLElement).click();
});
}
async function clickRandomLink(page: Page): Promise<any>
{
await page.evaluate((a, shuf) =>
{
function shuffle(array)
{
var tmp,
current,
top = array.length;
if (top)
{
while (--top)
{
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
}
return array;
}
let anchor = shuffle([...document.querySelectorAll('a')].filter(
f =>f.getAttribute("href") && f.target!="_blank" && /*f.innerText.indexOf('Earn Rewards') > -1 && */f.href.includes('www2.test.mutualart.com') &&
!f.href.includes('#') && !f.href.toLowerCase().includes('external') && ![
'Log Out',
'Log in',
'Auctions'
].some(a => f.innerText.toLowerCase()
.includes(a.toLowerCase()))))[0];
console.log("About to click " + anchor.href);
(anchor).click();
});
}
(async () =>
{
const browser = await puppeteer.launch({headless: false});
const page: Page = await browser.newPage();
await doLogin(page, USER);
await page.waitForNavigation({ timeout:99999999});
page.on('console', consoleObj => console.log(consoleObj.text()));
for (let i = 0; i < 50000; i++)
{
try
{
console.log('███79');
await clickRandomLink(page);
console.log('███81');
// console.log(new Date());
console.log(i+" Scanned - " );
await page.waitForNavigation({ timeout:99999999});
}
catch (e)
{
console.log("ERROR "+e )
await page.goto('https://www2.test.mutualart.com/?d='+ +new Date());
// console.log('███83');
// await page.waitForNavigation();
//console.log('███85');
}
}
await page.waitFor(2000);
// await page.goBack();
// await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment