mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-13 16:30:31 +00:00
Enhancement: forward cookies from request (#1804)
This commit is contained in:
parent
63f952509e
commit
d4edd432d8
3 changed files with 38 additions and 4 deletions
|
@ -37,3 +37,27 @@ export function addCookieToJar(url, headers) {
|
|||
cookieJar.setCookieSync(cookies[i], url.toString(), { ignoreError: true });
|
||||
}
|
||||
}
|
||||
|
||||
export function importCookieHeader(url, cookieHeader) {
|
||||
const cookies = cookieHeader.split(';');
|
||||
for (let i = 0; i < cookies.length; i += 1) {
|
||||
const [key, value] = cookies[i].trim().split('=');
|
||||
|
||||
// If there's an existing cookie with a matching key for this url,
|
||||
// we want to update it. Otherwise, we add a new cookie
|
||||
let existingCookie;
|
||||
try {
|
||||
existingCookie = cookieJar.getCookiesSync(url).find(existing => existing.key === key);
|
||||
} catch (e) {
|
||||
console.debug(`Failed to get cookies for ${url}: ${e}`)
|
||||
}
|
||||
|
||||
if (existingCookie) {
|
||||
existingCookie.value = value;
|
||||
} else {
|
||||
cookieJar.setCookieSync(new Cookie({
|
||||
key, value
|
||||
}), url.toString(), { ignoreError: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue