localStorage和'文件:'协议不持久,SQLite给出SECURITY_ERR

localStorage and 'file:' protocol not persistent, SQLite gives SECURITY_ERR

本文关键字:给出 SQLite SECURITY ERR 协议 文件 localStorage 不持久      更新时间:2023-09-26

简介

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

我想为工具栏存储一些设置,可以使用localStorage或SQLite。我已经阅读了一些关于indexedDB的信息,尽管我还没有发现如何使用它的具体实现

本地存储的问题

当我停留在预览模式时,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.

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

使用以下解决方案:通过一些修改实现WebView数据库配额委托,我能够使其工作。

以下委派方法对我有效(放在您的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应用商店。

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