Skip to content

Commit 63d3972

Browse files
committed
Added debouncing before parsing input + clear output before parsing
1 parent c95f5c8 commit 63d3972

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

src/Pages/Home/JwtDecoder/JwtDecoder.cshtml

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@page "~/jwt-decoder"
1+
@page "~/jwt-decoder"
22
@model IdentityServerHost.Pages.Home.JwtDecoder
33

44
<div class="jwt-decoder-page">
@@ -332,38 +332,10 @@
332332
});
333333
334334
const jwtInput = $('#jwt-input');
335-
jwtInput.on('input', async function() {
336-
decodedJwt = {
337-
header: null,
338-
payload: null,
339-
signature: null
340-
};
341-
342-
const jwt = $(this).text();
343-
if (jwt) {
344-
try {
345-
const parts = jwt.split('.');
346-
if (parts.length === 3) {
347-
const header = JSON.parse(atob(parts[0]));
348-
const payload = JSON.parse(atob(parts[1]));
349-
const signature = parts[2];
350-
351-
decodedJwt = {
352-
header: header,
353-
payload: payload,
354-
signature: signature
355-
};
356-
await showDecodedJwt(parts, header, payload, signature);
357-
colorJwtInput($(this), parts);
358-
} else {
359-
showError('Invalid JWT format. A JWT should have three parts separated by dots.');
360-
}
361-
} catch (e) {
362-
showError('Error decoding JWT: ' + e.message);
363-
}
364-
} else {
365-
await showDecodedJwt(null, null, null, ' ');
366-
}
335+
let debounceTimeout;
336+
jwtInput.on('input', function() {
337+
clearTimeout(debounceTimeout);
338+
debounceTimeout = setTimeout(async () => await parseJwt.call(this), 300); // 300ms debounce
367339
});
368340
369341
@if (Model.View?.Token != null)
@@ -376,6 +348,41 @@
376348
}
377349
}
378350
351+
async function parseJwt() {
352+
decodedJwt = {
353+
header: null,
354+
payload: null,
355+
signature: null
356+
};
357+
358+
await showDecodedJwt(null, null, null, ' ');
359+
const jwt = $(this).text();
360+
if (jwt) {
361+
try {
362+
const parts = jwt.split('.');
363+
if (parts.length === 3) {
364+
const header = JSON.parse(decodeBase64UrlSafe(parts[0]));
365+
const payload = JSON.parse(decodeBase64UrlSafe(parts[1]));
366+
const signature = parts[2];
367+
368+
decodedJwt = {
369+
header: header,
370+
payload: payload,
371+
signature: signature
372+
};
373+
await showDecodedJwt(parts, header, payload, signature);
374+
colorJwtInput($(this), parts);
375+
} else {
376+
showError('Invalid JWT format. A JWT should have three parts separated by dots.');
377+
}
378+
} catch (e) {
379+
showError('Error decoding JWT: ' + e.message);
380+
}
381+
} else {
382+
await showDecodedJwt(null, null, null, ' ');
383+
}
384+
}
385+
379386
function colorJwtInput(target, parts) {
380387
let html = '';
381388
if (parts.length > 0) {

0 commit comments

Comments
 (0)