This commit is contained in:
62
endpoints/cookie.js
Normal file
62
endpoints/cookie.js
Normal file
@@ -0,0 +1,62 @@
|
||||
function sleep(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
function logBrowserCookies({ domain, cookieName, cookies }) {
|
||||
console.log(
|
||||
`[cookie] Browser cookies before return for ${domain} (${cookieName}): ${JSON.stringify(cookies)}`
|
||||
)
|
||||
}
|
||||
|
||||
async function waitForCookie({ domain, proxy, cookieName }, page) {
|
||||
if (!domain) throw new Error("Missing domain parameter")
|
||||
if (!cookieName) throw new Error("Missing cookieName parameter")
|
||||
|
||||
const timeout = global.timeOut || 60000
|
||||
const pollInterval = 250
|
||||
const startTime = Date.now()
|
||||
|
||||
if (proxy?.username && proxy?.password) {
|
||||
await page.authenticate({
|
||||
username: proxy.username,
|
||||
password: proxy.password,
|
||||
})
|
||||
}
|
||||
|
||||
await page.goto(domain, { waitUntil: "domcontentloaded" })
|
||||
|
||||
const userAgent = await page.evaluate(() => navigator.userAgent)
|
||||
|
||||
while (Date.now() - startTime < timeout) {
|
||||
const cookies = await page.cookies()
|
||||
const match = cookies.find((cookie) => cookie.name === cookieName)
|
||||
|
||||
if (match) {
|
||||
logBrowserCookies({ domain, cookieName, cookies })
|
||||
return {
|
||||
cookie_name: match.name,
|
||||
cookie_value: match.value,
|
||||
cookie_domain: match.domain,
|
||||
cookie_path: match.path,
|
||||
http_only: match.httpOnly,
|
||||
secure: match.secure,
|
||||
user_agent: userAgent,
|
||||
elapsed_time: (Date.now() - startTime) / 1000,
|
||||
}
|
||||
}
|
||||
|
||||
await sleep(pollInterval)
|
||||
}
|
||||
|
||||
const cookies = await page.cookies()
|
||||
logBrowserCookies({ domain, cookieName, cookies })
|
||||
|
||||
return {
|
||||
cookie_name: cookieName,
|
||||
cookie_value: null,
|
||||
user_agent: userAgent,
|
||||
elapsed_time: (Date.now() - startTime) / 1000,
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = waitForCookie
|
||||
Reference in New Issue
Block a user