Chrome App localStorage无法持久存在且chrome.storage无法正常工作

时间:2020-12-28 16:52:22

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.

我有一个镀铬自助服务终端应用程序,我需要在机器开启和关闭之间保存数据(几个字节作为字符串)。但是,无论我尝试什么,localStorage似乎在重新启动时被删除。

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

当我转到chrome:// inspect / #apps来检查Chrome应用程序时,控制台中没有与LocalStorage相关的错误

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

在浏览器的Chrome中,我只会使用localStorage,但在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)

遵循以下建议:在单个应用程序自助终端模式执行中保留数据我有一个Chrome管理许可证,其中设置了以下设置,但这似乎没有任何区别(请参阅附件JPG)

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

使用Kiosk App,我在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

我尝试过移动到chrome.storage,但是当我尝试执行此操作时出现未定义的错误。运行Chrome应用程序和浏览器时出现此错误

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

我在这里尝试了解决方案,但它们不起作用。始终收到未定义的错误:https://groups.google.com/a/chromium.org/forum/#!topic / chrome-apps / _YcOT4PcdAQ

Chrome Management Settings

Chrome管理设置


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 个解决方案

#1


7  

There are two contexts for localStorage in a Chrome App.

Chrome应用中有两种localStorage上下文。

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

    应用程序的代码本身。 Chrome应用代码已停用localStorage。唯一的解决方案是使用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.

    (您的案例) 中的localStorage。它默认设计为临时的。如果您希望它持久化,则需要使用webview持久分区。

    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.

    如果存储分区ID以persist :( partition =“persist:googlepluswidgets”)开头,则webview将使用可用于应用程序中具有相同存储分区ID的所有guest虚拟机的持久存储分区。如果未设置ID或者没有'persist:'前缀,则webview将使用内存存储分区。

    <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).

    请注意,chrome.storage API不会公开给webview内容(除非您注入脚本)。

#1


7  

There are two contexts for localStorage in a Chrome App.

Chrome应用中有两种localStorage上下文。

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

    应用程序的代码本身。 Chrome应用代码已停用localStorage。唯一的解决方案是使用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.

    (您的案例) 中的localStorage。它默认设计为临时的。如果您希望它持久化,则需要使用webview持久分区。

    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.

    如果存储分区ID以persist :( partition =“persist:googlepluswidgets”)开头,则webview将使用可用于应用程序中具有相同存储分区ID的所有guest虚拟机的持久存储分区。如果未设置ID或者没有'persist:'前缀,则webview将使用内存存储分区。

    <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).

    请注意,chrome.storage API不会公开给webview内容(除非您注入脚本)。