I need to detect if user is on private mode in Safari both in older versions as well as Safari on IOS 11. Is there a test that would cover both?
Update: Here is a pen that attempts to combine the storage and openDatabase try-catch blocks based on jeprubio's solution below
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');
https://codepen.io/anon/pen/zpMZjp
In Safari new versions (11+) normal browser mode, that does not error on openDatabase test on the console but enters the second catch and isPrivate gets set to true. So, in Safari 11+ non-private mode is also detected as private mode.
No comments:
Post a Comment