Skip to content

Commit 92429ce

Browse files
committed
Protect all calls to localStorage.setItem
1 parent 37a0a62 commit 92429ce

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

js/app.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ if (parseSearch().value || parseHash().id_token) {
3131

3232
}
3333

34-
try {
35-
localStorage.setItem("visited", "1");
36-
} catch (e) {
37-
// Safari when in private browsing doesn't allow it
34+
function safeLocalStorageSetItem(key, value) {
35+
try {
36+
localStorage.setItem(key, value);
37+
} catch (e) {
38+
// Safari when in private browsing doesn't allow it
39+
}
3840
}
41+
safeLocalStorageSetItem("visited", "1");
3942

4043
/*
4144
* Show menu mobile
@@ -441,7 +444,7 @@ FaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n\
441444

442445
function saveToStorage(jwt) {
443446
// Save last valid jwt value for refresh
444-
localStorage.setItem("jwtValue", jwt);
447+
safeLocalStorageSetItem("jwtValue", jwt);
445448

446449

447450
}
@@ -728,15 +731,16 @@ $('.stars').each(function(idx, element){
728731
}
729732

730733
if (repo){
731-
if(!localStorage["stars_" + repo]) {
734+
var repoKey = "stars_" + repo;
735+
if(!localStorage.getItem(repoKey)) {
732736

733737
$.getJSON('https://api.github.com/repos/' + repo, function(repoData){
734-
localStorage.setItem("stars_" + repo, repoData.stargazers_count);
735-
736-
setCount(localStorage["stars_" + repo]);
738+
var starCount = repoData.stargazers_count;
739+
safeLocalStorageSetItem(repoKey, starCount);
740+
setCount(starCount);
737741
});
738742
} else {
739-
setCount(localStorage["stars_" + repo]);
743+
setCount(localStorage.getItem(repoKey));
740744
}
741745
}
742746
});

0 commit comments

Comments
 (0)