Reputation: 63
I have a captive portal which, as of Android 5.0+ Lollipop, launches in Android's Captive Portal Browser rather than the device's default browser.
I need to somehow detect if they are in the captive portal browser (as opposed to a regular web browser) and if so, show different content.
Is it possible, by examining the User Agent, or through Javascript, to detect if they are within a Captive Portal Browser window? I have looked at the user agents on my Android 5.1 device, but I can't see anything to differentiate them:
CAPTIVE PORTAL BROWSER:
Mozilla/5.0 (Linux; Android 5.1; Elite 5 Build/LMY47D) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36
REGULAR GOOGLE CHROME:
Mozilla/5.0 (Linux; Android 5.1; Elite 5 Build/LMY47D) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.94 Mobile Safari/537.36
There is a slight difference in the version numbers but this seems inconsistent across Android/Chrome versions and not a reliable way to determine the browser type.
Are there other ways to detect it? Thanks in advance.
Upvotes: 6
Views: 4681
Reputation: 1
I just found a simple technique for finding captive portal in Android only, https://developer.chrome.com/docs/multidevice/user-agent. Follow this instruction, all captive portal in android 're webview and they have "Version" on useragent.
Upvotes: 0
Reputation: 43
The Pixel devices I'm testing on now pass both these tests -
try {
const test = 'test';
localStorage.setItem(test, test);
localStorage.removeItem(test);
sessionStorage.setItem(test, test);
sessionStorage.removeItem(test);
alert("We're in a chrome instance!")
} catch(e) {
alert("We're inside a Captive Portal!")
}
Running on Android 10 - Pixel XL and regular Pixel
For a lot of other devices - the above localStorage test works fine - we go into a catch block and determine that we are not inside a full fledged Chrome tab/Android Default browser tab, but Android has changed the way it runs captive portal it seems.
Upvotes: 1
Reputation: 81
You can use Modernizr to detect the existence of the Fetch API or Local Storage features, both of which exist in regular Google Chrome and Firefox, etc but aren't available in the captive portal browser.
Upvotes: 3