如何在关闭和打开Chrome扩展弹出菜单时丢失数据 [英] how not to lose data when closing and opening chrome extension popup

查看:155
本文介绍了如何在关闭和打开Chrome扩展弹出菜单时丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Chrome扩展程序。
我需要在处理弹出窗口时创建数据,在关闭并重新打开它后可用。



这里有关于我的具体问题的更多详细信息:



每当我的Chrome扩展弹出窗口打开时,脚本就会运行。在弹出窗口中有一个选项可以单击一个按钮,该按钮将向阵列中添加一个项目,该项目用于在此弹出窗口的另一个选项卡中显示此项目列表。但是,由于每次打开弹出窗口后代码都会重新运行,弹出窗口再次打开时显然会清空该数组,当浏览器或操作系统重新启动时显然会清空该数组。我需要这个数组通过打开和关闭操作系统,浏览器和弹出窗口来保持一致。任何想法?

btw - 从后台页面管理它不够好,因为os重新启动后,后台页面会话停止并且数据丢失

解决方案

当弹出窗口关闭时,其HTML文档完全卸载;每当打开每个弹出窗口时,都需要恢复状态。没有办法。



正如你所提到的,在浏览器运行时,可以在后台页面中保存状态信息。但它仍然是临时存储;您需要使用持久性存储来保存您需要保存的任何状态。



最明显的(推荐)选择是 chrome.storage API ,专为此目的而设计。如果你愿意的话,你也可以使用Web API,比如 localStorage IndexedDB


I'm new to chrome extension. I need to have data created when working with the popup, available after closing and re-opening it.

Here're some more details about my specific problem:

whenever my chrome extension popup is opened a script runs. in the popup there's an option to click a button which will add an item to an array, which is used in order to display this list of items in another tab in this popup. However, since every time the popup is opened the code runs all over again, the array is emptied the moment the popup opens again, and obviously when the browser or the OS is restarted. I need this array to stay consistent through opening and closing of the OS, the browser and the popup itself. Any ideas?

btw - managing it from a background page is not good enough since the moment the os is restarted the background page session stops and the data is lost

解决方案

When a popup is closed, its HTML document is completely unloaded; you need to restore state when your popup loads every time it's opened. No way around that.

As you mention, it's possible to persist state information in the background page while the browser is running. But it's still temporary storage; you need to use persistent storage to save whatever state you need to save.

The most obvious (and recommended) choice is chrome.storage API, specifically designed for this purpose. You can also use Web APIs such as localStorage or IndexedDB if you like.

这篇关于如何在关闭和打开Chrome扩展弹出菜单时丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆