Skip to content

Commit 4238932

Browse files
committed
Fix last token loading/saving.
1 parent 17e29da commit 4238932

File tree

6 files changed

+45
-56
lines changed

6 files changed

+45
-56
lines changed

src/editor/index.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { copyTextToClipboard, deferToNextLoop } from '../utils.js';
1+
import { copyTextToClipboard, deferToNextLoop, safeLocalStorageSetItem } from '../utils.js';
22
import { downloadPublicKeyIfPossible } from './public-key-download.js';
33
import { tooltipHandler } from './tooltip.js';
44
import { tokenEditor, headerEditor, payloadEditor } from './instances.js';
@@ -178,6 +178,32 @@ function markAsInvalidWithElement(element, clearTokenEditor = true) {
178178
}
179179
}
180180

181+
function saveAsLastToken() {
182+
const token = getTrimmedValue(tokenEditor);
183+
if(token && token.length > 0) {
184+
safeLocalStorageSetItem('lastToken', token);
185+
}
186+
187+
const publicKey = publicKeyTextArea.value;
188+
if(publicKey && publicKey.length > 0) {
189+
safeLocalStorageSetItem('lastPublicKey', publicKey);
190+
}
191+
}
192+
193+
function loadToken() {
194+
const lastToken = localStorage.getItem('lastToken');
195+
if(lastToken) {
196+
setTokenEditorValue(lastToken);
197+
198+
const lastPublicKey = localStorage.getItem('lastPublicKey');
199+
if(lastPublicKey) {
200+
publicKeyTextArea.value = lastPublicKey;
201+
}
202+
} else {
203+
useDefaultToken('HS256');
204+
}
205+
}
206+
181207
function encodeToken() {
182208
deferToNextLoop(fixEditorHeight);
183209

@@ -211,8 +237,10 @@ function encodeToken() {
211237
secretInput.value :
212238
privateKeyTextArea.value,
213239
secretBase64Checkbox.checked);
214-
240+
215241
tokenEditor.setValue(encoded);
242+
243+
saveAsLastToken();
216244
} catch(e) {
217245
console.error('Failed to sign/encode token: ', e);
218246
markAsInvalid();
@@ -247,6 +275,7 @@ function decodeToken() {
247275
if(decoded.errors) {
248276
markAsInvalidWithElement(editorElement, false);
249277
} else {
278+
saveAsLastToken();
250279
verifyToken();
251280
}
252281
} catch(e) {
@@ -339,6 +368,6 @@ export function setTokenEditorValue(value) {
339368
export function setupTokenEditor() {
340369
setupEvents();
341370
selectAlgorithm('HS256');
342-
useDefaultToken('HS256');
371+
loadToken();
343372
fixEditorHeight();
344373
}

src/extension/index.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
1-
import {
2-
setupTokenEditor,
3-
setTokenEditorValue,
4-
useDefaultToken
5-
} from '../editor';
6-
import { publicKeyTextArea } from './dom-elements.js';
7-
8-
/* For initialization, look at the end of this file */
9-
10-
function loadToken() {
11-
const lastToken = localStorage.getItem('lastToken');
12-
if(lastToken) {
13-
setTokenEditorValue(value);
14-
15-
const lastPublicKey = localStorage.getItem('lastPublicKey');
16-
if(lastPublicKey) {
17-
publicKeyTextArea.value = lastPublicKey;
18-
}
19-
} else {
20-
useDefaultToken('HS256');
21-
}
22-
}
1+
import { setupTokenEditor } from '../editor';
232

243
// Initialization
254
setupTokenEditor();
26-
loadToken();

src/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,13 @@ export function getParameterByName(name, url) {
183183
export function isWideScreen() {
184184
return window.matchMedia('(min-width: 768px)').matches;
185185
}
186+
187+
export function safeLocalStorageSetItem(key, value) {
188+
try {
189+
localStorage.setItem(key, value);
190+
} catch (e) {
191+
console.log('Cannot save token to Local Storage ' +
192+
'(private browsing enabled?), ignoring...', e);
193+
// Safari when in private browsing doesn't allow it
194+
}
195+
}

src/website/index.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { setupNavbar } from './navbar.js';
22
import { setupExtensionButton } from './extension.js';
33
import { setupLibraries } from './libraries.js';
4-
import {
5-
setupTokenEditor,
6-
setTokenEditorValue,
7-
useDefaultToken
8-
} from '../editor';
4+
import { setupTokenEditor, setTokenEditorValue } from '../editor';
95
import { setupJwtCounter } from './counter.js';
106
import { setupSmoothScrolling } from './smooth-scrolling.js';
117
import { setupHighlighting } from './highlighting.js';
@@ -44,20 +40,6 @@ function parseLocationQuery() {
4440
}
4541
}
4642

47-
function loadToken() {
48-
const lastToken = localStorage.getItem('lastToken');
49-
if(lastToken) {
50-
setTokenEditorValue(value);
51-
52-
const lastPublicKey = localStorage.getItem('lastPublicKey');
53-
if(lastPublicKey) {
54-
publicKeyTextArea.value = lastPublicKey;
55-
}
56-
} else {
57-
useDefaultToken('HS256');
58-
}
59-
}
60-
6143
function pickEbookOrExtensionBanner() {
6244
if(Math.random() >= 0.5) {
6345
extensionSection.style.display = 'block';
@@ -72,7 +54,6 @@ setupExtensionButton();
7254
setupSmoothScrolling();
7355
setupLibraries();
7456
setupTokenEditor();
75-
loadToken();
7657
parseLocationQuery();
7758
setupHighlighting();
7859
setupJwtCounter();

src/website/libraries.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { safeLocalStorageSetItem } from './utils.js';
1+
import { safeLocalStorageSetItem } from '../utils.js';
22
import { httpGet } from '../utils.js';
33
import {
44
starsElements,

src/website/utils.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
export function safeLocalStorageSetItem(key, value) {
2-
try {
3-
localStorage.setItem(key, value);
4-
} catch (e) {
5-
console.error(e);
6-
// Safari when in private browsing doesn't allow it
7-
}
8-
}
9-
101
export function getOffsetBoundingClientRect(element) {
112
const rect = element.getBoundingClientRect();
123

0 commit comments

Comments
 (0)