All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.src.services.cookie-reader.js Maven / Gradle / Ivy

import { isUndefined } from "../shared/utils";

let lastCookies = {};
let lastCookieString = "";

/**
 * @returns {Object} List of all cookies
 */
export function getCookies() {
  let cookieArray;
  let cookie;
  let i;
  let index;
  let name;
  const currentCookieString = document.cookie;

  if (currentCookieString !== lastCookieString) {
    lastCookieString = currentCookieString;
    cookieArray = lastCookieString.split("; ");
    lastCookies = {};

    for (i = 0; i < cookieArray.length; i++) {
      cookie = cookieArray[i];
      index = cookie.indexOf("=");
      if (index > 0) {
        // ignore nameless cookies
        name = safeDecodeURIComponent(cookie.substring(0, index));
        // the first value that is seen for a cookie is the most
        // specific one.  values for the same cookie name that
        // follow are for less specific paths.
        if (isUndefined(lastCookies[name])) {
          lastCookies[name] = safeDecodeURIComponent(
            cookie.substring(index + 1),
          );
        }
      }
    }
  }
  return lastCookies;
}

function safeDecodeURIComponent(str) {
  try {
    return decodeURIComponent(str);
  } catch (e) {
    return str;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy