D
D
Dmitry2210602018-08-26 18:09:37
JavaScript
Dmitry221060, 2018-08-26 18:09:37

How to access LocalStorage?

There is a site that, after loading, deletes the window.localStorage, copying it to a local variable. More or less like this:

(function () {
    let LS = window.localStorage;
    delete window.localStorage;
})();

I tried to solve the problem like this -
// ==UserScript==
// @name         Get LS
// @run-at       document-start
// @grant        window.localStorage
// ==/UserScript==

(function() {
    'use strict';
    const LS = window.localStorage;
    console.log(LS);
})();

But it works through time. Sometimes the site manages to remove localStorage, sometimes not.
Q: How can I be sure to copy local storage?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Proskurin, 2018-08-26
@Dmitry221060

You need to make sure that the script is executed immediately when the window is created, somewhere before the scripts start loading, at the very top.
There is also such a trick as creating an empty iframe and extracting the desired property from its global window object.

var iframe = document.createElement("iframe");
iframe .style.display = "none";
document.documentElement.appendChild(iframe);
var localStorage = iframe.contentWindow.localStorage;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question