gpt4 book ai didi

javascript - 编辑 blob url 上的现有 localStorage 项

转载 作者:行者123 更新时间:2023-12-03 16:47:31 25 4
gpt4 key购买 nike

如何在 blob HTML 页面中编辑现有的 localStorage 项?

该项目来自另一个页面并在 blob 页面上被替换,但是当您尝试替换该项目时,您最终会创建一个只能在 blob 页面内访问的新项目。无法从浏览器控制台访问。 是否可以在 blob 页面中编辑“外部”项?

//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

最佳答案

您可以通过执行此操作更新现有的 localStorage 项目。

// 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);

关于javascript - 编辑 blob url 上的现有 localStorage 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58420403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com