diff --git a/README.md b/README.md index 04981d0..3682010 100644 --- a/README.md +++ b/README.md @@ -119,13 +119,15 @@ Open a page and wait for a specific cookie to appear. ```json { "domain": "https://example.com/", - "cookieName": "session_id" + "cookieName": "session_id", + "includeCookies": true } ``` #### Parameters - **`domain`** (required): Target website URL - **`cookieName`** (required): Cookie name to wait for +- **`includeCookies`** (optional): Include the full browser cookie jar in the response - **`user_agent`** (optional): User-Agent to use for the browser session - **`proxy`** (optional): Proxy configuration object @@ -138,6 +140,16 @@ Open a page and wait for a specific cookie to appear. "cookie_path": "/", "http_only": true, "secure": true, + "cookies": [ + { + "name": "session_id", + "value": "abc123", + "domain": ".example.com", + "path": "/", + "httpOnly": true, + "secure": true + } + ], "user_agent": "Mozilla/5.0...", "elapsed_time": 1.42 } @@ -148,6 +160,7 @@ Open a page and wait for a specific cookie to appear. { "cookie_name": "session_id", "cookie_value": null, + "cookies": [], "user_agent": "Mozilla/5.0...", "elapsed_time": 60.0 } diff --git a/api_test.py b/api_test.py index 9a4d089..8896f09 100644 --- a/api_test.py +++ b/api_test.py @@ -27,6 +27,7 @@ async def main(): json={ "domain": "https://example.com/", "cookieName": "session_id", + "includeCookies": True, }, ) print(resp3.json()) diff --git a/endpoints/cookie.js b/endpoints/cookie.js index 7bb608d..b593206 100644 --- a/endpoints/cookie.js +++ b/endpoints/cookie.js @@ -8,7 +8,29 @@ function logBrowserCookies({ domain, cookieName, cookies }) { ) } -async function waitForCookie({ domain, proxy, cookieName }, page) { +function buildCookieResponse({ cookieName, matchedCookie, userAgent, elapsedTime, cookies, includeCookies }) { + const result = { + cookie_name: matchedCookie?.name ?? cookieName, + cookie_value: matchedCookie?.value ?? null, + user_agent: userAgent, + elapsed_time: elapsedTime, + } + + if (matchedCookie) { + result.cookie_domain = matchedCookie.domain + result.cookie_path = matchedCookie.path + result.http_only = matchedCookie.httpOnly + result.secure = matchedCookie.secure + } + + if (includeCookies) { + result.cookies = cookies + } + + return result +} + +async function waitForCookie({ domain, proxy, cookieName, includeCookies }, page) { if (!domain) throw new Error("Missing domain parameter") if (!cookieName) throw new Error("Missing cookieName parameter") @@ -33,16 +55,14 @@ async function waitForCookie({ domain, proxy, cookieName }, page) { 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, - } + return buildCookieResponse({ + cookieName, + matchedCookie: match, + userAgent, + elapsedTime: (Date.now() - startTime) / 1000, + cookies, + includeCookies, + }) } await sleep(pollInterval) @@ -51,12 +71,13 @@ async function waitForCookie({ domain, proxy, cookieName }, page) { 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, - } + return buildCookieResponse({ + cookieName, + userAgent, + elapsedTime: (Date.now() - startTime) / 1000, + cookies, + includeCookies, + }) } module.exports = waitForCookie diff --git a/index.js b/index.js index c778154..5c53c24 100644 --- a/index.js +++ b/index.js @@ -173,6 +173,9 @@ app.post('/cookie', async (req, res) => { if (!data || typeof data.domain !== 'string' || typeof data.cookieName !== 'string') { return res.status(400).json({ message: 'Bad Request: missing or invalid domain/cookieName' }) } + if (typeof data.includeCookies !== 'undefined' && typeof data.includeCookies !== 'boolean') { + return res.status(400).json({ message: 'Bad Request: invalid includeCookies' }) + } if (data.user_agent && typeof data.user_agent !== 'string') { return res.status(400).json({ message: 'Bad Request: invalid user_agent' }) }