ios - 如何在 iOS 11+ Safari 以及旧版本的 Safari 中检测隐私浏览?
<p><p>我需要检测用户是否在旧版本的 Safari 和 IOS 11 上的 Safari 中处于私有(private)模式。是否有涵盖两者的测试?</p>
<p>更新:这是一个尝试结合 storage 和 openDatabase try-catchblock 的笔,基于下面 jeprubio 的解决方案</p>
<pre><code>var isPrivate = false;
// Check private in iOS < 11
var storage = window.sessionStorage;
try {
console.log('first try for storage')
storage.setItem("someKeyHere", "test");
storage.removeItem("someKeyHere");
} catch (e) {
console.log('first catch')
if (e.code === DOMException.QUOTA_EXCEEDED_ERR && storage.length === 0) {
isPrivate = true;
}
}
// Check private in iOS 11: https://gist.github.com/cou929/7973956#gistcomment-2272103
try {
console.log('second try for opendb');
window.openDatabase(null, null, null, null);
} catch (e) {
console.log('second catch');
isPrivate = true;
}
console.log('isPrivate: ' + isPrivate)
alert((isPrivate ? 'You are' : 'You are not')+ ' in private browsing mode');
</code></pre>
<p> <a href="https://codepen.io/anon/pen/zpMZjp" rel="noreferrer noopener nofollow">https://codepen.io/anon/pen/zpMZjp</a> </p>
<p>在 Safari 新版本 (11+) 普通浏览器模式下,控制台上的 openDatabase 测试不会出错,但进入第二个 catch 并且 isPrivate 设置为 true。因此,在 Safari 11+ 中,非隐私模式也被检测为隐私模式。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个:</p>
<pre><code>var isPrivate = false;
// Check private in iOS < 11
var storage = window.sessionStorage;
try {
storage.setItem("someKeyHere", "test");
storage.removeItem("someKeyHere");
} catch (e) {
if (e.code === DOMException.QUOTA_EXCEEDED_ERR && storage.length === 0) {
isPrivate = true;
}
}
// Check private in iOS 11: https://gist.github.com/cou929/7973956#gistcomment-2272103
try {
window.openDatabase(null, null, null, null);
} catch (_) {
isPrivate = true;
}
alert((isPrivate ? 'You\'re' : 'You aren\'t')+ ' in private browsing mode');
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在 iOS 11+ Safari 以及旧版本的 Safari 中检测隐私浏览?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/48169810/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/48169810/
</a>
</p>
页:
[1]