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

大憨熊 提交于 2019-12-20 02:36:17

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/29335806/how-not-to-lose-data-when-closing-and-opening-chrome-extension-popup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!