<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<title>JS 解析 JWT Token 信息</title>
</head>
<body>
<textarea placeholder="粘贴 JWT Token"></textarea>
</body>
</html>
textarea {
width: 90vw;
height: 90vh;
font-size: 1.5em;
}
document.querySelector("textarea").oninput = function () {
try {
let json = parseJwt(this.value);
this.value = JSON.stringify(json, null, 2);
} catch (ex) {
console.debug(ex);
this.value = `${ex}`;
}
}
function parseJwt(token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
};