68 lines
1.6 KiB
JavaScript
68 lines
1.6 KiB
JavaScript
|
import puppeteer from "puppeteer-core"
|
||
|
|
||
|
function sleep(ms) {
|
||
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
||
|
}
|
||
|
|
||
|
async function main() {
|
||
|
try {
|
||
|
const browser = await puppeteer.launch({
|
||
|
headless: false,
|
||
|
ignoreHTTPSErrors: true,
|
||
|
})
|
||
|
console.log("Opened Browser")
|
||
|
const page = await browser.newPage()
|
||
|
console.log("Opened Page")
|
||
|
|
||
|
await page.setRequestInterception(true)
|
||
|
let usefulRequestHeaders = {}
|
||
|
page.on("request", (request) => {
|
||
|
let url = request.url()
|
||
|
console.log("request url", url)
|
||
|
if (url == "usefulrequest") {
|
||
|
usefulRequestHeaders = { ...request.headers() }
|
||
|
console.log("hello!")
|
||
|
}
|
||
|
request.continue()
|
||
|
})
|
||
|
|
||
|
await page.goto("https://www.google.com")
|
||
|
console.log("Went to Page")
|
||
|
|
||
|
await page.waitForSelector('button[type="submit"]')
|
||
|
await page.type('input[name="userId"]', "username")
|
||
|
await page.type('input[name="password"]', "password")
|
||
|
console.log("Typed Credentials")
|
||
|
await page.click('button[type="submit"]')
|
||
|
|
||
|
for (let i = 0; i < 10; i++) {
|
||
|
if (usefulRequestHeaders.headerName) {
|
||
|
await browser.close()
|
||
|
return console.log("success")
|
||
|
}
|
||
|
await sleep(300)
|
||
|
}
|
||
|
console.log("after sleep loop")
|
||
|
|
||
|
// const response = await fetch(
|
||
|
// "https://www.google.com",
|
||
|
// {
|
||
|
// method: "POST",
|
||
|
// headers: usefulRequestHeaders,
|
||
|
// body: JSON.stringify({Request
|
||
|
// }),
|
||
|
// }
|
||
|
// );
|
||
|
|
||
|
// if (response.ok) {
|
||
|
// console.log(await response.json());
|
||
|
// }
|
||
|
|
||
|
await browser.close()
|
||
|
} catch (err) {
|
||
|
return console.error("Critical failure", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
main()
|