最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how not to lose data when closing and opening chrome extension popup - Stack Overflow

programmeradmin2浏览0评论

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

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

Share asked Mar 29, 2015 at 22:41 AmnonAmnon 3254 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

When a popup is closed, its HTML document is pletely 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 remended) choice is chrome.storage API, specifically designed for this purpose. You can also use Web APIs such as localStorage or IndexedDB if you like.

发布评论

评论列表(0)

  1. 暂无评论