Faking sessionStorage to keep sites from crashing
Tags: javascript, beginners, tutorial, localstorage
Published: 2024-05-10 08:15:49

---
title: Faking sessionStorage to keep sites from crashing
published: true
description: Yes, I disable cookies, localStorage, etc. And I'm honestly tired of having to enable it for random websites that have no business saving data on my PC. Here's how I got around that.
tags: javascript, beginner, tutorial, localstorage
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4voai517llisms87ihh.png



# Use a ratio of 100:42 for best results.
# published_at: 2023-11-30 07:54 +0000
---

## The Problem

So I have cookies disabled by default in my browser. The reason should be obvious: some sites just have no business saving data on my PC. Yes yes, cookies are cool for safety and performance stuff, but site owners couldn't handle their toy responsibly, so I'm taking it away.

Now, the problem is that the modern web isn't built for privacy-valuing users. Sites often use frameworks intended for highly interactive applications, even when all they do is display static content.

So I often end up seeing pages like this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhp37cozpqbtshlsoju1.png)

This is nextjs.org, by the way. Looking into the console, here's what happens:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5sdiinprn9dno0n9eax.png)

Cool. This site doesn't really "do" anything, I just want to read some text and ideally have some images in between. You know, a classic static website. And yet, it wants storage.

## The Solution

Okay, challenge accepted.

```js
Object.defineProperty(window, "sessionStorage", {
   get() { return 20 }
})

console.log(sessionStorage) // 20
```

Cool, looks like chromium lets me overwrite this property. And considering there's a flicker before the error appears, I bet I can inject a fix using an extension before the application even has a chance to fail.

```js
const fakeSessionStorage = {/* TODO: Implement */}
Object.defineProperty(window, "sessionStorage", {
   get() {
      return fakeSessionStorage
   }
}
```

Wait, but this is going to reset sessionStorage on every website, even if I enable cookies manually. *Ooooops*

Guess I'll have to wrap the whole snippet in some more code:

```js
try {
	window.sessionStorage
} catch {
	// inject custom sessionStorage
}
```

Looks good. What does the website do now?

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqbh33kdony0rdvtg2rz.png)

Perfect! Looks like I'm making progress. Now I face a different problem: I actually need to fake the `sessionStorage` object somehow.

What... what does that even do? [MDN to the rescue](https://developer.mozilla.org/en-US/docs/Web/API/Storage)!

It looks like the list of methods I'd have to fake is somewhat reasonable.

```js
Storage.key()
Storage.getItem()
Storage.setItem()
Storage.removeItem()
Storage.clear()
```

Honestly, this looks like a `Map`. The functions are just named differently, but that's about it. I just have to map (no pun intended) the methods to each other like this:

```js
getItem(key)        -> get(key)
setItem(key, value) -> set(key, value)
removeItem(key)     -> delete(key)
clear()             -> clear()
```

Then just re-implement the `key` method using Map's `keys` method. Sounds easy :)

## The Result

And after a bit of tinkering, here's the entirety of my resulting code:

```js
class FakeStorage {
   #map = new Map()
   
   getItem(key) { return this.#map.get(key) }
   setItem(key, value) { this.#map.set(key, value) }
   removeItem(key) { this.#map.delete(key) }
   clear() { this.#map.clear() }
   key(n) { return this.#map.keys()[n] || null }
}

const fakeLocalStorage = new FakeStorage()

try {
   window.sessionStorage
} catch {
   Object.defineProperty(window, "sessionStorage", {
      get() {
         return fakeLocalStorage
      }
   })
}
```

I inject this into the page using the "User JavaScript and CSS" chrome extension, but I'm sure most other extensions will work too.

And voilà:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30zfmeszddmodeif9c83.png)

A working next.js website without any pesky cookies. Would it have been so hard to build this fallback into their page in the first place? 😩

---

Note: I have not thoroughly tested my fake storage object. Don't just copy-paste this into your application without making sure it doesn't have any subtle bugs.