问题描述
我一直在开发 Chrome 扩展程序,它从网页中获取一个元素,使用 chrome.storage 存储该值,然后在弹出窗口中显示它。这工作正常,但从用户的角度考虑,我希望在用户关闭浏览器时删除数据,以便他们每次都“重新开始”。研究这个,我被指出使用 sessionStorage 的方向。我认为切换到它会很简单,但我遇到了一些问题。
这里是我使用 chrome.storage.local 设置变量的地方。这是在收到来自内容脚本的消息后在 background.js 中设置的:
chrome.storage.local.set({'amazon': 'test data'});
这是我访问 popup.js 中的变量并更新 popup.html 中的文本的地方:
let text = document.getElementById("site-data");
chrome.storage.local.get(['amazon'],function(result) {
let amazon_data = result['amazon'];
text.textContent = amazon_data;
});
这很好用,但就像我说的,sessionStorage 似乎更适合我的情况。所以我尝试将 background.js 中的代码替换为,
sessionStorage.setItem('amazon','test data');
并使用以下命令获取 popup.js 中的变量:
let text = document.getElementById("site-data");
let amazon_data = sessionStorage.getItem('amazon');
text.textContent = amazon_data;
我看过 sessionStorage 文档,它看起来应该很容易使用。我对如何实施它的理解中是否缺少某些东西?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)