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. Tree CodeForces -932D

    错误记录:如下注释语句 #include<cstdio> #include<algorithm> using namespace std; typedef long long ...

  2. 题解报告:hdu 1541 Stars(经典BIT)

    Problem Description Astronomers often examine star maps where stars are represented by points on a p ...

  3. Matlab实现图像分割 分类: 图像处理 2014-06-14 21:31 662人阅读 评论(1) 收藏

    下面使用极小值点阈值选取方法,编写MATLAB程序实现图像分割的功能. 极小值点阈值选取法即从原图像的直方图的包络线中选取出极小值点, 并以极小值点为阈值将图像转为二值图像 clear all; cl ...

  4. home键拦截

    代码: public class HomeKeyReceiver extends BroadcastReceiver { private static final String LOG_TAG = & ...

  5. OpenCV2.4.9 + Ubuntu15.04配置

    为了run Car-Detection安装了OpenCV. 基本上就照着这个弄下来: ubuntu14.04 + OpenCV2.4.9 配置方法   1.  安装openCV 所需依赖库或软件: s ...

  6. 174 Dungeon Game 地下城游戏

    一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格布局.我们英勇的骑士(K)最初被安置在左上角的房间里,并且必须通过地下城对抗来拯救公主.骑士具有以正整数 ...

  7. 浅析套接字中SO_REUSEPORT和SO_REUSEADDR的区别

    Socket的基本背景 在讨论这两个选项的区别时,我们需要知道的是BSD实现是所有socket实现的起源.基本上其他所有的系统某种程度上都参考了BSD socket实现(或者至少是其接口),然后开始了 ...

  8. [BZOJ1016][JSOI2008]最小生成树计数 最小生成树 搜索

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1016 做这道题之前需要知道一些结论,同一个图的最小生成树中相同权值的边的个数是不会变的,如 ...

  9. RFTWEB测试对象抓取的方法

    本文转自:http://feiyeguohai.iteye.com/blog/1468576 Rational Functional Tester (RFT) 作为 IBM 自己设计研发的自动化测试工 ...

  10. 简说JAVA8引入函数式的问题

    JAVA8中加入lambda演算是一个令人兴奋的新特性——虽然这个新特性来得太迟了,目前的主流开发语言中,JAVA似乎是最后一个支持函数式思维的语言. 虽然晚了点,但总比没有好——况且我认为它的实现还 ...