Skip to content

Commit

Permalink
feat: check if localStorage is allowed (SecurityError)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayhanadev committed Jun 13, 2021
1 parent 1147db9 commit 5f98dae
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion offline/index.html

Large diffs are not rendered by default.

32 changes: 24 additions & 8 deletions source/game.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
document.body.classList.add('game');

var storage = (function() {
var uid = new Date;
var storage;
var result;
try {
(storage = window.localStorage).setItem(uid, uid);
result = storage.getItem(uid) == uid;
storage.removeItem(uid);
return result && storage;
} catch (exception) {}
storage = function() { console.log('localStorage Disabled.') };
storage.getItem = function() { console.log('localStorage Disabled.') };
storage.setItem = function() { console.log('localStorage Disabled.') };
return storage;
}());

var DEBUG = window.location.hash === '#DEBUG',
INFO = DEBUG || window.location.hash === '#INFO',
game,
Expand All @@ -15,13 +31,13 @@ var DEBUG = window.location.hash === '#DEBUG',
playButtonProximityThreshold = 30,
maximumPossibleDistanceBetweenTwoMasses,
highScoreCookieKey = 'tetherHighScore',
highScore = localStorage.getItem(highScoreCookieKey) ?? 0,
highScore = storage.getItem(highScoreCookieKey) ?? 0,
musicMutedCookieKey = 'tetherMusicMuted',
lastDayCookieKey = 'tetherLastDate',
streakCountCookieKey = 'tetherStreakCount',
streakCount = localStorage.getItem(streakCountCookieKey) ?? 0,
streakCount = storage.getItem(streakCountCookieKey) ?? 0,
subtitleText = "",
lastDate = new Date(Number(localStorage.getItem(lastDayCookieKey))),
lastDate = new Date(Number(storage.getItem(lastDayCookieKey))),
lastTouchStart,
uidCookieKey = 'tetherId',
uid,
Expand Down Expand Up @@ -399,10 +415,10 @@ function initCanvas() {
var later48Hours = lastDate.getTime() + 2 * 86400000;
var currentDate = new Date();

var streak = Number(localStorage.getItem(streakCountCookieKey));
var streak = Number(storage.getItem(streakCountCookieKey));

if (
!Number(localStorage.getItem(lastDayCookieKey)) ||
!Number(storage.getItem(lastDayCookieKey)) ||
Number.isNaN(lastDate)
) {
saveCookie(lastDayCookieKey, currentDate.getTime());
Expand Down Expand Up @@ -473,8 +489,8 @@ function initCanvas() {
document.exitPointerLock =
document.exitPointerLock || document.mozExitPointerLock;

for (var key in localStorage) {
var value = localStorage.getItem(key);
for (var key in storage) {
var value = storage.getItem(key);
if (
achievements[key] ||
key === musicMutedCookieKey ||
Expand Down Expand Up @@ -1565,7 +1581,7 @@ function autoWave(difficulty) {
}

function saveCookie(key, value) {
localStorage.setItem(key, value);
storage.setItem(key, value);
document.cookie = key + '=' + value + cookieSuffix;
}

Expand Down
2 changes: 1 addition & 1 deletion source/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<meta content="tether!" name="title" />
<meta content="A game about swinging a ball around and sheer destruction." name="description" />
<meta content="game, fun, mobile, ball, videogame, online-game" name="keywords" />
<meta content="tether, tether!, rayhanadev, game, fun, mobile, ball, videogame, online-game" name="keywords" />
<meta content="index, follow" name="robots" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="English" name="language" />
Expand Down

1 comment on commit 5f98dae

@vercel
Copy link

@vercel vercel bot commented on 5f98dae Jun 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.