How to edit an existing localStorage item in a blob url

Asked

Viewed 285 times

0

How to edit an existing item in localStorage on an HTML blob page?

The item comes from another page and is replaced on the blob page, but when you try to replace it, you end up creating a new one accessible only inside the blob page, it is not possible to access it outside the blob page, not even by the browser console, I searched on this and found nothing informing this limitation of the blob.

You can edit the item outside the blob page with the same domain?

Example link in codepen: https://codepen.io/markvaaz/pen/LYYZgYE

//cria um item no localStorage (Ainda em um pagina comum)
localStorage.setItem("test", "not working")
//cria uma url blob
function newBlob(text){
  var blob = new Blob(["\ufeff", text], {type: "text/html"})
  var url = window.URL.createObjectURL(blob)
  return url
}
//da um conteúdo para o blob e entra na url blob retornada
window.location.href = newBlob(`<button onclick="localStorage.setItem('test', 'working')">Click here to replace the item</button>`)
//a função do botão é substituir o item que foi criado anteriormente com um novo conteúdo, porém acaba criando um novo item acessivel apenas dentro da pagina blob, não é possivel acessar o item no console do navegador.

1 answer

1

When your script starts to be interpreted it saves a value in the localStorage, builds the function newBlob and exchange the href passing the function result newBlob.

By replacing the href It’s like you switched addresses, so assuming your local address (a local file .html) be it C:/meudiretorio/arquivo.xml by replacing the href passing the Object URL, your browser will change the address to for example blob:null/9feeeee0-bba8-4f70-b63f-4b3d015a5996.

Changing the origin of the document localStorage will be different and for this reason will not work. Then considering your question, in these conditions the answer will be: it is not possible.

  • even if the blob is in the same domain as a normal page? as for example in codepen the domain https://cdpn.io/... next blob:https://cdpn.io/cd18b45a-84ea-4a2b-9cad-3bb488d0f28f

  • @Markvaaz in codepen the origin of the document does not change. Every time you change the code it changes only the end of the src iframe: https://cdpn.io/boomboom/v2/index.html?key=iFrameKey-XXXXX the source address of the document remains unchanged. In this case, as the origin is the same, localStorage works using its logic. Every time the source is changed the localStorage will have a new instance associated with the origin.

  • In this case, it does not work in codepen, and actually the iframe src does not change, but as you pass a new address it becomes the new iframe address.

  • @Markvaaz just opened your link in codepen and is working correctly. By clicking the value working inside the blob is displayed on body.innerHTML. If you remove the section localStorage.setItem('test', 'working inside the blob '); "not Working" will appear. Note that localStorage is the same before and after the click since the origin of the document has not changed. If you can, detail a little more please your question if it has not been clear

  • @Markvaaz only complementing, the origin of the src iframe keeps. Englobe your script with a setTimeout of a 2s and right in the first line outside of setTimeout printe the address. You will see something like: https://cdpn.io/boomboom/v2/index.html?key=iFrameKey-edca7c65-9f25-598b-56cf-f3ea718d9a4e and after 2s blob:https://cdpn.io/c42af343-2b6b-4679-996e-78c71a97c2d3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.