requestStorageAccess Failure

934 Views Asked by At

When I ask for storage access on a page in my iframe, It returns "Undefined".

What does that signify? I was hoping for a True, and would have understood false. (it was denied) but what does Undefined mean in this context? What went wrong?

<button id="start-button">Start Puctto</button>

<script>

function gain_access() {
  document.cookie = "foo=bar";              // set a cookie
  document.hasStorageAccess().then(hasAccess => {
    if (!hasAccess) {     
      return document.requestStorageAccess();
    }
  }).then(_ => {   
    // Now we have first-party storage access!
    // Let's access some items from the first-party cookie jar
    document.cookie = "foo=bar";              // set a cookie
    localStorage.setItem("username", "John"); // access a localStorage entry
    window.opener.postMessage('set_session', '*');   
    window.location.href = document.referrer;
  }).catch(_ => {
    alert(_);
  });
}
  document.getElementById('start-button').addEventListener('click', function (){
    console.log('Start button clicked');                
    gain_access();

  })
</script>
1

There are 1 best solutions below

4
Yakko Majuri On

I don't believe anything went wrong. Have you checked to see if the value was set correctly?

localStorage.setItem doesn't return anything even if it works, so when that's printed to the console, it shows undefined i.e. it's trying to print the return value but there isn't one so the return value is undefined.

Don't rely on a return value from it to do something else, just kind of take it for granted.

If it is setting the value correctly to localStorage then it's all good.

EDIT: Also, apparently the concept of requesting storage access is still only at proposal stage, so maybe just remove that part all together.