Skip to content

Instantly share code, notes, and snippets.

@EduardoAC
Created February 11, 2024 08:38
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/35ba733a64854993483ab543de066aa4 to your computer and use it in GitHub Desktop.
Save EduardoAC/35ba733a64854993483ab543de066aa4 to your computer and use it in GitHub Desktop.
Handling cookie changes events trigger by chrome.cookies.onChanged.addListener
export async function cookiesHandler({ cause, cookie, removed }: chrome.cookies.CookieChangeInfo) {
// We are interested in cookies which are set/updated in the browser
const hasUpdateCookie = !removed && cause === "explicit";
// We are interested when the cookie is removed from the browser
const hasClearCookie = removed && (cause === "explicit" || cause === "expired_overwrite");
const isRelevantCookieUpdate = hasUpdateCookie || hasClearCookie;
// We monitor only cookies within the our webApp domain
const yourWebAppDomain = extractDomain(`${your-webApp-url}`);
// Check for the language cookie belonging to your-webApp-url domain
const isLanguageCookieFromHub =
cookie &&
cookie.domain &&
cookie.domain === yourWebAppDomain &&
cookie.name === "{your-webApp-name}-language";
if (isRelevantCookieUpdate && isLanguageCookieFromHub) {
const language = cookie.value; // Assuming value is the language code
if (language) {
updateLanguage(language);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment