Cookies Reader


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description CookiesReader
* @augments
* @example
*
*/ const CookiesReader = (key = ``, debug = false) => {
if(key) {
let name = `${key}=`;
let decodedCookie = decodeURIComponent(document.cookie);
let cookies = decodedCookie.split(`;`);
for(let i = 0; i <cookies.length; i++) {
let cookie = cookies[i];
while (cookie.charAt(0) === ` `) {
// trim() ???
cookie = cookie.substring(1);
}
if (cookie.indexOf(name) === 0) {
return cookie.substring(name.length, cookie.length);
}
}
return "";
} else {
console.error(`Please pass the key of cookie!, key = ${key}`);
}
}; const getCookieValue = (key = ``, debug = false) => {
if(key) {
let name = `${key}=`;
let decodedCookie = decodeURIComponent(document.cookie);
let cookies = decodedCookie.split(`;`);
for(let i = 0; i <cookies.length; i++) {
let cookie = cookies[i];
while (cookie.charAt(0) === ` `) {
// trim() ???
cookie = cookie.substring(1);
}
if (cookie.indexOf(name) === 0) {
return cookie.substring(name.length, cookie.length);
}
}
return "";
} else {
console.error(`Please pass the key of cookie!, key = ${key}`);
}
}; export default CookiesReader; export {
CookiesReader,
getCookieValue,
}; /* import {
getCookieValue,
// CookiesReader,
} from "./cookies-reader"; JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_test`).split(`.`)[1])));
JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_prod`).split(`.`)[1]))); JSON.parse(decodeURIComponent(atob(getCookieValue(`${access_token}`).split(`.`)[1]))); */

demo


import {
getCookieValue,
// CookiesReader,
} from "./cookies-reader"; JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_test`).split(`.`)[1])));
JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_prod`).split(`.`)[1])));


import {
getCookieValue,
// CookiesReader,
} from "./cookies-reader"; import {
ROLE,
DEV
} from "./url-roles"; let access_token = ``; if (DEV === "production") {
access_token = `access_token_prod`;
} else {
access_token = `access_token_test`;
} // let token = JSON.parse(decodeURIComponent(atob(document.cookie.replace(`${access_token}`, ``).split(`.`)[1])));
let token = JSON.parse(decodeURIComponent(atob(getCookieValue(`${access_token}`).split(`.`)[1])));

HttpOnly & bug

bug

const CookiesReader = (key = ``, debug = false) => {
if(key) {
let name = `${key}=`;
let decodedCookie = decodeURIComponent(document.cookie);
let cookies = decodedCookie.split(`;`);
for(let i = 0; i <cookies.length; i++) {
let cookie = cookies[i];
while (cookie.charAt(0) === ` `) {
// trim() ???
cookie = cookie.substring(1);
}
if (cookie.indexOf(name) === 0) {
return cookie.substring(name.length, cookie.length);
}
}
return "";
} else {
console.error(`Please pass the key of cookie!, key = ${key}`);
}
}; CookiesReader(`JSESSIONID`);


CookiesReader的更多相关文章

  1. cookie 详解 与 封装 实用的cookie

    在WEB前端开发中,cookie是必不可少的,网上也有很多开源的库可以使用,不过我还是想自己总结一下,做个笔记并封装一个实用的库.(1)什么是cookie? 从web 角度 cookie是用于存储信息 ...

随机推荐

  1. break跳出嵌套循环体

    package com.wh.Object; public class Test { public static void main(String[] args) { // TODO Auto-gen ...

  2. 转 windows10 U盘系统启动盘怎么制作

    转 windows10 U盘系统启动盘怎么制作 现将http://jingyan.baidu.com/article/9f7e7ec05e24d56f29155455.html  将dvd 写入 is ...

  3. 209 Minimum Size Subarray Sum 大于给定和最短子数组

    给定一个含有 n 个正整数的数组和一个正整数 s , 找到一个最小的连续子数组的长度,使得这个子数组的数字和 ≥  s .如果不存在符合条件的子数组,返回 0.举个例子,给定数组 [2,3,1,2,4 ...

  4. Hadoop集群搭建及MapReduce应用

    一.Hadoop集群的搭建与配置 1.节点准备 集群规划: 主机名 IP 安装的软件 运行的进程 weekend 01 192.168.1.60 jdk.hadoop NameNode.DFSZKFa ...

  5. 转】R利剑NoSQL系列文章 之 Hive

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/3/ 感谢! Posted: Jul 27, 2013 Ta ...

  6. C# .net实现下载,带进度条

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  7. AJPFX总结JAVA基本数据类型

    1:关键字(掌握)        (1)被Java语言赋予特定含义的单词        (2)特点:                全部小写.        (3)注意事项:              ...

  8. Linux 之 2>&1

    我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下首先就是一个nohup表示当前用户和系统 ...

  9. Redis缓存Object,List对象

    一.到目前为止(jedis-2.2.0.jar),在Jedis中其实并没有提供这样的API对对象,或者是List对象的直接缓存,即并没有如下类似的API jedis.set(String key, O ...

  10. CAD参数绘制文字(网页版)

    在CAD设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX ...