Safari ITP 2.0 存储访问 API - 在 hasStorageAccess 中嵌套 requestStorageAccess 时出现问题 - 非嵌套工作
Safari ITP 2.0 Storage Access API - Trouble Nesting requestStorageAccess in hasStorageAccess - Non Nested Works
我目前正在尝试实现调用存储访问 API,但在 hasStorageAccess 中嵌套对 requestStorageAccess 的调用时遇到问题。
这是代码的概要 - 它相当标准:
requestStorageAccessAndServe() {
let thisObject = this;
var promise = document.hasStorageAccess();
promise.then(
function (hasCookieAccess) {
if (!hasCookieAccess) {
document.requestStorageAccess().then(
function successful() {
// reload iframe to run with cookie access
window.location.reload();
},
function fail() {
thisObject.serveContent(); // Code goes into here
});
} else {
thisObject.serveContent();
}
},
function (reason) {
thisObject.serveContent();
}
);
}
当点击按钮触发这个方法时,我总是陷入那个"fail"功能,没有出现请求存储访问的提示。
令人惊讶的是,这段非嵌套代码运行完美:
requestStorageAccessAndServe() {
let thisObject = this;
let hasCookieAccess = !!document.cookie;
if (!hasCookieAccess) {
document.requestStorageAccess().then(
function successful() {
window.location.reload();
},
function fail() {
thisObject.serveContent();
});
} else {
thisObject.serveContent();
}
}
此代码有效 - 它在第一个请求时重新加载 iframe,然后在另一个请求重新加载后提供数据,但是通过 !!document.cookie 检查 cookie 访问是非常 hacky(如果有首先没有 cookie 数据),我更想了解这里出了什么问题。有人知道吗?
对于一个可能的解决方案,有什么方法可以强制解析 document.hasStorageAccess() 这样我就不需要嵌套它了吗?
编辑:
强制承诺解决也无济于事。参见代码示例:
async requestStorageAccessAndServe() {
let thisObject = this;
let hasCookieAccess = await document.hasStorageAccess();
if (!hasCookieAccess) {
document.requestStorageAccess().then(
function successful() {
window.location.reload();
},
function fail() {
thisObject.serveContent();
});
} else {
thisObject.serveContent();
}
}
仍然进入 "fail" 功能...
这里的问题是 requestStorageAccess() 需要调用用户意图。通过将其嵌套在 hasStorageAccess() promise 中,用户意图(点击)被隐藏,Safari 自动拒绝请求。
为了解决这个问题,我在 iframe 加载时解析 hasStorageAccess(因为它不需要用户意图),将此结果存储在 class 变量中,然后如果它解析为 false,我在点击时检查 requestStorageAccess .
我目前正在尝试实现调用存储访问 API,但在 hasStorageAccess 中嵌套对 requestStorageAccess 的调用时遇到问题。
这是代码的概要 - 它相当标准:
requestStorageAccessAndServe() {
let thisObject = this;
var promise = document.hasStorageAccess();
promise.then(
function (hasCookieAccess) {
if (!hasCookieAccess) {
document.requestStorageAccess().then(
function successful() {
// reload iframe to run with cookie access
window.location.reload();
},
function fail() {
thisObject.serveContent(); // Code goes into here
});
} else {
thisObject.serveContent();
}
},
function (reason) {
thisObject.serveContent();
}
);
}
当点击按钮触发这个方法时,我总是陷入那个"fail"功能,没有出现请求存储访问的提示。
令人惊讶的是,这段非嵌套代码运行完美:
requestStorageAccessAndServe() {
let thisObject = this;
let hasCookieAccess = !!document.cookie;
if (!hasCookieAccess) {
document.requestStorageAccess().then(
function successful() {
window.location.reload();
},
function fail() {
thisObject.serveContent();
});
} else {
thisObject.serveContent();
}
}
此代码有效 - 它在第一个请求时重新加载 iframe,然后在另一个请求重新加载后提供数据,但是通过 !!document.cookie 检查 cookie 访问是非常 hacky(如果有首先没有 cookie 数据),我更想了解这里出了什么问题。有人知道吗?
对于一个可能的解决方案,有什么方法可以强制解析 document.hasStorageAccess() 这样我就不需要嵌套它了吗?
编辑:
强制承诺解决也无济于事。参见代码示例:
async requestStorageAccessAndServe() {
let thisObject = this;
let hasCookieAccess = await document.hasStorageAccess();
if (!hasCookieAccess) {
document.requestStorageAccess().then(
function successful() {
window.location.reload();
},
function fail() {
thisObject.serveContent();
});
} else {
thisObject.serveContent();
}
}
仍然进入 "fail" 功能...
这里的问题是 requestStorageAccess() 需要调用用户意图。通过将其嵌套在 hasStorageAccess() promise 中,用户意图(点击)被隐藏,Safari 自动拒绝请求。
为了解决这个问题,我在 iframe 加载时解析 hasStorageAccess(因为它不需要用户意图),将此结果存储在 class 变量中,然后如果它解析为 false,我在点击时检查 requestStorageAccess .