简体   繁体   English

关闭和打开chrome扩展弹出窗口时如何不丢失数据

[英]how not to lose data when closing and opening chrome extension popup

I'm new to chrome extension. 我是chrome扩展的新手。 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. 每当我的chrome扩展程序弹出窗口打开时,脚本就会运行。 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; 当弹出窗口关闭时,其HTML文档被完全卸载; 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. 最明显(和推荐)的选择是chrome.storage API ,专门为此目的而设计。 You can also use Web APIs such as localStorage or IndexedDB if you like. 如果您愿意,也可以使用诸如localStorageIndexedDB类的Web API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM