简体   繁体   English

检测android webview

[英]Detect android webview

I have an html-javascript page, and I need to detect whenever it open on web view (Like inside facebook webview, twitter webview, etc.), and if it a webview - display another content.我有一个html-javascript页面,我需要检测它何时在 web 视图中打开(比如在 facebook webview、twitter webview 等中),如果它是 webview - 显示另一个内容。

Note: I do not control the third-party Android apps, so I cannot make changes to their code.注意:我不控制第三方 Android 应用程序,因此我无法更改它们的代码。

I already found a way to detect an IOS webview (Found it on stackoverflow):我已经找到了一种检测 IOS webview 的方法(在 stackoverflow 上找到):

var isIosWebview =/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent)

Now I'm looking for a javascript code that can detect Android web view.现在我正在寻找可以检测 Android 网页视图的 javascript 代码。

Help?帮助?

You can't detect it by only using the user agent string, as any app that uses WebView can set the UA string to anything it wants.您不能仅使用用户代理字符串来检测它,因为任何使用 WebView 的应用程序都可以将 UA 字符串设置为它想要的任何内容。

If you still insist on using the UA string, this is the explanation of the official docs: initially, Chromium-based WebView was using .0.0.0 as Chrome version suffix;如果你还坚持使用 UA 字符串,这是官方文档的解释:最初,基于 Chromium 的 WebView 使用.0.0.0作为 Chrome 版本后缀; in the newer versions ; wv在较新的版本中; wv ; wv was added into the platform part of the UA string. ; wv被添加到 UA 字符串的平台部分。 Note that pre-KitKat WebViews didn't have the Chrome part.请注意,KitKat 之前的 WebViews 没有Chrome部分。 See older posts about that: Android, webview user agent vs browser user agent请参阅关于此的旧帖子: Android、webview 用户代理与浏览器用户代理

Another hint of WebView involvement is presence of X-Requested-With HTTP header with an application package name.另一个涉及 WebView 的提示是存在带有应用程序包名称的X-Requested-With HTTP 标头。 Note that this header is also set by XMLHttpRequest, so you need to look for the actual value of the header.注意这个header也是由XMLHttpRequest设置的,所以需要查找header的实际值。

The statement that WebView doesn't support iframes is not correct. WebView 不支持 iframes 的说法是不正确的。

The info others provided in this thread gave me what I needed to solve this problem in my case.该线程中其他人提供的信息为我提供了解决此问题所需的信息。 For others, here is the resulting JS regex which represents the detection described in the accepted answer:对于其他人,这是生成的 JS 正则表达式,它代表了已接受答案中描述的检测:

/(Version\/\d+.*\/\d+.0.0.0 Mobile|; ?wv|(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari))/i.test(navigator.userAgent)

The regex includes cases for old Android, new Android, iOS versions.正则表达式包括适用于旧 Android、新 Android、iOS 版本的案例。

I know how to do it.我知道该怎么做。 It is very simple.这很简单。 Because there is an object used by Android web view to trigger functions in its Android app via javascript.因为 Android 网络视图使用一个对象通过 javascript 触发其 Android 应用程序中的功能。 So in your js code you can use:所以在你的 js 代码中你可以使用:

if (typeof Android === "undefined") {
    // do something if is NOT a web view
} else {
    // do something else if is a web view
}

I needed to detect if the browser was an Android WebView without using the User-Agent (since it can be spoofed), and without reading the Headers server-side.我需要在不使用 User-Agent(因为它可以被欺骗)并且不读取 Headers 服务器端的情况下检测浏览器是否是 Android WebView。

It appears they were quite sneaky in hiding this, but you can check if the window has a property named "Android " (with a space at the end).看起来他们在隐藏这个方面非常狡猾,但是您可以检查window是否具有名为"Android "的属性(末尾有一个空格)。 This appears to be true only when being used as a WebView inside an app, and not in Chrome on the same device.这似乎仅在用作应用程序内的 WebView 时才true ,而不是在同一设备上的 Chrome 中。

const isAndroidWebView = window.hasOwnProperty('Android ')

Disclaimer: I have only verified this on a Pixel 3 running Android 10, and a Nokia 6 running Android 9. Please comment if this does or doesn't work for your device.免责声明:我仅在运行 Android 10 的 Pixel 3 和运行 Android 9 的诺基亚 6 上验证了这一点。请评论这是否适用于您的设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM