Skip to content

Instantly share code, notes, and snippets.

@EduardoAC
Created February 11, 2024 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EduardoAC/46ebc2d66e2cdef077290e5ec3e9fa83 to your computer and use it in GitHub Desktop.
Save EduardoAC/46ebc2d66e2cdef077290e5ec3e9fa83 to your computer and use it in GitHub Desktop.
Helper function to extract cookie value field using consider the cookie stores an object using RFC-6265 standard
export function extractValueFromCookie<T>(cookie: chrome.cookies.Cookie): T | undefined {
try {
// Follow standard parsing RFC-6265 -> https://github.com/js-cookie/js-cookie/blob/master/src/converter.mjs
const decodeCookieValue = cookie.value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent)
return JSON.parse(decodeCookieValue)
} catch (e) {
console.error("Cookie couldn't be parse")
console.error(e)
}
return undefined
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment