问题描述
我有一个chrome Kiosk应用程序,我需要在打开和关闭机器之间保存数据(作为字符串的几个字节).但是我尝试过的一切,在重新启动后似乎会擦除localStorage.
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.
当我去chrome://inspect/#apps检查Chrome应用时,控制台中没有与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浏览器中,我只会使用localStorage,但是在Kiosk应用程序中这种情况不会持久.
Within Chrome in a browser, I would simply use localStorage but this does not persist when in a Kiosk App.
代码示例:
window.localStorage.setItem(id, temp);
window.localStorage.getItem(id);
按照此处的建议进行操作:在单个应用程序信息亭模式执行过程中持久存储数据我有一个设置了以下设置的Chrome管理许可,但这似乎没有什么区别(请参阅附件JPG)
Following the advice here:Persist data across single app kiosk mode executionsI have a Chrome Management Licence with the following settings set but this does not seem to have made any difference (see attached JPG)
使用Kiosk App,我在manifest.json中拥有存储权限
With the Kiosk App, I have the storage permission in the manifest.json
我曾经尝试过使用chrome.storage,但每次尝试都遇到未定义的错误.当以Chrome应用和浏览器运行时,会发生此错误
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
我在这里尝试过解决方案,但是它们不起作用.总是得到一个未定义的错误: https://groups.google.com/a/chromium.org/forum/#!topic/chromium-apps/_YcOT4PcdAQ
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.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
有两个上下文.
There are two contexts for localStorage
in a Chrome App.
-
应用程序的代码本身.
localStorage
已 已禁用用于Chrome应用程序代码.唯一的解决方案是使用chrome.storage
API.
The app's code itself.
localStorage
is disabled for Chrome App code. The only solution is to usechrome.storage
API.
(您的情况) localStorage
在< webview>
内.默认情况下,它被设计为临时 .如果您希望它持久存在,则需要使用 webview持久分区.
(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.
<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内容(除非您注入脚本).
Do note that chrome.storage
API will not be exposed to webview content (unless you inject a script).
这篇关于Chrome应用程式localStorage无法持续运作,而chrome.storage无法运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!