UNPKG

selenium-webdriver-mcp

Version:
66 lines 2.27 kB
export class CookieService { driver; constructor(driver) { this.driver = driver; } async getCookies() { const cookies = await this.driver.manage().getCookies(); return cookies.map(cookie => cookie.name); } async getCookieByName(name) { const cookie = await this.driver.manage().getCookie(name); return cookie ? { name: cookie.name, value: cookie.value } : null; } async addCookieByName(name, value) { await this.driver.manage().addCookie({ name, value }); } async setCookie(cookie) { // Parse cookie string into an object const [nameValue, ...attributes] = cookie.split(';').map(part => part.trim()); let name = ''; let value = ''; if (nameValue) { const parts = nameValue.split('='); name = parts[0] ?? ''; value = parts[1] ?? ''; } const cookieObj = { name, value }; attributes.forEach(attr => { const parts = attr.split('='); const attrName = parts[0]; const attrValue = parts[1]; if (!attrName) return; switch (attrName.toLowerCase()) { case 'name': cookieObj.name = attrValue; break; case 'domain': cookieObj.domain = attrValue; break; case 'path': cookieObj.path = attrValue; break; case 'expires': if (attrValue !== undefined) { cookieObj.expiry = Math.floor(new Date(attrValue).getTime() / 1000); } break; case 'secure': cookieObj.secure = true; break; case 'httponly': cookieObj.httpOnly = true; break; } }); await this.driver.manage().addCookie(cookieObj); } async deleteCookie(name) { await this.driver.manage().deleteCookie(name); } async deleteAllCookies() { await this.driver.manage().deleteAllCookies(); } } //# sourceMappingURL=cookieService.js.map