Cookies Reader


  1. "use strict";
  2. /**
  3. *
  4. * @author xgqfrms
  5. * @license MIT
  6. * @copyright xgqfrms
  7. *
  8. * @description CookiesReader
  9. * @augments
  10. * @example
  11. *
  12. */
  13. const CookiesReader = (key = ``, debug = false) => {
  14. if(key) {
  15. let name = `${key}=`;
  16. let decodedCookie = decodeURIComponent(document.cookie);
  17. let cookies = decodedCookie.split(`;`);
  18. for(let i = 0; i <cookies.length; i++) {
  19. let cookie = cookies[i];
  20. while (cookie.charAt(0) === ` `) {
  21. // trim() ???
  22. cookie = cookie.substring(1);
  23. }
  24. if (cookie.indexOf(name) === 0) {
  25. return cookie.substring(name.length, cookie.length);
  26. }
  27. }
  28. return "";
  29. } else {
  30. console.error(`Please pass the key of cookie!, key = ${key}`);
  31. }
  32. };
  33. const getCookieValue = (key = ``, debug = false) => {
  34. if(key) {
  35. let name = `${key}=`;
  36. let decodedCookie = decodeURIComponent(document.cookie);
  37. let cookies = decodedCookie.split(`;`);
  38. for(let i = 0; i <cookies.length; i++) {
  39. let cookie = cookies[i];
  40. while (cookie.charAt(0) === ` `) {
  41. // trim() ???
  42. cookie = cookie.substring(1);
  43. }
  44. if (cookie.indexOf(name) === 0) {
  45. return cookie.substring(name.length, cookie.length);
  46. }
  47. }
  48. return "";
  49. } else {
  50. console.error(`Please pass the key of cookie!, key = ${key}`);
  51. }
  52. };
  53. export default CookiesReader;
  54. export {
  55. CookiesReader,
  56. getCookieValue,
  57. };
  58. /*
  59. import {
  60. getCookieValue,
  61. // CookiesReader,
  62. } from "./cookies-reader";
  63. JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_test`).split(`.`)[1])));
  64. JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_prod`).split(`.`)[1])));
  65. JSON.parse(decodeURIComponent(atob(getCookieValue(`${access_token}`).split(`.`)[1])));
  66. */

demo


  1. import {
  2. getCookieValue,
  3. // CookiesReader,
  4. } from "./cookies-reader";
  5. JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_test`).split(`.`)[1])));
  6. JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_prod`).split(`.`)[1])));
  1. import {
  2. getCookieValue,
  3. // CookiesReader,
  4. } from "./cookies-reader";
  5. import {
  6. ROLE,
  7. DEV
  8. } from "./url-roles";
  9. let access_token = ``;
  10. if (DEV === "production") {
  11. access_token = `access_token_prod`;
  12. } else {
  13. access_token = `access_token_test`;
  14. }
  15. // let token = JSON.parse(decodeURIComponent(atob(document.cookie.replace(`${access_token}`, ``).split(`.`)[1])));
  16. let token = JSON.parse(decodeURIComponent(atob(getCookieValue(`${access_token}`).split(`.`)[1])));

HttpOnly & bug

bug

  1. const CookiesReader = (key = ``, debug = false) => {
  2. if(key) {
  3. let name = `${key}=`;
  4. let decodedCookie = decodeURIComponent(document.cookie);
  5. let cookies = decodedCookie.split(`;`);
  6. for(let i = 0; i <cookies.length; i++) {
  7. let cookie = cookies[i];
  8. while (cookie.charAt(0) === ` `) {
  9. // trim() ???
  10. cookie = cookie.substring(1);
  11. }
  12. if (cookie.indexOf(name) === 0) {
  13. return cookie.substring(name.length, cookie.length);
  14. }
  15. }
  16. return "";
  17. } else {
  18. console.error(`Please pass the key of cookie!, key = ${key}`);
  19. }
  20. };
  21. CookiesReader(`JSESSIONID`);


CookiesReader的更多相关文章

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

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

随机推荐

  1. C#中的list的System.Predicate<in T>和System.Comparison<in T>的应用

    public class Data { ; ; ; ; public Data() { count++; ma = count; } } //一句话删除满足要求的集合 Asm.RemoveAll((D ...

  2. Selenium2工作原理

    Selenium 经历了两个版本,Selenium 1.0 和 Selenium 2.0,本文仅介绍Selenium2的原理,在Selenium 2.0 主推的是WebDriver,Selenium2 ...

  3. Miller-Rabin 素性测试 与 Pollard Rho 大整数分解

    \(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...

  4. call方法的使用bug--参数undefined

    call/apply是函数原型定义的方法(Function.prorotype),在使用时要注意第一个形参(args[0]),一定是指向函数所要挂载的上下文对象--context,若对象非必须,则要将 ...

  5. Vue.js学习笔记--4. 组件的基本使用

    整理自官网教程 -- https://cn.vuejs.org/ 1. 所有Vue组件同时也都是Vue实例,分为全局组件和局部组件,注册方式如下. <div id="app" ...

  6. CSS3实现边框线条动画特效

    <div class="box-line"></div> CSS代码 .box-line, .box-line::before, .box-line::af ...

  7. android ListView 分析(一)

    需要了解的内容 1. listview中的getItemAtPosition与Adapter的getItem的position的区别          listView中的getItemAtPosit ...

  8. wget安装更新

    #查看当前wget版本信息 wget -V #下载 wget https://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz #解压 tar xvf wget-1.19.t ...

  9. vscode增加xdebug扩展

    首先确保php增加了xdebug扩展,方法很多,可参考 https://www.cnblogs.com/wanghaokun/p/9084188.html.可通过phpinfo()查看是否已开启支持. ...

  10. C++学习_继承覆盖重载

    今天通过对实验二继承,重载,覆盖的学习,让我更深一步理解了这些概念的区别. 首先来明确一个概念,函数名即地址,也就是说函数名就是个指针. 编译阶段,编译器为每个函数的代码分配一个地址空间并编译函数代码 ...