Edit existing localStorage Item on blob url

Viewed 55

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

The item comes from another page and is replaced on the blob page, but when you try to replace the item, you end up creating a new one accessible only within the blob page. Unable to access from browser console. Is it possible to edit the "external" item in a blob page?

//create a new item in localStorage
localStorage.setItem("test", "not working")
//create a blob url
function newBlob(text){
  var blob = new Blob(["\ufeff", text], {type: "text/html"})
  var url = window.URL.createObjectURL(blob)
  return url
}
//give a content to the blob and return the url
window.location.href = newBlob(`<button onclick="localStorage.setItem('test', 'working')">Click here to replace the item</button>`)
//the button is here to replace the item but is actually creating a new one that can only be accessed within the blob

1 Answers

You can update an existing localStorage item by doing this.

// Get the existing data
var existing = localStorage.getItem("example");

// If no existing data, use the value by itself
// Otherwise, add the new value to it
var data = existing ? "updatedData" : "updatedData";

// Save back to localStorage
localStorage.setItem("example", data);
Related