Chrome应用程序本地存储不持久,Chrome.storage不工作

Chrome App localStorage not persisting and chrome.storage not working

本文关键字:Chrome storage 工作 存储 应用程序 不持久      更新时间:2023-09-26

我有一个chrome Kiosk应用程序,我需要在机器打开和关闭之间保存数据(几个字节作为字符串)。但无论我怎么尝试,localStorage似乎在重新启动时就被擦除了。

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

在浏览器的Chrome中,我会简单地使用localStorage,但在Kiosk应用程序中这种情况不会持续存在。

代码示例:

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

遵循此处的建议:跨单个应用程序kiosk模式执行持久化数据我有一个Chrome管理许可证,设置了以下设置,但这似乎没有任何区别(见附件JPG)

使用Kiosk应用程序,我在manifest.json 中拥有存储权限

我试过移动到chrome.storage,但当我尝试这样做时,我会遇到一个未定义的错误。当作为Chrome应用程序和浏览器运行时会发生此错误

我尝试过这里的解决方案,但都不起作用。总是得到一个未定义的错误:https://groups.google.com/a/chromium.org/forum/#!主题/铬应用程序/_YcOT4PcdAQ

Chrome管理设置


添加自注释:代码:

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>

Chrome应用程序中有两个localStorage上下文。

  1. 应用程序的代码本身。对于Chrome应用程序代码,localStorage已禁用。唯一的解决方案是使用chrome.storage API。

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

    如果存储分区ID以persist:partition="persist:googlepluswidgets")开头,则Web视图将使用可供应用程序中具有相同存储分区ID的所有来宾使用的持久存储分区。如果未设置ID或没有'persist:'前缀,则Web View将使用内存中存储分区。

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

    请注意,chrome.storage API不会暴露于webview内容(除非注入脚本)。