【问题标题】:localStorage and 'file:' protocol not persistent, SQLite gives SECURITY_ERRlocalStorage 和 'file:' 协议不持久,SQLite 提供 SECURITY_ERR
【发布时间】:2011-11-24 08:23:38
【问题描述】:

简介

我使用 RapidWeaver(Mac OS X CMS 应用程序),它不使用服务器环境。它有一个编辑器和一个预览模式。预览模式是基于 Webkit 的渲染器,我可以使用“检查元素”,就像您通常在 Safari 中所做的那样。

我想使用 localStorage 或 SQLite 存储工具栏的一些设置。我已经阅读了一些关于 indexedDB 的信息,但我没有找到关于如何使用它的具体实现。

localStorage 的问题

当我处于预览模式时,localStorage 工作正常,当我在编辑器和预览模式之间切换时,url - location.href - 略有改变:

file:///private/var/folders/s7/x8y2s0sd27z6kdt2jjdw7c_c0000gn/T/TemporaryItems/RapidWeaver/98970/document-143873968-28/RWDocumentPagePreview/code/styled/index.html

file:///private/var/folders/s7/x8y2s0sd27z6kdt2jjdw7c_c0000gn/T/TemporaryItems/RapidWeaver/98970/document-143873968-29/RWDocumentPagePreview/code/styled/index.html

document-143873968-28 更改为 文档 143873968-29

我所读到的关于 localStorage 的内容,基本上是用于 FireFox 的 globalStorage[location.hostname]。据我所知,Safari 不支持 globalStorage,所以我无法尝试。

SQLite 的问题

当我尝试打开数据库时:

var shortName = 'mydatabase';
var version = '1.0';
var displayName = 'My Important Database';
var maxSize = 65536; // in bytes
var db = openDatabase(shortName, version, displayName, maxSize);

我在控制台中得到了这个:

SECURITY_ERR: DOM Exception 18: An attempt was made to break through the security policy of the user agent.

这基本上结束了我的问题,我将真诚地感谢任何答案或cmets。

【问题讨论】:

    标签: javascript sqlite webkit local local-storage


    【解决方案1】:

    使用以下解决方案:Implementing a WebView database quota delegate 进行了一些修改,我能够让它工作。

    以下委托方法对我有用(放在您的 webViewDelegate 中):

    - (void)webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(id) origin database:(NSString *)databaseIdentifier
    {
      static const unsigned long long defaultQuota = 5 * 1024 * 1024;
      if ([origin respondsToSelector: @selector(setQuota:)]) {
        [origin performSelector:@selector(setQuota:) withObject:[NSNumber numberWithLongLong: defaultQuota]];
      } else { 
        NSLog(@"could not increase quota for %@", defaultQuota); 
      }
    }
    

    默认情况下,数据库被赋予 0 字节,这会导致您在上面得到模糊的错误消息。在没有足够空间时尝试创建数据库后调用上述方法。请注意,此方法在 WebUIDelegatePrivate.h (http://opensource.apple.com/source/WebKit/WebKit-7533.16/mac/WebView/WebUIDelegatePrivate.h) 中定义,使用可能会阻止您将应用提交到 mac 应用商店。

    【讨论】:

      【解决方案2】:

      localStorage 是一种 html5 机制,可以为脚本提供比 cookie 更多的空间。 Safari 支持:https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Name-ValueStorage/Name-ValueStorage.html

      我不知道对于基于 file:/// 的应用程序应该有什么路径限制(如果有的话)。

      编辑:进一步研究路径限制,我发现您应该使用 Safari,FF 最近修复了一个错误,使其无法在那里工作:https://bugzilla.mozilla.org/show%5Fbug.cgi?id=507361

      【讨论】:

      • 它对我不起作用,我也使用 MAMP 在 localhost 上测试了 localStorage,但没有成功。
      猜你喜欢
      • 1970-01-01
      • 2012-07-09
      • 2012-01-14
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 2022-01-11
      相关资源
      最近更新 更多