* Retrieves cookies based on the provided filter and partition. * * @param {CookieFilter} [filter] - The filter to apply to the cookies. * @param {(BrowsingContextPartitionDescriptor|StorageKeyPartitionDescriptor)} [partition] - The partition to retrieve cookies from. * @returns {Promise
(filter = undefined, partition = undefined)
| 49 | * @throws {Error} If the partition parameter is provided but is not an instance of BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor. |
| 50 | */ |
| 51 | async getCookies(filter = undefined, partition = undefined) { |
| 52 | if (filter !== undefined && !(filter instanceof CookieFilter)) { |
| 53 | throw new Error(`Params must be an instance of CookieFilter. Received:'${filter}'`) |
| 54 | } |
| 55 | |
| 56 | if ( |
| 57 | partition !== undefined && |
| 58 | !(partition instanceof BrowsingContextPartitionDescriptor || partition instanceof StorageKeyPartitionDescriptor) |
| 59 | ) { |
| 60 | throw new Error( |
| 61 | `Params must be an instance of BrowsingContextPartitionDescriptor or StorageKeyPartitionDescriptor. Received:'${partition}'`, |
| 62 | ) |
| 63 | } |
| 64 | |
| 65 | const command = { |
| 66 | method: 'storage.getCookies', |
| 67 | params: { |
| 68 | filter: filter ? Object.fromEntries(filter.asMap()) : undefined, |
| 69 | partition: partition ? Object.fromEntries(partition.asMap()) : undefined, |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | let response = await this.bidi.send(command) |
| 74 | |
| 75 | let cookies = [] |
| 76 | response.result.cookies.forEach((cookie) => { |
| 77 | cookies.push( |
| 78 | new Cookie( |
| 79 | cookie.name, |
| 80 | new BytesValue(cookie.value.type, cookie.value.value), |
| 81 | cookie.domain, |
| 82 | cookie.path, |
| 83 | cookie.size, |
| 84 | cookie.httpOnly, |
| 85 | cookie.secure, |
| 86 | cookie.sameSite, |
| 87 | cookie.expiry, |
| 88 | ), |
| 89 | ) |
| 90 | }) |
| 91 | |
| 92 | if (Object.prototype.hasOwnProperty.call(response.result, 'partitionKey')) { |
| 93 | if ( |
| 94 | Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'userContext') && |
| 95 | Object.prototype.hasOwnProperty.call(response.result.partitionKey, 'sourceOrigin') |
| 96 | ) { |
| 97 | let partitionKey = new PartitionKey( |
| 98 | response.result.partitionKey.userContext, |
| 99 | response.result.partitionKey.sourceOrigin, |
| 100 | ) |
| 101 | return { cookies, partitionKey } |
| 102 | } |
| 103 | |
| 104 | return { cookies } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |