Chrome 应用程序 localStorage 不持久并且 chrome.storage 不工作

Chrome App localStorage not persisting and chrome.storage not working

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

当我转到 chrome://inspect/#apps 检查 Chrome 应用程序时,控制台中没有关于 LocalStorage

的相关错误

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

代码示例:

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

遵循此处的建议: 我有一个 Chrome 管理许可证,设置了以下设置,但这似乎没有任何区别(见附件 JPG)

使用 Kiosk 应用程序,我在 manifest.json

中拥有存储权限

我曾尝试移动到 chrome.storage,但每当我尝试执行 this.This 时出现未定义错误 运行 作为 Chrome 应用程序并在浏览器

我已经尝试过这里的解决方案,但它们不起作用。总是得到一个未定义的错误: https://groups.google.com/a/chromium.org/forum/#!topic/chromium-apps/_YcOT4PcdAQ

Chrome Management Settings


从评论中添加: 代码:

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. 应用的代码本身。 localStoragedisabled for Chrome App code。唯一的解决办法是使用 chrome.storage API.

  2. (你的情况) localStorage 里面有一个 <webview>。它被设计为临时 默认 。如果你希望它持续存在,你需要使用 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>
    

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