Search code examples
javascriptgoogle-chrome-devtoolsdevtools

How to access localStorage information with javascript and devtool


I'm logged into the discord.com page, I would like to know what javascript code I should run to access my email that is in local storage, I tried to run

localStorage.getItem("email_cache") 

but I get the error:

Uncaught ReferenceError: localStorage is not defined

But if I open devtools in the applications tab I see my email in localStorage, how can I access it with javascript?


Solution

  • Confirmed, as @Barmar suggested, they are renaming the localStorage object inside a closure and then deleting the global reference to it, for security reasons obviously.

    This is done in a script called /assets/89354aa0051558f7225a.js which is loaded near the end of the <head> section.

    var r;
    try {
      r = window.localStorage
    } catch (e) {}
    try {
      delete window.localStorage
    } catch (e) {}
    

    The solution is to inject a script at the top of the <head> (before any other scripts), to save a reference to the localStorage object to another name so that it can be used later. Since you didn't specify how you're using Selenium I can't give further advise on that, but there are proxies and other ways to get that done.

    Side note: I've written and used this function in the browser console to search the Discord source code.