【问题标题】:Chrome App localStorage not persisting and chrome.storage not workingChrome App localStorage 不持久且 chrome.storage 不工作
【发布时间】:2016-02-28 18:49:41
【问题描述】:

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

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

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

代码示例:

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

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

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

我已尝试迁移到 chrome.storage,但每次尝试执行此操作时都会收到未定义的错误。作为 Chrome 应用程序和在浏览器中运行时会出现此错误

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

Chrome Management Settings


从 cmets 添加: 代码:

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.storage 的代码和确切的错误文本。
  • @wOxxOm chrome.storage.local.set({ 'key1': 'first', 'key2': 'second', 'key3': 'third', 'key4': 'fourth' , 'key5': 'fifth' }, function() { console.debug('设置保存'); });错误:TypeError:无法读取未定义的属性“本地”
  • 问题可能是通过 webview 从 Chrome 应用程序调用代码吗?代码: link-to-my-website-which-calls-localstorage.com" style="width:100% ; 高度:100%; 位置: 绝对; 上:0; 左:0; 右:0; 下:0;">

标签: javascript webview local-storage google-chrome-app


【解决方案1】:

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

  1. 应用程序的代码本身。 localStoragedisabled for Chrome App code。唯一的解决方案是使用chrome.storage API。

  2. (您的情况) localStorage&lt;webview&gt; 内。它被设计为临时的默认情况下。如果您希望它持续存在,您需要使用webview persistent partition

    如果存储分区 ID 以 persist: (partition="persist:googlepluswidgets") 开头,则 web 视图将使用一个持久存储分区,该持久存储分区可供应用程序中具有相同存储分区 ID 的所有访客使用。如果未设置 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>
    

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

【讨论】:

  • 我添加了 partition="persistent:browser" 但重新启动时 localStorage 仍然丢失。
  • @Spencer 这是我的错字,应该是persist,而不是persistent。阅读我链接的文档可以解决这个问题。
  • @Mr Pablo 当然会有所帮助,但正如他们所说,事后诸葛亮
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 2012-01-24
  • 2020-07-19
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多