Chrome App localStorage not persisting and chrome.storage not working

妖精的绣舞 提交于 2021-02-07 13:17:53

问题


I have a chrome Kiosk App which I need to save data (a few bytes as a string) between the machine turning on and off. But what ever I try, the localStorage seems to be wiped on restart.

When I go to chrome://inspect/#apps to inspect the Chrome App, there are no related errors in the console regarding to LocalStorage

Within Chrome in a browser, I would simply use localStorage but this does not persist when in a Kiosk App.

Example of code:

window.localStorage.setItem(id, temp);
window.localStorage.getItem(id);

Following the advice here: Persist data across single app kiosk mode executions I have a Chrome Management Licence with the following settings set but this does not seem to have made any difference (see attached JPG)

With the Kiosk App, I have the storage permission in the manifest.json

I have tried moving to chrome.storage but I get an undefined error when ever I try and do this.This error occurs when running as a Chrome App and in the browser

I have tried the solutions here but they don't work. Always get an undefined error: https://groups.google.com/a/chromium.org/forum/#!topic/chromium-apps/_YcOT4PcdAQ

Chrome Management Settings


Added from comments: CODE:

chrome.storage.local.set({ 'key1': 'first', 'key2': 'second', 'key3': 'third', 'key4': 'fourth', 'key5': 'fifth' }, function() { console.debug('Settings saved'); });

<body class="trim full">
  <form id="kiosk" model="AppConfig" view="KioskView">
    <webview id="browser" src="link-to-my-website-which-calls-localstorage.com" style="width:100%; height:100%; position:absolute; top:0; left:0; right:0; bottom:0;"></webview>
  </form>
</body>

回答1:


There are two contexts for localStorage in a Chrome App.

  1. The app's code itself. localStorage is disabled for Chrome App code. The only solution is to use chrome.storage API.

  2. (Your case) localStorage inside a <webview>. It's designed to be temporary by default. If you wish for it to persist, you need to use a webview persistent partition.

    If the storage partition ID starts with persist: (partition="persist:googlepluswidgets"), the webview will use a persistent storage partition available to all guests in the app with the same storage partition ID. If the ID is unset or if there is no 'persist:' prefix, the webview will use an in-memory storage partition.

    <webview id="browser"
             src="link-to-my-website-which-calls-localstorage.com"
             style="width:100%; height:100%; position:absolute; top:0; left:0; right:0; bottom:0;"
             partition="persist:browser">
    </webview>
    

    Do note that chrome.storage API will not be exposed to webview content (unless you inject a script).



来源:https://stackoverflow.com/questions/33925681/chrome-app-localstorage-not-persisting-and-chrome-storage-not-working

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